Fixed some issues with Sponge schematics.

This commit is contained in:
Matthew Miller 2018-08-02 22:53:25 +10:00
parent ea349bdbf1
commit 2c1b234e38
2 changed files with 30 additions and 30 deletions

View File

@ -20,16 +20,18 @@
package com.sk89q.worldedit.extent.clipboard.io; package com.sk89q.worldedit.extent.clipboard.io;
import com.google.common.collect.ImmutableSet; 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.NBTInputStream;
import com.sk89q.jnbt.NBTOutputStream; 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -62,24 +64,22 @@ public enum BuiltInClipboardFormat implements ClipboardFormat {
@Override @Override
public boolean isFormat(File file) { public boolean isFormat(File file) {
try (DataInputStream str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) { try (NBTInputStream str = new NBTInputStream(new GZIPInputStream(new FileInputStream(file)))) {
if ((str.readByte() & 0xFF) != NBTConstants.TYPE_COMPOUND) { NamedTag rootTag = str.readNamedTag();
if (!rootTag.getName().equals("Schematic")) {
return false; return false;
} }
byte[] nameBytes = new byte[str.readShort() & 0xFFFF]; CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
str.readFully(nameBytes);
String name = new String(nameBytes, NBTConstants.CHARSET); // Check
if (!name.equals("Schematic")) { Map<String, Tag> schematic = schematicTag.getValue();
if (!schematic.containsKey("Blocks")) {
return false; 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) { } catch (IOException e) {
return false; return false;
} }
return true;
} }
}, },
SPONGE_SCHEMATIC("sponge", "schem") { SPONGE_SCHEMATIC("sponge", "schem") {
@ -103,26 +103,23 @@ public enum BuiltInClipboardFormat implements ClipboardFormat {
@Override @Override
public boolean isFormat(File file) { public boolean isFormat(File file) {
try (DataInputStream str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))) { try (NBTInputStream str = new NBTInputStream(new GZIPInputStream(new FileInputStream(file)))) {
if ((str.readByte() & 0xFF) != NBTConstants.TYPE_COMPOUND) { NamedTag rootTag = str.readNamedTag();
if (!rootTag.getName().equals("Schematic")) {
return false; return false;
} }
byte[] nameBytes = new byte[str.readShort() & 0xFFFF]; CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
str.readFully(nameBytes);
String name = new String(nameBytes, NBTConstants.CHARSET); // Check
if (!name.equals("Schematic")) { Map<String, Tag> schematic = schematicTag.getValue();
if (!schematic.containsKey("Version")) {
return false; 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) { } catch (IOException e) {
return false; return false;
} }
return true;
} }
}; };

View File

@ -119,7 +119,7 @@ public class SpongeSchematicReader extends NBTSchematicReader {
int originZ = requireTag(metadata, "WEOriginZ", IntTag.class).getValue(); int originZ = requireTag(metadata, "WEOriginZ", IntTag.class).getValue();
Vector min = new Vector(originX, originY, originZ); Vector min = new Vector(originX, originY, originZ);
origin = min.subtract(offset); 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 { } else {
origin = Vector.ZERO.subtract(offset); origin = Vector.ZERO.subtract(offset);
region = new CuboidRegion(origin, origin.add(width, height, length).subtract(Vector.ONE)); region = new CuboidRegion(origin, origin.add(width, height, length).subtract(Vector.ONE));
@ -134,6 +134,9 @@ public class SpongeSchematicReader extends NBTSchematicReader {
Map<Integer, BlockState> palette = new HashMap<>(); Map<Integer, BlockState> palette = new HashMap<>();
ParserContext parserContext = new ParserContext(); ParserContext parserContext = new ParserContext();
parserContext.setRestricted(false);
parserContext.setTryLegacy(false);
parserContext.setPreferringWildcard(false);
for (String palettePart : paletteObject.keySet()) { for (String palettePart : paletteObject.keySet()) {
int id = requireTag(paletteObject, palettePart, IntTag.class).getValue(); int id = requireTag(paletteObject, palettePart, IntTag.class).getValue();
@ -186,9 +189,9 @@ public class SpongeSchematicReader extends NBTSchematicReader {
i++; i++;
} }
// index = (y * length + z) * width + x // index = (y * length + z) * width + x
int y = index / (width * length); int y = origin.getBlockY() + index / (width * length);
int z = (index % (width * length)) / width; int z = origin.getBlockZ() + (index % (width * length)) / width;
int x = (index % (width * length)) % width; int x = origin.getBlockX() + (index % (width * length)) % width;
BlockState state = palette.get(value); BlockState state = palette.get(value);
BlockVector pt = new BlockVector(x, y, z); BlockVector pt = new BlockVector(x, y, z);
try { try {