From 2846363a2c4b1102841fcb8a3ab1a30a8f78b76a Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 27 Dec 2018 20:06:02 +0000 Subject: [PATCH] Revert "Use our own schematic readers in only create one input stream, and allow for future customisation of schematics if required/wanted." This reverts commit 3521b8aa22624dcd725175243c9105dfc034d7f2. --- .../schematic/MCEditSchematicReader.java | 278 ------------------ .../schematic/SpongeSchematicReader.java | 174 ----------- .../plot/util/SchematicHandler.java | 45 +-- 3 files changed, 4 insertions(+), 493 deletions(-) delete mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/MCEditSchematicReader.java delete mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/SpongeSchematicReader.java diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/MCEditSchematicReader.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/MCEditSchematicReader.java deleted file mode 100644 index 15ea68ae1..000000000 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/MCEditSchematicReader.java +++ /dev/null @@ -1,278 +0,0 @@ -package com.github.intellectualsites.plotsquared.plot.object.schematic; - -import com.github.intellectualsites.plotsquared.plot.PlotSquared; -import com.sk89q.jnbt.*; -import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.entity.BaseEntity; -import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; -import com.sk89q.worldedit.extent.clipboard.Clipboard; -import com.sk89q.worldedit.extent.clipboard.io.NBTSchematicReader; -import com.sk89q.worldedit.extent.clipboard.io.legacycompat.NBTCompatibilityHandler; -import com.sk89q.worldedit.extent.clipboard.io.legacycompat.SignCompatibilityHandler; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.regions.CuboidRegion; -import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.util.Location; -import com.sk89q.worldedit.world.block.BlockState; -import com.sk89q.worldedit.world.entity.EntityType; -import com.sk89q.worldedit.world.entity.EntityTypes; -import com.sk89q.worldedit.world.registry.LegacyMapper; -import com.sk89q.worldedit.world.storage.NBTConversions; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static com.google.common.base.Preconditions.checkNotNull; - -public class MCEditSchematicReader extends NBTSchematicReader { - - private static final List COMPATIBILITY_HANDLERS = new ArrayList<>(); - - static { - COMPATIBILITY_HANDLERS.add(new SignCompatibilityHandler()); - // TODO Add a handler for skulls, flower pots, note blocks, etc. - } - - private final NBTInputStream inputStream; - - public MCEditSchematicReader(NBTInputStream inputStream) { - checkNotNull(inputStream); - this.inputStream = inputStream; - } - - @Override public Clipboard read() throws IOException { - // Schematic tag - NamedTag rootTag = inputStream.readNamedTag(); - if (!rootTag.getName().equals("Schematic")) { - throw new IOException("Tag 'Schematic' does not exist or is not first"); - } - CompoundTag schematicTag = (CompoundTag) rootTag.getTag(); - - // Check - Map schematic = schematicTag.getValue(); - if (!schematic.containsKey("Blocks")) { - throw new IOException("Schematic file is missing a 'Blocks' tag"); - } - - // Check type of Schematic - String materials = requireTag(schematic, "Materials", StringTag.class).getValue(); - if (!materials.equals("Alpha")) { - throw new IOException("Schematic file is not an Alpha schematic"); - } - - // ==================================================================== - // Metadata - // ==================================================================== - - BlockVector3 origin; - Region region; - - // Get information - short width = requireTag(schematic, "Width", ShortTag.class).getValue(); - short height = requireTag(schematic, "Height", ShortTag.class).getValue(); - short length = requireTag(schematic, "Length", ShortTag.class).getValue(); - - origin = BlockVector3.at(0, 0, 0); - region = - new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE)); - - // ==================================================================== - // Blocks - // ==================================================================== - - // Get blocks - byte[] blockId = requireTag(schematic, "Blocks", ByteArrayTag.class).getValue(); - byte[] blockData = requireTag(schematic, "Data", ByteArrayTag.class).getValue(); - byte[] addId = new byte[0]; - short[] blocks = new short[blockId.length]; // Have to later combine IDs - - // We support 4096 block IDs using the same method as vanilla Minecraft, where - // the highest 4 bits are stored in a separate byte array. - if (schematic.containsKey("AddBlocks")) { - addId = requireTag(schematic, "AddBlocks", ByteArrayTag.class).getValue(); - } - - // Combine the AddBlocks data with the first 8-bit block ID - for (int index = 0; index < blockId.length; index++) { - if ((index >> 1) >= addId.length) { // No corresponding AddBlocks index - blocks[index] = (short) (blockId[index] & 0xFF); - } else { - if ((index & 1) == 0) { - blocks[index] = - (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF)); - } else { - blocks[index] = - (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF)); - } - } - } - - // Need to pull out tile entities - List tileEntities = requireTag(schematic, "TileEntities", ListTag.class).getValue(); - Map> tileEntitiesMap = new HashMap<>(); - - for (Tag tag : tileEntities) { - if (!(tag instanceof CompoundTag)) - continue; - 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()); - } - - int index = y * width * length + z * width + x; - BlockState block = - LegacyMapper.getInstance().getBlockFromLegacy(blocks[index], blockData[index]); - if (block != null) { - for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) { - if (handler.isAffectedBlock(block)) { - handler.updateNBT(block, values); - } - } - } - - BlockVector3 vec = BlockVector3.at(x, y, z); - tileEntitiesMap.put(vec, values); - } - - BlockArrayClipboard clipboard = new BlockArrayClipboard(region); - clipboard.setOrigin(origin); - - // Don't log a torrent of errors - int failedBlockSets = 0; - - for (int x = 0; x < width; ++x) { - for (int y = 0; y < height; ++y) { - for (int z = 0; z < length; ++z) { - int index = y * width * length + z * width + x; - BlockVector3 pt = BlockVector3.at(x, y, z); - BlockState state = LegacyMapper.getInstance() - .getBlockFromLegacy(blocks[index], blockData[index]); - - try { - if (state != null) { - if (tileEntitiesMap.containsKey(pt)) { - clipboard.setBlock(region.getMinimumPoint().add(pt), - state.toBaseBlock(new CompoundTag(tileEntitiesMap.get(pt)))); - } else { - clipboard.setBlock(region.getMinimumPoint().add(pt), state); - } - } else { - PlotSquared.log( - "Unknown block when pasting schematic: " + blocks[index] + ":" - + blockData[index] + ". Please report this issue."); - } - } catch (WorldEditException e) { - switch (failedBlockSets) { - case 0: - PlotSquared.log("Failed to set block on a Clipboard"); - e.printStackTrace(); - break; - case 1: - PlotSquared.log( - "Failed to set block on a Clipboard (again) -- no more messages will be logged"); - e.printStackTrace(); - break; - default: - } - - failedBlockSets++; - } - } - } - } - - // ==================================================================== - // Entities - // ==================================================================== - - try { - List entityTags = requireTag(schematic, "Entities", ListTag.class).getValue(); - - for (Tag tag : entityTags) { - if (tag instanceof CompoundTag) { - CompoundTag compound = (CompoundTag) tag; - String id = convertEntityId(compound.getString("id")); - Location location = NBTConversions - .toLocation(clipboard, compound.getListTag("Pos"), - compound.getListTag("Rotation")); - - if (!id.isEmpty()) { - EntityType entityType = EntityTypes.get(id.toLowerCase()); - if (entityType != null) { - BaseEntity state = new BaseEntity(entityType, compound); - clipboard.createEntity(location, state); - } else { - PlotSquared - .log("Unknown entity when pasting schematic: " + id.toLowerCase()); - } - } - } - } - } catch (IOException ignored) { // No entities? No problem - } - - return clipboard; - } - - private String convertEntityId(String id) { - switch (id) { - case "xp_orb": - return "experience_orb"; - case "xp_bottle": - return "experience_bottle"; - case "eye_of_ender_signal": - return "eye_of_ender"; - case "ender_crystal": - return "end_crystal"; - case "fireworks_rocket": - return "firework_rocket"; - case "commandblock_minecart": - return "command_block_minecart"; - case "snowman": - return "snow_golem"; - case "villager_golem": - return "iron_golem"; - case "evocation_fangs": - return "evoker_fangs"; - case "evocation_illager": - return "evoker"; - case "vindication_illager": - return "vindicator"; - case "illusion_illager": - return "illusioner"; - } - return id; - } - - @Override public void close() throws IOException { - inputStream.close(); - } -} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/SpongeSchematicReader.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/SpongeSchematicReader.java deleted file mode 100644 index 8c7eaa4e9..000000000 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/SpongeSchematicReader.java +++ /dev/null @@ -1,174 +0,0 @@ -package com.github.intellectualsites.plotsquared.plot.object.schematic; - -import com.google.common.collect.Maps; -import com.sk89q.jnbt.*; -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.extension.input.InputParseException; -import com.sk89q.worldedit.extension.input.ParserContext; -import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; -import com.sk89q.worldedit.extent.clipboard.Clipboard; -import com.sk89q.worldedit.extent.clipboard.io.NBTSchematicReader; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.regions.CuboidRegion; -import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.world.block.BlockState; - -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import static com.google.common.base.Preconditions.checkNotNull; - -public class SpongeSchematicReader extends NBTSchematicReader { - - private final NBTInputStream inputStream; -/* private static final List COMPATIBILITY_HANDLERS = new ArrayList<>(); - - static { - // If NBT Compat handlers are needed - add them here. - }*/ - - public SpongeSchematicReader(NBTInputStream inputStream) { - checkNotNull(inputStream); - this.inputStream = inputStream; - } - - - @Override public Clipboard read() throws IOException { - NamedTag rootTag = inputStream.readNamedTag(); - if (!rootTag.getName().equals("Schematic")) { - throw new IOException("Tag 'Schematic' does not exist or is not first"); - } - CompoundTag schematicTag = (CompoundTag) rootTag.getTag(); - - // Check - Map schematic = schematicTag.getValue(); - int version = requireTag(schematic, "Version", IntTag.class).getValue(); - switch (version) { - case 1: - return readVersion1(schematic); - default: - throw new IOException("This schematic version is currently not supported"); - } - } - - private Clipboard readVersion1(Map schematic) throws IOException { - BlockVector3 origin; - Region region; - - int width = requireTag(schematic, "Width", ShortTag.class).getValue(); - int height = requireTag(schematic, "Height", ShortTag.class).getValue(); - int length = requireTag(schematic, "Length", ShortTag.class).getValue(); - - origin = BlockVector3.at(0, 0, 0); - region = - new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE)); - - int paletteMax = requireTag(schematic, "PaletteMax", IntTag.class).getValue(); - Map paletteObject = - requireTag(schematic, "Palette", CompoundTag.class).getValue(); - if (paletteObject.size() != paletteMax) { - throw new IOException("Differing given palette size to actual size"); - } - - 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(); - BlockState state; - try { - state = WorldEdit.getInstance().getBlockFactory() - .parseFromInput(palettePart, parserContext).toImmutableState(); - } catch (InputParseException e) { - throw new IOException("Invalid BlockState in schematic: " + palettePart - + ". Are you missing a mod or using a schematic made in a newer version of Minecraft?"); - } - palette.put(id, state); - } - - byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue(); - - Map> tileEntitiesMap = new HashMap<>(); - try { - List> tileEntityTags = - requireTag(schematic, "TileEntities", ListTag.class).getValue().stream() - .map(tag -> (CompoundTag) tag).map(CompoundTag::getValue) - .collect(Collectors.toList()); - - for (Map tileEntity : tileEntityTags) { - int[] pos = requireTag(tileEntity, "Pos", IntArrayTag.class).getValue(); - tileEntitiesMap.put(BlockVector3.at(pos[0], pos[1], pos[2]), tileEntity); - } - } catch (Exception e) { - throw new IOException("Failed to load Tile Entities: " + e.getMessage()); - } - - BlockArrayClipboard clipboard = new BlockArrayClipboard(region); - clipboard.setOrigin(origin); - - int index = 0; - int i = 0; - int value = 0; - int varintLength = 0; - while (i < blocks.length) { - value = 0; - varintLength = 0; - - while (true) { - value |= (blocks[i] & 127) << (varintLength++ * 7); - if (varintLength > 5) { - throw new RuntimeException("VarInt too big (probably corrupted data)"); - } - if ((blocks[i] & 128) != 128) { - i++; - break; - } - i++; - } - // index = (y * length + z) * width + x - int y = index / (width * length); - int z = (index % (width * length)) / width; - int x = (index % (width * length)) % width; - BlockState state = palette.get(value); - BlockVector3 pt = BlockVector3.at(x, y, z); - try { - if (tileEntitiesMap.containsKey(pt)) { - Map values = Maps.newHashMap(tileEntitiesMap.get(pt)); -/* for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) { - if (handler.isAffectedBlock(state)) { - handler.updateNBT(state, values); - } - }*/ - values.put("x", new IntTag(pt.getBlockX())); - values.put("y", new IntTag(pt.getBlockY())); - values.put("z", new IntTag(pt.getBlockZ())); - values.put("id", values.get("Id")); - values.remove("Id"); - values.remove("Pos"); - clipboard.setBlock(clipboard.getMinimumPoint().add(pt), - state.toBaseBlock(new CompoundTag(values))); - } else { - clipboard.setBlock(clipboard.getMinimumPoint().add(pt), state); - } - } catch (WorldEditException e) { - throw new IOException("Failed to load a block in the schematic"); - } - - index++; - } - - return clipboard; - } - - @Override public void close() throws IOException { - inputStream.close(); - } -} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java index ae7775170..f1ca31b4d 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java @@ -8,15 +8,11 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.generator.ClassicPlotWorld; import com.github.intellectualsites.plotsquared.plot.object.*; -import com.github.intellectualsites.plotsquared.plot.object.schematic.MCEditSchematicReader; import com.github.intellectualsites.plotsquared.plot.object.schematic.Schematic; -import com.github.intellectualsites.plotsquared.plot.object.schematic.SpongeSchematicReader; import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue; import com.sk89q.jnbt.*; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader; +import com.sk89q.worldedit.extent.clipboard.io.*; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.block.BaseBlock; @@ -294,10 +290,9 @@ public abstract class SchematicHandler { return null; } try { - NBTInputStream nbtInputStream = - new NBTInputStream(new GZIPInputStream(new FileInputStream(file))); - ClipboardReader reader = getReader(nbtInputStream, file); - if (reader != null) { + ClipboardFormat format = ClipboardFormats.findByFile(file); + if (format != null) { + ClipboardReader reader = format.getReader(new FileInputStream(file)); BlockArrayClipboard clip = (BlockArrayClipboard) reader.read(); return new Schematic(clip); } else { @@ -310,38 +305,6 @@ public abstract class SchematicHandler { return null; } - /** - * Get the ClipboardReader required to read the schematic - * - * @param nbtInputStream NBTInputStream (of file) - * @param file File (to attempt generic check from WE if not found) - * @return ClipboardReader if found, else null - */ - public ClipboardReader getReader(NBTInputStream nbtInputStream, File file) { - try { - NamedTag rootTag = nbtInputStream.readNamedTag(); - if (rootTag.getName().equals("Schematic")) { - CompoundTag schematicTag = (CompoundTag) rootTag.getTag(); - - // Check - Map schematic = schematicTag.getValue(); - if (schematic.containsKey("Materials")) { - return new SpongeSchematicReader(nbtInputStream); - } else if (schematic.containsKey("Version")) { - return new MCEditSchematicReader(nbtInputStream); - } - - if (ClipboardFormats.getAll().size() > 1) { - ClipboardFormat format = ClipboardFormats.findByFile(file); - return format != null ? format.getReader(new FileInputStream(file)) : null; - } - } - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - public Schematic getSchematic(URL url) { try { ReadableByteChannel rbc = Channels.newChannel(url.openStream());