diff --git a/src/EditSession.java b/src/EditSession.java index 86d99a8bc..99d5a71fe 100755 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -238,7 +238,7 @@ public class EditSession { return new SignBlock(type, data, text); // Chest } else if (type == 54) { - Map> items = + BaseItemStack[] items = ServerInterface.getChestContents(pt); return new ChestBlock(data, items); // Mob spawner diff --git a/src/ServerInterface.java b/src/ServerInterface.java index 8bbaa9583..cf0b817d4 100644 --- a/src/ServerInterface.java +++ b/src/ServerInterface.java @@ -19,8 +19,8 @@ import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.blocks.BlockType; - import java.util.logging.Logger; import java.util.Map; import java.util.HashMap; @@ -134,11 +134,11 @@ public class ServerInterface { * @param pt * @return */ - public static Map> getChestContents(Vector pt) { + public static BaseItemStack[] getChestContents(Vector pt) { ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); - Map> items; + BaseItemStack[] items; Item[] nativeItems; if (cblock instanceof Chest) { @@ -148,14 +148,14 @@ public class ServerInterface { return null; } - items = new HashMap>(); + items = new BaseItemStack[nativeItems.length]; for (byte i = 0; i < nativeItems.length; i++) { Item item = nativeItems[i]; + if (item != null) { - items.put(i, - new Countable( - new BaseItem((short)item.getItemId()), item.getAmount())); + items[i] = new BaseItemStack((short)item.getItemId(), + item.getAmount(), (short)item.getDamage()); } } @@ -170,19 +170,24 @@ public class ServerInterface { * @return */ public static boolean setChestContents(Vector pt, - Map> contents) { + BaseItemStack[] contents) { ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); if (cblock instanceof Chest) { Chest chest = (Chest)cblock; - Item[] nativeItems = new Item[contents.size()]; + Item[] nativeItems = new Item[contents.length]; - for (Map.Entry> entry : contents.entrySet()) { - nativeItems[entry.getKey()] = - new Item(entry.getValue().getID().getID(), - entry.getValue().getAmount()); + for (int i = 0; i < contents.length; i++) { + BaseItemStack item = contents[i]; + + if (item != null) { + Item nativeItem = + new Item(item.getID(), item.getAmount()); + nativeItem.setDamage(item.getDamage()); + nativeItems[i] = nativeItem; + } } setContents(chest, nativeItems); @@ -224,7 +229,8 @@ public class ServerInterface { if (contents[i] == null) { itemArray.removeItem(i); } else { - itemArray.setSlot(contents[i], i); + itemArray.setSlot(contents[i].getItemId(), + contents[i].getAmount(), contents[i].getDamage(), i); } } } diff --git a/src/com/sk89q/worldedit/blocks/BaseItemStack.java b/src/com/sk89q/worldedit/blocks/BaseItemStack.java new file mode 100644 index 000000000..a8712303c --- /dev/null +++ b/src/com/sk89q/worldedit/blocks/BaseItemStack.java @@ -0,0 +1,75 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldedit.blocks; + +/** + * Represents a stack of BaseItems. + * + * @author sk89q + */ +public class BaseItemStack extends BaseItem { + /** + * Amount of an item. + */ + private int amount = 1; + + /** + * Construct the object. + * + * @param id + */ + public BaseItemStack(short id) { + super(id); + } + + /** + * Construct the object. + * + * @param id + */ + public BaseItemStack(short id, int amount) { + super(id); + this.amount = amount; + } + + /** + * Construct the object. + * + * @param id + */ + public BaseItemStack(short id, int amount, short damage) { + super(id, damage); + this.amount = amount; + } + + /** + * @return the amount + */ + public int getAmount() { + return amount; + } + + /** + * @param amount the amount to set + */ + public void setAmount(int amount) { + this.amount = amount; + } +} diff --git a/src/com/sk89q/worldedit/blocks/ChestBlock.java b/src/com/sk89q/worldedit/blocks/ChestBlock.java index 785aa1e64..5d39a9296 100644 --- a/src/com/sk89q/worldedit/blocks/ChestBlock.java +++ b/src/com/sk89q/worldedit/blocks/ChestBlock.java @@ -36,14 +36,14 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { /** * Store the list of items. */ - private Map> items; + private BaseItemStack[] items; /** * Construct the chest block. */ public ChestBlock() { super(54); - items = new HashMap>(); + items = new BaseItemStack[27]; } /** @@ -53,7 +53,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { */ public ChestBlock(int data) { super(54, data); - items = new HashMap>(); + items = new BaseItemStack[27]; } /** @@ -62,7 +62,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { * @param data * @param items */ - public ChestBlock(int data, Map> items) { + public ChestBlock(int data, BaseItemStack[] items) { super(54, data); this.items = items; } @@ -72,7 +72,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { * * @return */ - public Map> getItems() { + public BaseItemStack[] getItems() { return items; } @@ -81,7 +81,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { * * @return */ - public void setItems(Map> items) { + public void setItems(BaseItemStack[] items) { this.items = items; } @@ -103,15 +103,15 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { public Map toTileEntityNBT() throws DataException { List itemsList = new ArrayList(); - for (Map.Entry> entry : items.entrySet()) { + for (int i = 0; i < items.length; i++) { + BaseItemStack item = items[i]; Map data = new HashMap(); - CompoundTag item = new CompoundTag("Items", data); - BaseItem i = entry.getValue().getID(); - data.put("id", new ShortTag("id", i.getID())); - data.put("Damage", new ShortTag("Damage", i.getDamage())); - data.put("Count", new ByteTag("Count", (byte)entry.getValue().getAmount())); - data.put("Slot", new ByteTag("Slot", entry.getKey())); - itemsList.add(item); + CompoundTag itemTag = new CompoundTag("Items", data); + data.put("id", new ShortTag("id", item.getID())); + 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)); + itemsList.add(itemTag); } Map values = new HashMap(); values.put("Items", new ListTag("Items", CompoundTag.class, itemsList)); @@ -130,7 +130,6 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { return; } - Map> newItems = new HashMap>(); Tag t = values.get("id"); if (!(t instanceof StringTag) || !((StringTag)t).getValue().equals("Chest")) { @@ -138,6 +137,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { } ListTag items = (ListTag)Chunk.getChildTag(values, "Items", ListTag.class); + BaseItemStack[] newItems = new BaseItemStack[27]; for (Tag tag : items.getValue()) { if (!(tag instanceof CompoundTag)) { @@ -156,7 +156,9 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock { byte slot = (Byte)((ByteTag)Chunk.getChildTag(itemValues, "Slot", ByteTag.class)) .getValue(); - newItems.put(slot, new Countable(new BaseItem(id, damage), count)); + if (slot >= 0 && slot <= 26) { + newItems[slot] = new BaseItemStack(id, count, damage); + } } this.items = newItems;