From 97c370549fc9ff9c3deb5caa6ef6d7bb1c02b02a Mon Sep 17 00:00:00 2001 From: zml2008 Date: Fri, 23 Mar 2012 21:07:05 -0700 Subject: [PATCH] Handle Bukkit versions without Chest#getBlockInventory() --- .../sk89q/worldedit/bukkit/BukkitWorld.java | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 99706982d..4f5f63006 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -46,6 +46,7 @@ import org.bukkit.entity.Minecart; import org.bukkit.entity.Painting; import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.Tameable; +import org.bukkit.inventory.DoubleChestInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.Effect; @@ -395,6 +396,34 @@ public class BukkitWorld extends LocalWorld { return false; } + /** + * Gets the single block inventory for a potentially double chest. + * Handles people who have an old version of Bukkit. + * This should be replaced with {@link org.bukkit.block.Chest#getBlockInventory()} + * in a few months (now = March 2012) + * + * @param chest The chest to get a single block inventory for + * @return The chest's inventory + */ + private Inventory getBlockInventory(Chest chest) { + try { + return chest.getBlockInventory(); + } catch (Throwable t) { + if (chest.getInventory() instanceof DoubleChestInventory) { + DoubleChestInventory inven = (DoubleChestInventory) chest.getInventory(); + if (inven.getLeftSide().getHolder().equals(chest)) { + return inven.getLeftSide(); + } else if (inven.getRightSide().getHolder().equals(chest)) { + return inven.getRightSide(); + } else { + return inven; + } + } else { + return chest.getInventory(); + } + } + } + /** * Clear a chest's contents. * @@ -414,7 +443,7 @@ public class BukkitWorld extends LocalWorld { org.bukkit.inventory.InventoryHolder chest = (org.bukkit.inventory.InventoryHolder) state; Inventory inven = chest.getInventory(); if (chest instanceof Chest) { - inven = ((Chest) chest).getBlockInventory(); + inven = getBlockInventory((Chest) chest); } inven.clear(); return true; @@ -726,7 +755,7 @@ public class BukkitWorld extends LocalWorld { org.bukkit.inventory.InventoryHolder container = (org.bukkit.inventory.InventoryHolder) state; Inventory inven = container.getInventory(); if (container instanceof Chest) { - inven = ((Chest) container).getBlockInventory(); + inven = getBlockInventory((Chest) container); } int size = inven.getSize(); BaseItemStack[] contents = new BaseItemStack[size]; @@ -769,7 +798,7 @@ public class BukkitWorld extends LocalWorld { org.bukkit.inventory.InventoryHolder chest = (org.bukkit.inventory.InventoryHolder) state; Inventory inven = chest.getInventory(); if (chest instanceof Chest) { - inven = ((Chest) chest).getBlockInventory(); + inven = getBlockInventory((Chest) chest); } int size = inven.getSize();