2014-04-03 02:08:50 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2014-04-03 02:08:50 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2014-04-03 02:08:50 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2014-04-03 02:08:50 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-04-03 02:08:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.command;
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
|
|
|
|
import static com.sk89q.worldedit.internal.anvil.ChunkDeleter.DELCHUNKS_FILE_NAME;
|
|
|
|
|
|
|
|
import com.google.gson.JsonIOException;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissions;
|
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
|
|
|
|
import com.sk89q.worldedit.command.util.Logging;
|
2019-09-02 19:22:43 +00:00
|
|
|
import com.sk89q.worldedit.command.util.WorldEditAsyncCommandBuilder;
|
2014-06-28 08:01:49 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
2019-08-06 15:29:09 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.internal.anvil.ChunkDeleter;
|
|
|
|
import com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
|
|
|
import com.sk89q.worldedit.regions.Region;
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
|
2019-09-02 19:22:43 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.text.Component;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
|
|
|
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
|
|
|
|
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
|
2019-08-06 15:29:09 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.storage.LegacyChunkStore;
|
|
|
|
import com.sk89q.worldedit.world.storage.McRegionChunkStore;
|
2019-07-06 00:46:48 +00:00
|
|
|
import java.io.File;
|
2014-04-05 04:02:56 +00:00
|
|
|
import java.io.IOException;
|
2019-07-06 00:46:48 +00:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.util.ArrayList;
|
2019-09-02 19:22:43 +00:00
|
|
|
import java.util.List;
|
2014-04-05 04:02:56 +00:00
|
|
|
import java.util.Set;
|
2019-07-06 00:46:48 +00:00
|
|
|
import org.enginehub.piston.annotation.Command;
|
|
|
|
import org.enginehub.piston.annotation.CommandContainer;
|
|
|
|
import org.enginehub.piston.annotation.param.ArgFlag;
|
|
|
|
import org.enginehub.piston.exception.StopExecutionException;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2014-04-03 02:08:50 +00:00
|
|
|
/**
|
2014-06-28 08:01:49 +00:00
|
|
|
* Commands for working with chunks.
|
2014-04-03 02:08:50 +00:00
|
|
|
*/
|
2019-07-06 00:46:48 +00:00
|
|
|
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
|
2014-04-03 02:08:50 +00:00
|
|
|
public class ChunkCommands {
|
2014-06-28 08:01:49 +00:00
|
|
|
|
|
|
|
private final WorldEdit worldEdit;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2014-06-28 08:01:49 +00:00
|
|
|
public ChunkCommands(WorldEdit worldEdit) {
|
|
|
|
checkNotNull(worldEdit);
|
|
|
|
this.worldEdit = worldEdit;
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "chunkinfo",
|
|
|
|
desc = "Get information about the chunk you're inside"
|
2014-04-03 02:08:50 +00:00
|
|
|
)
|
|
|
|
@CommandPermissions("worldedit.chunkinfo")
|
2019-07-06 00:46:48 +00:00
|
|
|
public void chunkInfo(Player player) {
|
2019-08-06 15:29:09 +00:00
|
|
|
Location pos = player.getBlockLocation();
|
2014-04-03 02:08:50 +00:00
|
|
|
int chunkX = (int) Math.floor(pos.getBlockX() / 16.0);
|
|
|
|
int chunkZ = (int) Math.floor(pos.getBlockZ() / 16.0);
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
final BlockVector2 chunkPos = BlockVector2.at(chunkX, chunkZ);
|
2019-06-06 01:17:34 +00:00
|
|
|
player.print("Chunk: " + chunkX + ", " + chunkZ);
|
2019-07-06 00:46:48 +00:00
|
|
|
player.print("Old format: " + LegacyChunkStore.getFilename(chunkPos));
|
|
|
|
player.print("McRegion: region/" + McRegionChunkStore.getFilename(chunkPos));
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "listchunks",
|
|
|
|
desc = "List chunks that your selection includes"
|
2014-04-03 02:08:50 +00:00
|
|
|
)
|
|
|
|
@CommandPermissions("worldedit.listchunks")
|
2019-08-06 15:29:09 +00:00
|
|
|
public void listChunks(Actor actor, World world, LocalSession session,
|
2019-07-06 00:46:48 +00:00
|
|
|
@ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) throws WorldEditException {
|
2019-09-02 19:22:43 +00:00
|
|
|
final Region region = session.getSelection(world);
|
2018-12-23 16:19:33 +00:00
|
|
|
|
2019-09-02 19:22:43 +00:00
|
|
|
WorldEditAsyncCommandBuilder.createAndSendMessage(actor,
|
|
|
|
() -> new ChunkListPaginationBox(region).create(page),
|
|
|
|
"Listing chunks for " + actor.getName());
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2019-07-06 00:46:48 +00:00
|
|
|
name = "delchunks",
|
|
|
|
desc = "Delete chunks that your selection includes"
|
2014-04-03 02:08:50 +00:00
|
|
|
)
|
|
|
|
@CommandPermissions("worldedit.delchunks")
|
|
|
|
@Logging(REGION)
|
2019-08-06 15:29:09 +00:00
|
|
|
public void deleteChunks(Actor actor, World world, LocalSession session,
|
2019-07-06 00:46:48 +00:00
|
|
|
@ArgFlag(name = 'o', desc = "Only delete chunks older than the specified time.", def = "")
|
|
|
|
ZonedDateTime beforeTime) throws WorldEditException {
|
2019-08-06 15:29:09 +00:00
|
|
|
Path worldDir = world.getStoragePath();
|
2019-07-06 00:46:48 +00:00
|
|
|
if (worldDir == null) {
|
|
|
|
throw new StopExecutionException(TextComponent.of("Couldn't find world folder for this world."));
|
|
|
|
}
|
2014-04-03 02:08:50 +00:00
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
File chunkFile = worldEdit.getWorkingDirectoryFile(DELCHUNKS_FILE_NAME);
|
|
|
|
Path chunkPath = chunkFile.toPath();
|
|
|
|
ChunkDeletionInfo currentInfo = null;
|
|
|
|
if (Files.exists(chunkPath)) {
|
2014-04-03 02:08:50 +00:00
|
|
|
try {
|
2019-07-06 00:46:48 +00:00
|
|
|
currentInfo = ChunkDeleter.readInfo(chunkFile.toPath());
|
2014-04-03 02:08:50 +00:00
|
|
|
} catch (IOException e) {
|
2019-07-06 00:46:48 +00:00
|
|
|
throw new StopExecutionException(TextComponent.of("Error reading existing chunk file."));
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
2019-07-06 00:46:48 +00:00
|
|
|
}
|
|
|
|
if (currentInfo == null) {
|
|
|
|
currentInfo = new ChunkDeletionInfo();
|
|
|
|
currentInfo.batches = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ChunkDeletionInfo.ChunkBatch newBatch = new ChunkDeletionInfo.ChunkBatch();
|
|
|
|
newBatch.worldPath = worldDir.toAbsolutePath().normalize().toString();
|
|
|
|
newBatch.backup = true;
|
2019-08-06 15:29:09 +00:00
|
|
|
final Region selection = session.getSelection(world);
|
2019-07-06 00:46:48 +00:00
|
|
|
if (selection instanceof CuboidRegion) {
|
2019-08-06 15:29:09 +00:00
|
|
|
newBatch.minChunk = selection.getMinimumPoint().shr(4).toBlockVector2();
|
|
|
|
newBatch.maxChunk = selection.getMaximumPoint().shr(4).toBlockVector2();
|
2014-04-03 02:08:50 +00:00
|
|
|
} else {
|
2019-07-06 00:46:48 +00:00
|
|
|
// this has a possibility to OOM for very large selections still
|
|
|
|
Set<BlockVector2> chunks = selection.getChunks();
|
|
|
|
newBatch.chunks = new ArrayList<>(chunks);
|
|
|
|
}
|
|
|
|
if (beforeTime != null) {
|
|
|
|
newBatch.deletionPredicates = new ArrayList<>();
|
|
|
|
ChunkDeletionInfo.DeletionPredicate timePred = new ChunkDeletionInfo.DeletionPredicate();
|
|
|
|
timePred.property = "modification";
|
|
|
|
timePred.comparison = "<";
|
|
|
|
timePred.value = String.valueOf((int) beforeTime.toOffsetDateTime().toEpochSecond());
|
|
|
|
newBatch.deletionPredicates.add(timePred);
|
|
|
|
}
|
|
|
|
currentInfo.batches.add(newBatch);
|
|
|
|
|
|
|
|
try {
|
|
|
|
ChunkDeleter.writeInfo(currentInfo, chunkPath);
|
|
|
|
} catch (IOException | JsonIOException e) {
|
|
|
|
throw new StopExecutionException(TextComponent.of("Failed to write chunk list: " + e.getMessage()));
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:29:09 +00:00
|
|
|
actor.print(String.format("%d chunk(s) have been marked for deletion the next time the server starts.",
|
2019-07-06 00:46:48 +00:00
|
|
|
newBatch.getChunkCount()));
|
|
|
|
if (currentInfo.batches.size() > 1) {
|
2019-08-06 15:29:09 +00:00
|
|
|
actor.printDebug(String.format("%d chunks total marked for deletion. (May have overlaps).",
|
2019-07-06 00:46:48 +00:00
|
|
|
currentInfo.batches.stream().mapToInt(ChunkDeletionInfo.ChunkBatch::getChunkCount).sum()));
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
2019-08-06 15:29:09 +00:00
|
|
|
actor.print(TextComponent.of("You can mark more chunks for deletion, or to stop now, run: ", TextColor.LIGHT_PURPLE)
|
2019-07-06 00:46:48 +00:00
|
|
|
.append(TextComponent.of("/stop", TextColor.AQUA)
|
|
|
|
.clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/stop"))));
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
2019-04-02 22:21:02 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
private static class ChunkListPaginationBox extends PaginationBox.ListPaginationBox {
|
2019-09-02 19:22:43 +00:00
|
|
|
//private final Region region;
|
|
|
|
|
|
|
|
ChunkListPaginationBox(Region region) {
|
2019-10-23 04:23:52 +00:00
|
|
|
super("Selected Chunks", "/listchunks -p %page%", region.getChunks());
|
2019-09-02 19:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Component getComponent(int number) {
|
2019-10-23 04:23:52 +00:00
|
|
|
return create(number);
|
2019-09-02 19:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|