From 2c1b234e38a1487d5e4f5eb208bf250722ad023a Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Thu, 2 Aug 2018 22:53:25 +1000 Subject: [PATCH] Fixed some issues with Sponge schematics. --- .../clipboard/io/BuiltInClipboardFormat.java | 49 +++++++++---------- .../clipboard/io/SpongeSchematicReader.java | 11 +++-- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java index 574c5b881..5c8672f08 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java @@ -20,16 +20,18 @@ package com.sk89q.worldedit.extent.clipboard.io; import com.google.common.collect.ImmutableSet; -import com.sk89q.jnbt.NBTConstants; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.NBTInputStream; import com.sk89q.jnbt.NBTOutputStream; +import com.sk89q.jnbt.NamedTag; +import com.sk89q.jnbt.Tag; -import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Map; import java.util.Set; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -62,24 +64,22 @@ public enum BuiltInClipboardFormat implements ClipboardFormat { @Override public boolean isFormat(File file) { - try (DataInputStream str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) { - if ((str.readByte() & 0xFF) != NBTConstants.TYPE_COMPOUND) { + try (NBTInputStream str = new NBTInputStream(new GZIPInputStream(new FileInputStream(file)))) { + NamedTag rootTag = str.readNamedTag(); + if (!rootTag.getName().equals("Schematic")) { return false; } - byte[] nameBytes = new byte[str.readShort() & 0xFFFF]; - str.readFully(nameBytes); - String name = new String(nameBytes, NBTConstants.CHARSET); - if (!name.equals("Schematic")) { + CompoundTag schematicTag = (CompoundTag) rootTag.getTag(); + + // Check + Map schematic = schematicTag.getValue(); + if (!schematic.containsKey("Blocks")) { return false; } - str.readByte(); // Ignore - nameBytes = new byte[str.readShort() & 0xFFFF]; - str.readFully(nameBytes); - name = new String(nameBytes, NBTConstants.CHARSET); - return !name.equals("Version"); } catch (IOException e) { return false; } + return true; } }, SPONGE_SCHEMATIC("sponge", "schem") { @@ -103,26 +103,23 @@ public enum BuiltInClipboardFormat implements ClipboardFormat { @Override public boolean isFormat(File file) { - try (DataInputStream str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) { - if ((str.readByte() & 0xFF) != NBTConstants.TYPE_COMPOUND) { + try (NBTInputStream str = new NBTInputStream(new GZIPInputStream(new FileInputStream(file)))) { + NamedTag rootTag = str.readNamedTag(); + if (!rootTag.getName().equals("Schematic")) { return false; } - byte[] nameBytes = new byte[str.readShort() & 0xFFFF]; - str.readFully(nameBytes); - String name = new String(nameBytes, NBTConstants.CHARSET); - if (!name.equals("Schematic")) { + CompoundTag schematicTag = (CompoundTag) rootTag.getTag(); + + // Check + Map schematic = schematicTag.getValue(); + if (!schematic.containsKey("Version")) { return false; } - if ((str.readByte() & 0xFF) != NBTConstants.TYPE_INT) { - return false; - } - nameBytes = new byte[str.readShort() & 0xFFFF]; - str.readFully(nameBytes); - name = new String(nameBytes, NBTConstants.CHARSET); - return name.equals("Version"); } catch (IOException e) { return false; } + + return true; } }; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java index ad1609ccd..593cd4432 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java @@ -119,7 +119,7 @@ public class SpongeSchematicReader extends NBTSchematicReader { int originZ = requireTag(metadata, "WEOriginZ", IntTag.class).getValue(); Vector min = new Vector(originX, originY, originZ); origin = min.subtract(offset); - region = new CuboidRegion(min, min.add(width, height, length).subtract(Vector.ONE)); + region = new CuboidRegion(origin, origin.add(width, height, length).subtract(Vector.ONE)); } else { origin = Vector.ZERO.subtract(offset); region = new CuboidRegion(origin, origin.add(width, height, length).subtract(Vector.ONE)); @@ -134,6 +134,9 @@ public class SpongeSchematicReader extends NBTSchematicReader { Map palette = new HashMap<>(); ParserContext parserContext = new ParserContext(); + parserContext.setRestricted(false); + parserContext.setTryLegacy(false); + parserContext.setPreferringWildcard(false); for (String palettePart : paletteObject.keySet()) { int id = requireTag(paletteObject, palettePart, IntTag.class).getValue(); @@ -186,9 +189,9 @@ public class SpongeSchematicReader extends NBTSchematicReader { i++; } // index = (y * length + z) * width + x - int y = index / (width * length); - int z = (index % (width * length)) / width; - int x = (index % (width * length)) % width; + int y = origin.getBlockY() + index / (width * length); + int z = origin.getBlockZ() + (index % (width * length)) / width; + int x = origin.getBlockX() + (index % (width * length)) % width; BlockState state = palette.get(value); BlockVector pt = new BlockVector(x, y, z); try {