Search in sources :

Example 1 with ChunkDeletionInfo

use of com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo in project FastAsyncWorldEdit by IntellectualSites.

the class ChunkCommands method deleteChunks.

@Command(name = "delchunks", aliases = { "/delchunks" }, desc = "Delete chunks that your selection includes")
@CommandPermissions("worldedit.delchunks")
@Logging(REGION)
public void deleteChunks(Actor actor, World world, LocalSession session, @ArgFlag(name = 'o', desc = "Only delete chunks older than the specified time.") ZonedDateTime beforeTime) throws WorldEditException {
    Path worldDir = world.getStoragePath();
    if (worldDir == null) {
        throw new StopExecutionException(TextComponent.of("Couldn't find world folder for this world."));
    }
    Path chunkPath = worldEdit.getWorkingDirectoryPath(DELCHUNKS_FILE_NAME);
    ChunkDeletionInfo currentInfo = null;
    if (Files.exists(chunkPath)) {
        try {
            currentInfo = ChunkDeleter.readInfo(chunkPath);
        } catch (IOException e) {
            throw new StopExecutionException(TextComponent.of("Error reading existing chunk file."));
        }
    }
    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;
    final Region selection = session.getSelection(world);
    if (selection instanceof CuboidRegion) {
        newBatch.minChunk = selection.getMinimumPoint().shr(4).toBlockVector2();
        newBatch.maxChunk = selection.getMaximumPoint().shr(4).toBlockVector2();
    } else {
        // 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()));
    }
    actor.print(TextComponent.of(String.format("%d chunk(s) have been marked for deletion the next time the server starts.", newBatch.getChunkCount())));
    if (currentInfo.batches.size() > 1) {
        actor.printDebug(TextComponent.of(String.format("%d chunks total marked for deletion. (May have overlaps).", currentInfo.batches.stream().mapToInt(ChunkDeletionInfo.ChunkBatch::getChunkCount).sum())));
    }
    actor.print(TextComponent.of("You can mark more chunks for deletion, or to stop now, run: ", TextColor.LIGHT_PURPLE).append(TextComponent.of("/stop", TextColor.AQUA).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/stop"))));
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockVector2(com.sk89q.worldedit.math.BlockVector2) ChunkDeletionInfo(com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo) StopExecutionException(org.enginehub.piston.exception.StopExecutionException) JsonIOException(com.google.gson.JsonIOException) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) Logging(com.sk89q.worldedit.command.util.Logging) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Aggregations

JsonIOException (com.google.gson.JsonIOException)1 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)1 Logging (com.sk89q.worldedit.command.util.Logging)1 ChunkDeletionInfo (com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo)1 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)1 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)1 Region (com.sk89q.worldedit.regions.Region)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Command (org.enginehub.piston.annotation.Command)1 StopExecutionException (org.enginehub.piston.exception.StopExecutionException)1