@SuppressWarnings({"deprecation"}) @Override public MaterialAndData getBlock(Vector v) { int x = v.getBlockX() + center.getBlockX(); int y = v.getBlockY() + center.getBlockY(); int z = v.getBlockZ() + center.getBlockZ(); try { if (x < 0 || x >= size.getBlockZ() || y < 0 || y >= size.getBlockY() || z < 0 || z >= size.getBlockZ()) { return null; } com.sk89q.worldedit.Vector vector = new com.sk89q.worldedit.Vector(x, y, z); BaseBlock baseBlock = weSchematic.getBlock(vector); Material material = Material.getMaterial(baseBlock.getId()); int materialData = baseBlock.getData(); MaterialAndData blockData = new MaterialAndData(material, (byte) materialData); // Note.. we don't actually get a SignBlock here, for some reason. // May have something to do with loading schematics not actually supporting sign // text, it doesn't work with //schematic and //paste, either. // It looks like //paste works in a dev build of WE, but it still doesn't give me the blocks // Looking at WE's code, it seems like the part that's needed is commented out... ?? if (material == Material.SIGN_POST || material == Material.WALL_SIGN) { try { if (baseBlock.hasNbtData()) { SignBlock signBlock = new SignBlock(material.getId(), materialData); CompoundTag nbtData = baseBlock.getNbtData(); signBlock.setNbtData(nbtData); blockData.setSignLines(signBlock.getText()); } } catch (Throwable ex) { ex.printStackTrace(); } } else if (material == Material.COMMAND) { try { if (baseBlock.hasNbtData()) { CompoundTag nbtRoot = baseBlock.getNbtData(); Map<String, Tag> rootValues = nbtRoot.getValue(); if (rootValues.containsKey("Command")) { Object commandValue = rootValues.get("Command").getValue(); blockData.setCommandLine((String) commandValue); } if (rootValues.containsKey("CustomName")) { Object nameValue = rootValues.get("CustomName").getValue(); blockData.setCustomName((String) nameValue); } } } catch (Throwable ex) { ex.printStackTrace(); } } else if (material == Material.CHEST) { try { if (baseBlock.hasNbtData()) { ChestBlock chestBlock = new ChestBlock(materialData); CompoundTag nbtRoot = baseBlock.getNbtData(); chestBlock.setNbtData(nbtRoot); BaseItemStack[] items = chestBlock.getItems(); if (items != null && items.length > 0) { ItemStack[] contents = new ItemStack[items.length]; for (int i = 0; i < items.length; i++) { if (items[i] != null) { Material itemMaterial = Material.getMaterial(items[i].getType()); // Bukkit.getLogger().info("Item from chest: " + itemMaterial + " at " + i + " / " // + contents.length); short itemData = items[i].getData(); int itemAmount = items[i].getAmount(); ItemStack newStack = new ItemStack(itemMaterial, itemAmount, itemData); Map<Integer, Integer> enchantments = items[i].getEnchantments(); if (enchantments != null && enchantments.size() > 0) { for (Entry<Integer, Integer> enchantment : enchantments.entrySet()) { try { Enchantment enchantmentType = Enchantment.getById(enchantment.getKey()); newStack.addEnchantment(enchantmentType, enchantment.getValue()); } catch (Exception ex) { // This seems to happen a lot .. like on potions especially. ex.printStackTrace(); } } } contents[i] = newStack; } } blockData.setInventoryContents(contents); } } } catch (Throwable ex) { ex.printStackTrace(); } } return blockData; } catch (ArrayIndexOutOfBoundsException ex) { // TODO: Figure out why this still happens, even with the size check } catch (Throwable ex) { ex.printStackTrace(); } return null; }
@Override public void save(CuboidClipboard clipboard, File file) throws IOException, DataException { int width = clipboard.getWidth(); int height = clipboard.getHeight(); int length = clipboard.getLength(); if (width > MAX_SIZE) { throw new DataException("Width of region too large for a .schematic"); } if (height > MAX_SIZE) { throw new DataException("Height of region too large for a .schematic"); } if (length > MAX_SIZE) { throw new DataException("Length of region too large for a .schematic"); } HashMap<String, Tag> schematic = new HashMap<String, Tag>(); schematic.put("Width", new ShortTag((short) width)); schematic.put("Length", new ShortTag((short) length)); schematic.put("Height", new ShortTag((short) height)); schematic.put("Materials", new StringTag("Alpha")); schematic.put("WEOriginX", new IntTag(clipboard.getOrigin().getBlockX())); schematic.put("WEOriginY", new IntTag(clipboard.getOrigin().getBlockY())); schematic.put("WEOriginZ", new IntTag(clipboard.getOrigin().getBlockZ())); schematic.put("WEOffsetX", new IntTag(clipboard.getOffset().getBlockX())); schematic.put("WEOffsetY", new IntTag(clipboard.getOffset().getBlockY())); schematic.put("WEOffsetZ", new IntTag(clipboard.getOffset().getBlockZ())); // Copy byte[] blocks = new byte[width * height * length]; byte[] addBlocks = null; byte[] blockData = new byte[width * height * length]; ArrayList<Tag> tileEntities = new ArrayList<Tag>(); 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; BaseBlock block = clipboard.getPoint(new BlockVector(x, y, z)); // Save 4096 IDs in an AddBlocks section if (block.getType() > 255) { if (addBlocks == null) { // Lazily create section addBlocks = new byte[(blocks.length >> 1) + 1]; } addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? addBlocks[index >> 1] & 0xF0 | (block.getType() >> 8) & 0xF : addBlocks[index >> 1] & 0xF | ((block.getType() >> 8) & 0xF) << 4); } blocks[index] = (byte) block.getType(); blockData[index] = (byte) block.getData(); // Get the list of key/values from the block CompoundTag rawTag = block.getNbtData(); if (rawTag != null) { Map<String, Tag> values = new HashMap<String, Tag>(); for (Entry<String, Tag> entry : rawTag.getValue().entrySet()) { values.put(entry.getKey(), entry.getValue()); } values.put("id", new StringTag(block.getNbtId())); values.put("x", new IntTag(x)); values.put("y", new IntTag(y)); values.put("z", new IntTag(z)); CompoundTag tileEntityTag = new CompoundTag(values); tileEntities.add(tileEntityTag); } } } } schematic.put("Blocks", new ByteArrayTag(blocks)); schematic.put("Data", new ByteArrayTag(blockData)); schematic.put("Entities", new ListTag(CompoundTag.class, new ArrayList<Tag>())); schematic.put("TileEntities", new ListTag(CompoundTag.class, tileEntities)); if (addBlocks != null) { schematic.put("AddBlocks", new ByteArrayTag(addBlocks)); } // Build and output CompoundTag schematicTag = new CompoundTag(schematic); NBTOutputStream stream = new NBTOutputStream(new GZIPOutputStream(new FileOutputStream(file))); stream.writeNamedTag("Schematic", schematicTag); stream.close(); }