diff --git a/src/EditSession.java b/src/EditSession.java index d895a04f5..86d99a8bc 100755 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -123,17 +123,7 @@ public class EditSession { // Chests } else if (block instanceof ChestBlock) { ChestBlock chestBlock = (ChestBlock)block; - BaseItem blankItem = new BaseItem((short)1); - Map> items = chestBlock.getItems(); - for (byte i = 0; i <= 26; i++) { - Countable item = items.get(i); - if (item != null) { - ServerInterface.setChestSlot(pt, i, item.getID(), - item.getAmount()); - } else { - ServerInterface.setChestSlot(pt, i, blankItem, 0); - } - } + ServerInterface.setChestContents(pt, chestBlock.getItems()); // Mob spawners } else if (block instanceof MobSpawnerBlock) { MobSpawnerBlock mobSpawnerblock = (MobSpawnerBlock)block; @@ -248,7 +238,8 @@ public class EditSession { return new SignBlock(type, data, text); // Chest } else if (type == 54) { - Map> items = ServerInterface.getChestContents(pt); + Map> items = + ServerInterface.getChestContents(pt); return new ChestBlock(data, items); // Mob spawner } else if (type == 52) { diff --git a/src/ServerInterface.java b/src/ServerInterface.java index 907e52e78..8bbaa9583 100644 --- a/src/ServerInterface.java +++ b/src/ServerInterface.java @@ -19,6 +19,8 @@ import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.blocks.BlockType; + import java.util.logging.Logger; import java.util.Map; import java.util.HashMap; @@ -126,28 +128,34 @@ public class ServerInterface { } /** - * Gets the contents of chests. + * Gets the contents of chests. Will return null if the chest does not + * really exist or it is the second block for a double chest. * * @param pt * @return */ public static Map> getChestContents(Vector pt) { - ComplexBlock cblock = etc.getServer().getComplexBlock( + ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); - if (!(cblock instanceof Chest)) { + Map> items; + Item[] nativeItems; + + if (cblock instanceof Chest) { + Chest chest = (Chest)cblock; + nativeItems = chest.getContents(); + } else { return null; } - Chest chest = (Chest)cblock; - Map> items = - new HashMap>(); + items = new HashMap>(); - for (byte i = 0; i <= 26; i++) { - Item item = chest.getItemFromSlot(i); + 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.put(i, + new Countable( + new BaseItem((short)item.getItemId()), item.getAmount())); } } @@ -158,23 +166,29 @@ public class ServerInterface { * Sets a chest slot. * * @param pt - * @param slot - * @param item - * @param amount + * @param contents * @return */ - public static boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount) { - ComplexBlock cblock = etc.getServer().getComplexBlock( + public static boolean setChestContents(Vector pt, + Map> contents) { + + ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); - if (!(cblock instanceof Chest)) { - return false; + if (cblock instanceof Chest) { + Chest chest = (Chest)cblock; + Item[] nativeItems = new Item[contents.size()]; + + for (Map.Entry> entry : contents.entrySet()) { + nativeItems[entry.getKey()] = + new Item(entry.getValue().getID().getID(), + entry.getValue().getAmount()); + } + + setContents(chest, nativeItems); } - Chest chest = (Chest)cblock; - chest.addItem(new Item(item.getID(), amount, slot)); - chest.update(); - return true; + return false; } /** @@ -183,18 +197,36 @@ public class ServerInterface { * @param pt */ public static boolean clearChest(Vector pt) { - ComplexBlock cblock = etc.getServer().getComplexBlock( + ComplexBlock cblock = etc.getServer().getOnlyComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); - if (!(cblock instanceof Chest)) { - return false; + if (cblock instanceof Chest) { + Chest chest = (Chest)cblock; + chest.clearContents(); + chest.update(); + return true; } - Chest chest = (Chest)cblock; - chest.clearContents(); - chest.update(); - return true; + return false; + } + + /** + * Set the contents of an ItemArray. + * + * @param itemArray + * @param contents + */ + private static void setContents(ItemArray itemArray, Item[] contents) { + int size = contents.length; + + for (int i = 0; i < size; i++) { + if (contents[i] == null) { + itemArray.removeItem(i); + } else { + itemArray.setSlot(contents[i], i); + } + } } /**