diff --git a/pom.xml b/pom.xml index c7dc835b7..1d9606601 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ org.bukkit bukkit - 1.2.3-R0.3-SNAPSHOT + 1.2.4-R1.0 diff --git a/src/main/java/com/sk89q/jnbt/ListTag.java b/src/main/java/com/sk89q/jnbt/ListTag.java index df701ea5f..f6f0e9580 100644 --- a/src/main/java/com/sk89q/jnbt/ListTag.java +++ b/src/main/java/com/sk89q/jnbt/ListTag.java @@ -2,24 +2,24 @@ package com.sk89q.jnbt; /* * JNBT License - * + * * Copyright (c) 2010 Graham Edgecombe * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * * Neither the name of the JNBT team nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -30,7 +30,7 @@ package com.sk89q.jnbt; * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * POSSIBILITY OF SUCH DAMAGE. */ import java.util.Collections; @@ -40,9 +40,9 @@ import com.sk89q.jnbt.Tag; /** * The TAG_List tag. - * + * * @author Graham Edgecombe - * + * */ public final class ListTag extends Tag { @@ -58,7 +58,7 @@ public final class ListTag extends Tag { /** * Creates the tag. - * + * * @param name * The name. * @param type @@ -66,7 +66,7 @@ public final class ListTag extends Tag { * @param value * The value. */ - public ListTag(String name, Class type, List value) { + public ListTag(String name, Class type, List value) { super(name); this.type = type; this.value = Collections.unmodifiableList(value); @@ -74,7 +74,7 @@ public final class ListTag extends Tag { /** * Gets the type of item in this list. - * + * * @return The type of item in this list. */ public Class getType() { diff --git a/src/main/java/com/sk89q/jnbt/NBTUtils.java b/src/main/java/com/sk89q/jnbt/NBTUtils.java index c2c8fc9aa..21057a1ec 100644 --- a/src/main/java/com/sk89q/jnbt/NBTUtils.java +++ b/src/main/java/com/sk89q/jnbt/NBTUtils.java @@ -19,24 +19,24 @@ import com.sk89q.worldedit.data.InvalidFormatException; /* * JNBT License - * + * * Copyright (c) 2010 Graham Edgecombe * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * * Neither the name of the JNBT team nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -47,20 +47,20 @@ import com.sk89q.worldedit.data.InvalidFormatException; * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * POSSIBILITY OF SUCH DAMAGE. */ /** * A class which contains NBT-related utility methods. - * + * * @author Graham Edgecombe - * + * */ public final class NBTUtils { /** * Gets the type name of a tag. - * + * * @param clazz * The tag class. * @return The type name. @@ -98,7 +98,7 @@ public final class NBTUtils { /** * Gets the type code of a tag class. - * + * * @param clazz * The tag class. * @return The type code. @@ -129,7 +129,7 @@ public final class NBTUtils { } else if (clazz.equals(StringTag.class)) { return NBTConstants.TYPE_STRING; } else if (clazz.equals(IntArrayTag.class)) { - return NBTConstants.TYPE_INT_ARRAY; + return NBTConstants.TYPE_INT_ARRAY; } else { throw new IllegalArgumentException("Invalid tag classs (" + clazz.getName() + ")."); @@ -138,7 +138,7 @@ public final class NBTUtils { /** * Gets the class of a type of tag. - * + * * @param type * The type. * @return The class. @@ -183,7 +183,7 @@ public final class NBTUtils { private NBTUtils() { } - + /** * Get child tag of a NBT structure. * diff --git a/src/main/java/com/sk89q/worldedit/BlockVector.java b/src/main/java/com/sk89q/worldedit/BlockVector.java index c2ebe6ada..0854be1df 100644 --- a/src/main/java/com/sk89q/worldedit/BlockVector.java +++ b/src/main/java/com/sk89q/worldedit/BlockVector.java @@ -93,9 +93,9 @@ public class BlockVector extends Vector { */ @Override public int hashCode() { - return (Integer.valueOf((int) x).hashCode() << 19) ^ - (Integer.valueOf((int) y).hashCode() << 12) ^ - Integer.valueOf((int) z).hashCode(); + return ((int) x << 19) ^ + ((int) y << 12) ^ + (int) z; } @Override diff --git a/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java b/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java index 4ae3f00b2..c7bfd2de6 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java @@ -42,7 +42,7 @@ public class BaseBlock { * @param type */ public BaseBlock(int type) { - this.type = (short) type; + this(type, 0); } /** @@ -166,7 +166,7 @@ public class BaseBlock { } /** - * + * * * @param iter * @return diff --git a/src/main/java/com/sk89q/worldedit/blocks/ChestBlock.java b/src/main/java/com/sk89q/worldedit/blocks/ChestBlock.java index 020233d90..e21bd822a 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/ChestBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/ChestBlock.java @@ -25,25 +25,19 @@ import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; -import java.util.Map.Entry; /** * Represents chests. * * @author sk89q */ -public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerBlock { - /** - * Store the list of items. - */ - private BaseItemStack[] items; +public class ChestBlock extends ContainerBlock { /** * Construct the chest block. */ public ChestBlock() { - super(BlockID.CHEST); - items = new BaseItemStack[27]; + super(BlockID.CHEST, 27); } /** @@ -52,8 +46,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB * @param data */ public ChestBlock(int data) { - super(BlockID.CHEST, data); - items = new BaseItemStack[27]; + super(BlockID.CHEST, data, 27); } /** @@ -63,24 +56,8 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB * @param items */ public ChestBlock(int data, BaseItemStack[] items) { - super(BlockID.CHEST, data); - this.items = items; - } - - /** - * Get the list of items. - * - * @return - */ - public BaseItemStack[] getItems() { - return items; - } - - /** - * Set the list of items. - */ - public void setItems(BaseItemStack[] items) { - this.items = items; + super(BlockID.CHEST, data, 27); + setItems(items); } /** @@ -100,36 +77,8 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB */ public Map toTileEntityNBT() throws DataException { - List itemsList = new ArrayList(); - for (int i = 0; i < items.length; ++i) { - BaseItemStack item = items[i]; - if (item != null) { - Map data = new HashMap(); - CompoundTag itemTag = new CompoundTag("Items", data); - data.put("id", new ShortTag("id", (short) item.getType())); - data.put("Damage", new ShortTag("Damage", item.getDamage())); - data.put("Count", new ByteTag("Count", (byte) item.getAmount())); - data.put("Slot", new ByteTag("Slot", (byte) i)); - if(item.getEnchantments().size() > 0) { - Map ench = new HashMap(); - CompoundTag compound = new CompoundTag("tag", ench); - List list = new ArrayList(); - ListTag enchlist = new ListTag("ench", CompoundTag.class, list); - for(Entry entry : item.getEnchantments().entrySet()) { - Map enchantment = new HashMap(); - CompoundTag enchantcompound = new CompoundTag(null, enchantment); - enchantment.put("id", new ShortTag("id", entry.getKey().shortValue())); - enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue())); - list.add(enchantcompound); - } - ench.put("ench", enchlist); - data.put("tag", compound); - } - itemsList.add(itemTag); - } - } Map values = new HashMap(); - values.put("Items", new ListTag("Items", CompoundTag.class, itemsList)); + values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems()))); return values; } @@ -150,39 +99,15 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB throw new DataException("'Chest' tile entity expected"); } - ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class); - BaseItemStack[] newItems = new BaseItemStack[27]; - - for (Tag tag : items.getValue()) { + List items = new ArrayList(); + for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) { if (!(tag instanceof CompoundTag)) { throw new DataException("CompoundTag expected as child tag of Chest's Items"); } - CompoundTag item = (CompoundTag) tag; - Map itemValues = item.getValue(); - - short id = NBTUtils.getChildTag(itemValues, "id", ShortTag.class).getValue(); - short damage = NBTUtils.getChildTag(itemValues, "Damage", ShortTag.class).getValue(); - byte count = NBTUtils.getChildTag(itemValues, "Count", ByteTag.class).getValue(); - byte slot = NBTUtils.getChildTag(itemValues, "Slot", ByteTag.class).getValue(); - - if (slot >= 0 && slot <= 26) { - BaseItemStack itemstack = new BaseItemStack(id, count, damage); - - if(itemValues.containsKey("tag")) { - ListTag ench = (ListTag) NBTUtils.getChildTag(itemValues, "tag", CompoundTag.class).getValue().get("ench"); - for(Tag e : ench.getValue()) { - Map vars = ((CompoundTag) e).getValue(); - short enchid = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue(); - short enchlvl = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue(); - itemstack.getEnchantments().put((int) enchid, (int)enchlvl); - } - } - - newItems[slot] = itemstack; - } + items.add((CompoundTag) tag); } - this.items = newItems; + setItems(deserializeInventory(items)); } } diff --git a/src/main/java/com/sk89q/worldedit/blocks/ContainerBlock.java b/src/main/java/com/sk89q/worldedit/blocks/ContainerBlock.java index a6a638d15..394ff27b0 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/ContainerBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/ContainerBlock.java @@ -19,23 +19,119 @@ package com.sk89q.worldedit.blocks; +import com.sk89q.jnbt.ByteTag; +import com.sk89q.jnbt.CompoundTag; +import com.sk89q.jnbt.ListTag; +import com.sk89q.jnbt.NBTUtils; +import com.sk89q.jnbt.ShortTag; +import com.sk89q.jnbt.Tag; +import com.sk89q.worldedit.data.DataException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * Represents a block that stores items. - * + * * @author sk89q */ -public interface ContainerBlock { +public abstract class ContainerBlock extends BaseBlock implements TileEntityBlock { + private BaseItemStack[] items; + + public ContainerBlock(int type, int inventorySize) { + super(type); + this.items = new BaseItemStack[inventorySize]; + } + + public ContainerBlock(int type, int data, int inventorySize) { + super(type, data); + this.items = new BaseItemStack[inventorySize]; + } + + /** * Get the list of items. * * @return */ - public BaseItemStack[] getItems(); + public BaseItemStack[] getItems() { + return this.items; + } /** * Set the list of items. - * - * @param items + * + * @param items */ - public void setItems(BaseItemStack[] items); + public void setItems(BaseItemStack[] items) { + this.items = items; + } + + public Map serializeItem(BaseItemStack item) { + Map data = new HashMap(); + data.put("id", new ShortTag("id", (short) item.getType())); + data.put("Damage", new ShortTag("Damage", item.getDamage())); + data.put("Count", new ByteTag("Count", (byte) item.getAmount())); + if (item.getEnchantments().size() > 0) { + List enchantmentList = new ArrayList(); + for(Map.Entry entry : item.getEnchantments().entrySet()) { + Map enchantment = new HashMap(); + enchantment.put("id", new ShortTag("id", entry.getKey().shortValue())); + enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue())); + enchantmentList.add(new CompoundTag(null, enchantment)); + } + + Map auxData = new HashMap(); + auxData.put("ench", new ListTag("ench", CompoundTag.class, enchantmentList)); + data.put("tag", new CompoundTag("tag", auxData)); + } + return data; + } + + public BaseItemStack deserializeItem(Map data) throws DataException { + short id = NBTUtils.getChildTag(data, "id", ShortTag.class).getValue(); + short damage = NBTUtils.getChildTag(data, "Damage", ShortTag.class).getValue(); + byte count = NBTUtils.getChildTag(data, "Count", ByteTag.class).getValue(); + + BaseItemStack stack = new BaseItemStack(id, count, damage); + + if (data.containsKey("tag")) { + Map auxData = NBTUtils.getChildTag(data, "tag", CompoundTag.class).getValue(); + ListTag ench = (ListTag)auxData.get("ench"); + for(Tag e : ench.getValue()) { + Map vars = ((CompoundTag) e).getValue(); + short enchId = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue(); + short enchLevel = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue(); + stack.getEnchantments().put((int) enchId, (int) enchLevel); + } + } + return stack; + } + + public BaseItemStack[] deserializeInventory(List items) throws DataException { + BaseItemStack[] stacks = new BaseItemStack[items.size()]; + for (CompoundTag tag : items) { + Map item = tag.getValue(); + BaseItemStack stack = deserializeItem(item); + byte slot = NBTUtils.getChildTag(item, "Slot", ByteTag.class).getValue(); + if (slot >= 0 && slot < stacks.length) { + stacks[slot] = stack; + } + } + return stacks; + } + + public List serializeInventory(BaseItemStack[] items) { + List tags = new ArrayList(); + for (int i = 0; i < items.length; ++i) { + if (items[i] != null) { + Map tagData = serializeItem(items[i]); + tagData.put("Slot", new ByteTag("Slot", (byte) i)); + tags.add(new CompoundTag("", tagData)); + } + } + return tags; + } } diff --git a/src/main/java/com/sk89q/worldedit/blocks/DispenserBlock.java b/src/main/java/com/sk89q/worldedit/blocks/DispenserBlock.java index 7b7cc73b2..f7e9ba692 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/DispenserBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/DispenserBlock.java @@ -32,18 +32,13 @@ import java.util.Map.Entry; * * @author sk89q */ -public class DispenserBlock extends BaseBlock implements TileEntityBlock, ContainerBlock { - /** - * Store the list of items. - */ - private BaseItemStack[] items; +public class DispenserBlock extends ContainerBlock { /** * Construct the dispenser block. */ public DispenserBlock() { - super(BlockID.DISPENSER); - items = new BaseItemStack[9]; + super(BlockID.DISPENSER, 9); } /** @@ -52,8 +47,7 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai * @param data */ public DispenserBlock(int data) { - super(BlockID.DISPENSER, data); - items = new BaseItemStack[9]; + super(BlockID.DISPENSER, data, 9); } /** @@ -63,24 +57,8 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai * @param items */ public DispenserBlock(int data, BaseItemStack[] items) { - super(BlockID.DISPENSER, data); - this.items = items; - } - - /** - * Get the list of items. - * - * @return - */ - public BaseItemStack[] getItems() { - return items; - } - - /** - * Set the list of items. - */ - public void setItems(BaseItemStack[] items) { - this.items = items; + super(BlockID.DISPENSER, data, 9); + this.setItems(items); } /** @@ -100,36 +78,8 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai */ public Map toTileEntityNBT() throws DataException { - List itemsList = new ArrayList(); - for (int i = 0; i < items.length; ++i) { - BaseItemStack item = items[i]; - if (item != null) { - Map data = new HashMap(); - CompoundTag itemTag = new CompoundTag("Items", data); - data.put("id", new ShortTag("id", (short) item.getType())); - data.put("Damage", new ShortTag("Damage", item.getDamage())); - data.put("Count", new ByteTag("Count", (byte) item.getAmount())); - data.put("Slot", new ByteTag("Slot", (byte) i)); - if(item.getEnchantments().size() > 0) { - Map ench = new HashMap(); - CompoundTag compound = new CompoundTag("tag", ench); - List list = new ArrayList(); - ListTag enchlist = new ListTag("ench", CompoundTag.class, list); - for(Entry entry : item.getEnchantments().entrySet()) { - Map enchantment = new HashMap(); - CompoundTag enchantcompound = new CompoundTag(null, enchantment); - enchantment.put("id", new ShortTag("id", entry.getKey().shortValue())); - enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue())); - list.add(enchantcompound); - } - ench.put("ench", enchlist); - data.put("tag", compound); - } - itemsList.add(itemTag); - } - } Map values = new HashMap(); - values.put("Items", new ListTag("Items", CompoundTag.class, itemsList)); + values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems()))); return values; } @@ -150,39 +100,15 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai throw new DataException("'Trap' tile entity expected"); } - ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class); - BaseItemStack[] newItems = new BaseItemStack[9]; - - for (Tag tag : items.getValue()) { + List items = new ArrayList(); + for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) { if (!(tag instanceof CompoundTag)) { throw new DataException("CompoundTag expected as child tag of Trap Items"); } - CompoundTag item = (CompoundTag) tag; - Map itemValues = item.getValue(); - - short id = NBTUtils.getChildTag(itemValues, "id", ShortTag.class).getValue(); - short damage = NBTUtils.getChildTag(itemValues, "Damage", ShortTag.class).getValue(); - byte count = NBTUtils.getChildTag(itemValues, "Count", ByteTag.class).getValue(); - byte slot = NBTUtils.getChildTag(itemValues, "Slot", ByteTag.class).getValue(); - - if (slot >= 0 && slot <= 8) { - BaseItemStack itemstack = new BaseItemStack(id, count, damage); - - if(itemValues.containsKey("tag")) { - ListTag ench = (ListTag) NBTUtils.getChildTag(itemValues, "tag", CompoundTag.class).getValue().get("ench"); - for(Tag e : ench.getValue()) { - Map vars = ((CompoundTag) e).getValue(); - short enchid = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue(); - short enchlvl = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue(); - itemstack.getEnchantments().put((int) enchid, (int)enchlvl); - } - } - - newItems[slot] = itemstack; - } + items.add((CompoundTag) tag); } - this.items = newItems; + setItems(deserializeInventory(items)); } } diff --git a/src/main/java/com/sk89q/worldedit/blocks/FurnaceBlock.java b/src/main/java/com/sk89q/worldedit/blocks/FurnaceBlock.java index e62d329e8..a65c875cb 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/FurnaceBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/FurnaceBlock.java @@ -21,22 +21,18 @@ package com.sk89q.worldedit.blocks; import com.sk89q.jnbt.*; import com.sk89q.worldedit.data.*; + +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.HashMap; -import java.util.List; -import java.util.ArrayList; -import java.util.Map.Entry; /** * Represents furnaces. * * @author sk89q */ -public class FurnaceBlock extends BaseBlock implements TileEntityBlock, ContainerBlock { - /** - * Store the list of items. - */ - private BaseItemStack[] items; +public class FurnaceBlock extends ContainerBlock { /** * Fuel time. @@ -54,8 +50,7 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe * @param type */ public FurnaceBlock(int type) { - super(type); - items = new BaseItemStack[2]; + super(type, 2); } /** @@ -65,8 +60,7 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe * @param data */ public FurnaceBlock(int type, int data) { - super(type, data); - items = new BaseItemStack[2]; + super(type, data, 2); } /** @@ -77,24 +71,8 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe * @param items */ public FurnaceBlock(int type, int data, BaseItemStack[] items) { - super(type, data); - this.items = items; - } - - /** - * Get the list of items. - * - * @return - */ - public BaseItemStack[] getItems() { - return items; - } - - /** - * Set the list of items. - */ - public void setItems(BaseItemStack[] items) { - this.items = items; + super(type, data, 2); + setItems(items); } /** @@ -142,36 +120,8 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe */ public Map toTileEntityNBT() throws DataException { - List itemsList = new ArrayList(); - for (int i = 0; i < items.length; ++i) { - BaseItemStack item = items[i]; - if (item != null) { - Map data = new HashMap(); - CompoundTag itemTag = new CompoundTag("Items", data); - data.put("id", new ShortTag("id", (short) item.getType())); - data.put("Damage", new ShortTag("Damage", item.getDamage())); - data.put("Count", new ByteTag("Count", (byte) item.getAmount())); - data.put("Slot", new ByteTag("Slot", (byte) i)); - if(item.getEnchantments().size() > 0) { - Map ench = new HashMap(); - CompoundTag compound = new CompoundTag("tag", ench); - List list = new ArrayList(); - ListTag enchlist = new ListTag("ench", CompoundTag.class, list); - for(Entry entry : item.getEnchantments().entrySet()) { - Map enchantment = new HashMap(); - CompoundTag enchantcompound = new CompoundTag(null, enchantment); - enchantment.put("id", new ShortTag("id", entry.getKey().shortValue())); - enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue())); - list.add(enchantcompound); - } - ench.put("ench", enchlist); - data.put("tag", compound); - } - itemsList.add(itemTag); - } - } Map values = new HashMap(); - values.put("Items", new ListTag("Items", CompoundTag.class, itemsList)); + values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems()))); values.put("BurnTime", new ShortTag("BurnTime", burnTime)); values.put("CookTime", new ShortTag("CookTime", cookTime)); return values; @@ -194,40 +144,17 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe throw new DataException("'Furnace' tile entity expected"); } - ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class); - BaseItemStack[] newItems = new BaseItemStack[3]; + ListTag items = NBTUtils.getChildTag(values, "Items", ListTag.class); + + List compound = new ArrayList(); for (Tag tag : items.getValue()) { if (!(tag instanceof CompoundTag)) { - throw new DataException("CompoundTag expected as child tag of Trap Items"); - } - - CompoundTag item = (CompoundTag) tag; - Map itemValues = item.getValue(); - - short id = NBTUtils.getChildTag(itemValues, "id", ShortTag.class).getValue(); - short damage = NBTUtils.getChildTag(itemValues, "Damage", ShortTag.class).getValue(); - byte count = NBTUtils.getChildTag(itemValues, "Count", ByteTag.class).getValue(); - byte slot = NBTUtils.getChildTag(itemValues, "Slot", ByteTag.class).getValue(); - - if (slot >= 0 && slot <= 2) { - BaseItemStack itemstack = new BaseItemStack(id, count, damage); - - if(itemValues.containsKey("tag")) { - ListTag ench = (ListTag) NBTUtils.getChildTag(itemValues, "tag", CompoundTag.class).getValue().get("ench"); - for(Tag e : ench.getValue()) { - Map vars = ((CompoundTag) e).getValue(); - short enchid = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue(); - short enchlvl = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue(); - itemstack.getEnchantments().put((int) enchid, (int)enchlvl); - } - } - - newItems[slot] = itemstack; + throw new DataException("CompoundTag expected as child tag of Furnace Items"); } + compound.add((CompoundTag) tag); } - - this.items = newItems; + setItems(deserializeInventory(compound)); t = values.get("BurnTime"); if (t instanceof ShortTag) { diff --git a/src/main/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java b/src/main/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java index 2430502bc..92b08315e 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java @@ -151,8 +151,8 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock { throw new DataException("'MobSpawner' tile entity expected"); } - StringTag mobTypeTag = (StringTag) NBTUtils.getChildTag(values, "EntityId", StringTag.class); - ShortTag delayTag = (ShortTag) NBTUtils.getChildTag(values, "Delay", ShortTag.class); + StringTag mobTypeTag = NBTUtils.getChildTag(values, "EntityId", StringTag.class); + ShortTag delayTag = NBTUtils.getChildTag(values, "Delay", ShortTag.class); this.mobType = mobTypeTag.getValue(); this.delay = delayTag.getValue(); diff --git a/src/main/java/com/sk89q/worldedit/blocks/NoteBlock.java b/src/main/java/com/sk89q/worldedit/blocks/NoteBlock.java index 135b9d2c8..5a382ed2d 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/NoteBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/NoteBlock.java @@ -55,7 +55,7 @@ public class NoteBlock extends BaseBlock implements TileEntityBlock { /** * Construct the note block. * - * @param data + * @param data * @param note */ public NoteBlock(int data, byte note) { diff --git a/src/main/java/com/sk89q/worldedit/blocks/TileEntityBlock.java b/src/main/java/com/sk89q/worldedit/blocks/TileEntityBlock.java index 436afad79..eae44de23 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/TileEntityBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/TileEntityBlock.java @@ -21,6 +21,7 @@ package com.sk89q.worldedit.blocks; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.data.*; + import java.util.Map; /** @@ -29,12 +30,13 @@ import java.util.Map; * @author sk89q */ public interface TileEntityBlock { + /** * Return the name of the title entity ID. * * @return tile entity ID */ - public String getTileEntityID(); + public abstract String getTileEntityID(); /** * Store additional tile entity data. @@ -42,7 +44,7 @@ public interface TileEntityBlock { * @return map of values * @throws DataException When invalid data is encountered */ - public Map toTileEntityNBT() + public abstract Map toTileEntityNBT() throws DataException; /** @@ -51,6 +53,8 @@ public interface TileEntityBlock { * @param values map of data * @throws DataException When invalid data is encountered */ - public void fromTileEntityNBT(Map values) + public abstract void fromTileEntityNBT(Map values) throws DataException; + + } diff --git a/src/main/java/com/sk89q/worldedit/data/ChunkStore.java b/src/main/java/com/sk89q/worldedit/data/ChunkStore.java index e4ca9e5f2..d977f7516 100644 --- a/src/main/java/com/sk89q/worldedit/data/ChunkStore.java +++ b/src/main/java/com/sk89q/worldedit/data/ChunkStore.java @@ -31,6 +31,11 @@ import com.sk89q.worldedit.*; * @author sk89q */ public abstract class ChunkStore { + /** + * >> to chunk + * << from chunk + */ + public static final int CHUNK_SHIFTS = 4; /** * Convert a position to a chunk. * @@ -66,7 +71,7 @@ public abstract class ChunkStore { */ public Chunk getChunk(Vector2D pos, LocalWorld world) throws DataException, IOException { - + CompoundTag tag = getChunkTag(pos, world); Map tags = tag.getValue(); if(tags.containsKey("Sections")) {