mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-13 12:48:35 +00:00
Re-add delchunks command (#481)
The new command now writes a json file to WorldEdit's working directory with instructions on which chunks to delete, which is read by the plugin/mod at startup and calls the ChunkDeleter. The chunk deleter parses the json and iterates the instructions, backing up .mca files as it goes and overwriting the offset headers with 0 wherever a chunk needs to be deleted. This allows Minecraft to reclaim the space used for that chunk, as well as forcing it to be generated from scratch next time the area is loaded.
This commit is contained in:
@ -19,10 +19,7 @@
|
||||
|
||||
package com.sk89q.worldedit.command;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
|
||||
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
@ -30,22 +27,36 @@ import com.sk89q.worldedit.command.util.CommandPermissions;
|
||||
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
|
||||
import com.sk89q.worldedit.command.util.Logging;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.internal.anvil.ChunkDeleter;
|
||||
import com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo;
|
||||
import com.sk89q.worldedit.math.BlockVector2;
|
||||
import com.sk89q.worldedit.math.MathUtils;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
|
||||
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;
|
||||
import com.sk89q.worldedit.world.storage.LegacyChunkStore;
|
||||
import com.sk89q.worldedit.world.storage.McRegionChunkStore;
|
||||
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;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Commands for working with chunks.
|
||||
*/
|
||||
@ -69,15 +80,10 @@ public class ChunkCommands {
|
||||
int chunkX = (int) Math.floor(pos.getBlockX() / 16.0);
|
||||
int chunkZ = (int) Math.floor(pos.getBlockZ() / 16.0);
|
||||
|
||||
String folder1 = Integer.toString(MathUtils.divisorMod(chunkX, 64), 36);
|
||||
String folder2 = Integer.toString(MathUtils.divisorMod(chunkZ, 64), 36);
|
||||
String filename = "c." + Integer.toString(chunkX, 36)
|
||||
+ "." + Integer.toString(chunkZ, 36) + ".dat";
|
||||
|
||||
final BlockVector2 chunkPos = BlockVector2.at(chunkX, chunkZ);
|
||||
player.print("Chunk: " + chunkX + ", " + chunkZ);
|
||||
player.print("Old format: " + folder1 + "/" + folder2 + "/" + filename);
|
||||
player.print("McRegion: region/" + McRegionChunkStore.getFilename(
|
||||
BlockVector2.at(chunkX, chunkZ)));
|
||||
player.print("Old format: " + LegacyChunkStore.getFilename(chunkPos));
|
||||
player.print("McRegion: region/" + McRegionChunkStore.getFilename(chunkPos));
|
||||
}
|
||||
|
||||
@Command(
|
||||
@ -86,7 +92,7 @@ public class ChunkCommands {
|
||||
)
|
||||
@CommandPermissions("worldedit.listchunks")
|
||||
public void listChunks(Player player, LocalSession session,
|
||||
@ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) throws WorldEditException {
|
||||
@ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) throws WorldEditException {
|
||||
Set<BlockVector2> chunks = session.getSelection(player.getWorld()).getChunks();
|
||||
|
||||
PaginationBox paginationBox = PaginationBox.fromStrings("Selected Chunks", "/listchunks -p %page%",
|
||||
@ -100,82 +106,64 @@ public class ChunkCommands {
|
||||
)
|
||||
@CommandPermissions("worldedit.delchunks")
|
||||
@Logging(REGION)
|
||||
public void deleteChunks(Player player, LocalSession session) throws WorldEditException {
|
||||
player.print("Note that this command does not yet support the mcregion format.");
|
||||
LocalConfiguration config = worldEdit.getConfiguration();
|
||||
|
||||
Set<BlockVector2> chunks = session.getSelection(player.getWorld()).getChunks();
|
||||
FileOutputStream out = null;
|
||||
|
||||
if (config.shellSaveType == null) {
|
||||
player.printError("Shell script type must be configured: 'bat' or 'bash' expected.");
|
||||
} else if (config.shellSaveType.equalsIgnoreCase("bat")) {
|
||||
try {
|
||||
out = new FileOutputStream("worldedit-delchunks.bat");
|
||||
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
|
||||
writer.write("@ECHO off\r\n");
|
||||
writer.write("ECHO This batch file was generated by WorldEdit.\r\n");
|
||||
writer.write("ECHO It contains a list of chunks that were in the selected region\r\n");
|
||||
writer.write("ECHO at the time that the /delchunks command was used. Run this file\r\n");
|
||||
writer.write("ECHO in order to delete the chunk files listed in this file.\r\n");
|
||||
writer.write("ECHO.\r\n");
|
||||
writer.write("PAUSE\r\n");
|
||||
|
||||
for (BlockVector2 chunk : chunks) {
|
||||
String filename = LegacyChunkStore.getFilename(chunk);
|
||||
writer.write("ECHO " + filename + "\r\n");
|
||||
writer.write("DEL \"world/" + filename + "\"\r\n");
|
||||
}
|
||||
|
||||
writer.write("ECHO Complete.\r\n");
|
||||
writer.write("PAUSE\r\n");
|
||||
writer.close();
|
||||
player.print("worldedit-delchunks.bat written. Run it when no one is near the region.");
|
||||
} catch (IOException e) {
|
||||
player.printError("Error occurred: " + e.getMessage());
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException ignored) { }
|
||||
}
|
||||
}
|
||||
} else if (config.shellSaveType.equalsIgnoreCase("bash")) {
|
||||
try {
|
||||
out = new FileOutputStream("worldedit-delchunks.sh");
|
||||
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
|
||||
writer.write("#!/bin/bash\n");
|
||||
writer.write("echo This shell file was generated by WorldEdit.\n");
|
||||
writer.write("echo It contains a list of chunks that were in the selected region\n");
|
||||
writer.write("echo at the time that the /delchunks command was used. Run this file\n");
|
||||
writer.write("echo in order to delete the chunk files listed in this file.\n");
|
||||
writer.write("echo\n");
|
||||
writer.write("read -p \"Press any key to continue...\"\n");
|
||||
|
||||
for (BlockVector2 chunk : chunks) {
|
||||
String filename = LegacyChunkStore.getFilename(chunk);
|
||||
writer.write("echo " + filename + "\n");
|
||||
writer.write("rm \"world/" + filename + "\"\n");
|
||||
}
|
||||
|
||||
writer.write("echo Complete.\n");
|
||||
writer.write("read -p \"Press any key to continue...\"\n");
|
||||
writer.close();
|
||||
player.print("worldedit-delchunks.sh written. Run it when no one is near the region.");
|
||||
player.print("You will have to chmod it to be executable.");
|
||||
} catch (IOException e) {
|
||||
player.printError("Error occurred: " + e.getMessage());
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
player.printError("Shell script type must be configured: 'bat' or 'bash' expected.");
|
||||
public void deleteChunks(Player player, LocalSession session,
|
||||
@ArgFlag(name = 'o', desc = "Only delete chunks older than the specified time.", def = "")
|
||||
ZonedDateTime beforeTime) throws WorldEditException {
|
||||
Path worldDir = player.getWorld().getStoragePath();
|
||||
if (worldDir == null) {
|
||||
throw new StopExecutionException(TextComponent.of("Couldn't find world folder for this world."));
|
||||
}
|
||||
|
||||
File chunkFile = worldEdit.getWorkingDirectoryFile(DELCHUNKS_FILE_NAME);
|
||||
Path chunkPath = chunkFile.toPath();
|
||||
ChunkDeletionInfo currentInfo = null;
|
||||
if (Files.exists(chunkPath)) {
|
||||
try {
|
||||
currentInfo = ChunkDeleter.readInfo(chunkFile.toPath());
|
||||
} 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(player.getWorld());
|
||||
int chunkCount;
|
||||
if (selection instanceof CuboidRegion) {
|
||||
newBatch.minChunk = BlockVector2.at(selection.getMinimumPoint().getBlockX() >> 4, selection.getMinimumPoint().getBlockZ() >> 4);
|
||||
newBatch.maxChunk = BlockVector2.at(selection.getMaximumPoint().getBlockX() >> 4, selection.getMaximumPoint().getBlockZ() >> 4);
|
||||
final BlockVector2 dist = newBatch.maxChunk.subtract(newBatch.minChunk).add(1, 1);
|
||||
chunkCount = dist.getBlockX() * dist.getBlockZ();
|
||||
} else {
|
||||
// this has a possibility to OOM for very large selections still
|
||||
Set<BlockVector2> chunks = selection.getChunks();
|
||||
newBatch.chunks = new ArrayList<>(chunks);
|
||||
chunkCount = chunks.size();
|
||||
}
|
||||
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()));
|
||||
}
|
||||
|
||||
player.print(String.format("%d chunk(s) have been marked for deletion and will be deleted the next time the server starts.", chunkCount));
|
||||
player.print(TextComponent.of("You can mark more chunks for deletion, or to stop the server now, run: ", TextColor.LIGHT_PURPLE)
|
||||
.append(TextComponent.of("/stop", TextColor.AQUA).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/stop"))));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user