From c73fc288476c5fb6987c3dfbb7d67ae4404fbfa0 Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Fri, 31 May 2019 11:20:02 -0400 Subject: [PATCH] Minor upstream changes --- .../command/SnapshotUtilCommands.java | 27 ++++--------- .../worldedit/world/chunk/AnvilChunk13.java | 40 +++++-------------- .../worldedit/world/snapshot/Snapshot.java | 19 +++++---- .../worldedit/world/storage/ChunkStore.java | 1 + 4 files changed, 28 insertions(+), 59 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java index 171c16b70..446277b9a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java @@ -56,7 +56,6 @@ public class SnapshotUtilCommands { aliases = { "restore", "/restore" }, usage = "[snapshot]", desc = "Restore the selection from a snapshot", - min = 0, max = 1 ) @Logging(REGION) @@ -112,23 +111,14 @@ public class SnapshotUtilCommands { } } - ChunkStore chunkStore = null; // Load chunk store - try { - chunkStore = snapshot.getChunkStore(); + SnapshotRestore restore; + try (ChunkStore chunkStore = snapshot.getChunkStore()) { BBC.SNAPSHOT_LOADED.send(player, snapshot.getName()); - } catch (DataException e) { - player.printError(BBC.getPrefix() + "Failed to load snapshot: " + e.getMessage()); - return; - } catch (IOException e) { - player.printError(BBC.getPrefix() + "Failed to load snapshot: " + e.getMessage()); - return; - } - try { // Restore snapshot - SnapshotRestore restore = new SnapshotRestore(chunkStore, editSession, region); + restore = new SnapshotRestore(chunkStore, editSession, region); //player.print(restore.getChunksAffected() + " chunk(s) will be loaded."); restore.restore(); @@ -137,21 +127,18 @@ public class SnapshotUtilCommands { String error = restore.getLastErrorMessage(); if (error != null) { BBC.SNAPSHOT_ERROR_RESTORE.send(player); - player.printError(BBC.getPrefix() + "Last error: " + error); + player.printError("Last error: " + error); } else { BBC.SNAPSHOT_ERROR_RESTORE_CHUNKS.send(player); } } else { - player.print(BBC.getPrefix() + String.format("Restored; %d " + player.print(String.format("Restored; %d " + "missing chunks and %d other errors.", restore.getMissingChunks().size(), restore.getErrorChunks().size())); } - } finally { - try { - chunkStore.close(); - } catch (IOException ignored) { - } + } catch (DataException | IOException e) { + player.printError("Failed to load snapshot: " + e.getMessage()); } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java index a8d2ac171..ba5473839 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java @@ -35,12 +35,11 @@ import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.storage.InvalidFormatException; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.annotation.Nullable; - /** * The chunk format for Minecraft 1.13 and newer */ @@ -160,11 +159,13 @@ public class AnvilChunk13 implements Chunk { * @throws DataException */ private void populateTileEntities() throws DataException { + tileEntities = new HashMap<>(); + if (!rootTag.getValue().containsKey("TileEntities")) { + return; + } List tags = NBTUtils.getChildTag(rootTag.getValue(), "TileEntities", ListTag.class).getValue(); - tileEntities = new HashMap<>(); - for (Tag tag : tags) { if (!(tag instanceof CompoundTag)) { throw new InvalidFormatException("CompoundTag expected in TileEntities"); @@ -172,33 +173,10 @@ public class AnvilChunk13 implements Chunk { CompoundTag t = (CompoundTag) tag; - int x = 0; - int y = 0; - int z = 0; - - Map values = new HashMap<>(); - - for (Map.Entry entry : t.getValue().entrySet()) { - switch (entry.getKey()) { - case "x": - if (entry.getValue() instanceof IntTag) { - x = ((IntTag) entry.getValue()).getValue(); - } - break; - case "y": - if (entry.getValue() instanceof IntTag) { - y = ((IntTag) entry.getValue()).getValue(); - } - break; - case "z": - if (entry.getValue() instanceof IntTag) { - z = ((IntTag) entry.getValue()).getValue(); - } - break; - } - - values.put(entry.getKey(), entry.getValue()); - } + Map values = new HashMap<>(t.getValue()); + int x = ((IntTag) values.get("x")).getValue(); + int y = ((IntTag) values.get("y")).getValue(); + int z = ((IntTag) values.get("z")).getValue(); BlockVector3 vec = BlockVector3.at(x, y, z); tileEntities.put(vec, values); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java index 0af2b6e18..8dc7735b1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java @@ -35,6 +35,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; import java.util.Calendar; +import java.util.Locale; import java.util.zip.ZipFile; /** @@ -83,7 +84,8 @@ public class Snapshot implements Comparable { * @throws DataException */ private ChunkStore internalGetChunkStore() throws IOException, DataException { - if (file.getName().toLowerCase().endsWith(".zip")) { + String lowerCaseFileName = file.getName().toLowerCase(Locale.ROOT); + if (lowerCaseFileName.endsWith(".zip")) { try { ChunkStore chunkStore = new TrueZipMcRegionChunkStore(file); @@ -101,9 +103,9 @@ public class Snapshot implements Comparable { return chunkStore; } - } else if (file.getName().toLowerCase().endsWith(".tar.bz2") - || file.getName().toLowerCase().endsWith(".tar.gz") - || file.getName().toLowerCase().endsWith(".tar")) { + } else if (lowerCaseFileName.endsWith(".tar.bz2") + || lowerCaseFileName.endsWith(".tar.gz") + || lowerCaseFileName.endsWith(".tar")) { try { ChunkStore chunkStore = new TrueZipMcRegionChunkStore(file); @@ -133,14 +135,15 @@ public class Snapshot implements Comparable { */ public boolean containsWorld(String worldname) { try { - if (file.getName().toLowerCase().endsWith(".zip")) { + String lowerCaseFileName = file.getName().toLowerCase(Locale.ROOT); + if (lowerCaseFileName.endsWith(".zip")) { try (ZipFile entry = new ZipFile(file)) { return (entry.getEntry(worldname) != null || entry.getEntry(worldname + "/level.dat") != null); } - } else if (file.getName().toLowerCase().endsWith(".tar.bz2") - || file.getName().toLowerCase().endsWith(".tar.gz") - || file.getName().toLowerCase().endsWith(".tar")) { + } else if (lowerCaseFileName.endsWith(".tar.bz2") + || lowerCaseFileName.endsWith(".tar.gz") + || lowerCaseFileName.endsWith(".tar")) { try { de.schlichtherle.util.zip.ZipFile entry = new de.schlichtherle.util.zip.ZipFile(file); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java index 319503740..871e4fb15 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java @@ -114,6 +114,7 @@ public abstract class ChunkStore implements Closeable { return new OldChunk(world, tag); } + @Override public void close() throws IOException { }