From be59618f51d33dc2d2c030b46c6d86d491fe6a40 Mon Sep 17 00:00:00 2001 From: EmmaIreland Date: Tue, 22 Apr 2014 19:57:45 -0500 Subject: [PATCH 1/3] Converted the if/else block in getType() to a series of helper methods. This allows the compiler to take responsibility for figuring out what type of inventory it is. --- .../craftbukkit/inventory/CraftInventory.java | 93 ++++++++++++------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index 6748465da6..d09c298202 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -19,12 +19,12 @@ import net.minecraft.server.TileEntityFurnace; import org.apache.commons.lang.Validate; +import org.bukkit.Material; import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; -import org.bukkit.Material; public class CraftInventory implements Inventory { protected final IInventory inventory; @@ -424,37 +424,66 @@ public String getTitle() { return inventory.getInventoryName(); } + + // Thanks to Droppers extending Dispensers, order is important for getTypeHelper. + public InventoryType getTypeHelper(InventoryCrafting inventory) { + return inventory.getSize() >= 9 ? InventoryType.WORKBENCH : InventoryType.CRAFTING; + } + + public InventoryType getTypeHelper(PlayerInventory inventory) { + return InventoryType.PLAYER; + } + + public InventoryType getTypeHelper(TileEntityDropper inventory) { + return InventoryType.DROPPER; + } + + public InventoryType getTypeHelper(TileEntityDispenser inventory) { + return InventoryType.DISPENSER; + } + + public InventoryType getTypeHelper(TileEntityFurnace inventory) { + return InventoryType.FURNACE; + } + + public InventoryType getTypeHelper(ContainerEnchantTableInventory inventory) { + return InventoryType.ENCHANTING; + } + + public InventoryType getTypeHelper(TileEntityBrewingStand inventory) { + return InventoryType.BREWING; + } + + public InventoryType getTypeHelper(CraftInventoryCustom.MinecraftInventory inventory) { + return inventory.getType(); + } + + public InventoryType getTypeHelper(InventoryEnderChest inventory) { + return InventoryType.ENDER_CHEST; + } + + public InventoryType getTypeHelper(InventoryMerchant inventory) { + return InventoryType.MERCHANT; + } + + public InventoryType getTypeHelper(TileEntityBeacon inventory) { + return InventoryType.BEACON; + } + + public InventoryType getTypeHelper(ContainerAnvilInventory inventory) { + return InventoryType.ANVIL; + } + + public InventoryType getTypeHelper(IHopper inventory) { + return InventoryType.HOPPER; + } + + public InventoryType getTypeHelper(IInventory inventory) { + return InventoryType.CHEST; + } + public InventoryType getType() { - // Thanks to Droppers extending Dispensers, order is important. - if (inventory instanceof InventoryCrafting) { - return inventory.getSize() >= 9 ? InventoryType.WORKBENCH : InventoryType.CRAFTING; - } else if (inventory instanceof PlayerInventory) { - return InventoryType.PLAYER; - } else if (inventory instanceof TileEntityDropper) { - return InventoryType.DROPPER; - } else if (inventory instanceof TileEntityDispenser) { - return InventoryType.DISPENSER; - } else if (inventory instanceof TileEntityFurnace) { - return InventoryType.FURNACE; - } else if (inventory instanceof ContainerEnchantTableInventory) { - return InventoryType.ENCHANTING; - } else if (inventory instanceof TileEntityBrewingStand) { - return InventoryType.BREWING; - } else if (inventory instanceof CraftInventoryCustom.MinecraftInventory) { - return ((CraftInventoryCustom.MinecraftInventory) inventory).getType(); - } else if (inventory instanceof InventoryEnderChest) { - return InventoryType.ENDER_CHEST; - } else if (inventory instanceof InventoryMerchant) { - return InventoryType.MERCHANT; - } else if (inventory instanceof TileEntityBeacon) { - return InventoryType.BEACON; - } else if (inventory instanceof ContainerAnvilInventory) { - return InventoryType.ANVIL; - } else if (inventory instanceof IHopper) { - return InventoryType.HOPPER; - } else { - return InventoryType.CHEST; - } + return getTypeHelper(inventory); } public InventoryHolder getHolder() { @@ -469,12 +498,10 @@ public void setMaxStackSize(int size) { inventory.setMaxStackSize(size); } - @Override public int hashCode() { return inventory.hashCode(); } - @Override public boolean equals(final Object obj) { return obj instanceof CraftInventory && ((CraftInventory) obj).inventory.equals(this.inventory); } From 3a1862369b0b1e61a44fe4416832078d170e11f3 Mon Sep 17 00:00:00 2001 From: EmmaIreland Date: Thu, 24 Apr 2014 11:16:31 -0500 Subject: [PATCH 2/3] Corrected the CraftInventoryCustom case that Eclipse so helpfully changed for us; added two @Overrides that Eclipse removed. --- .../java/org/bukkit/craftbukkit/inventory/CraftInventory.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index d09c298202..efe53bc5f8 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -455,7 +455,7 @@ public InventoryType getTypeHelper(TileEntityBrewingStand inventory) { } public InventoryType getTypeHelper(CraftInventoryCustom.MinecraftInventory inventory) { - return inventory.getType(); + return ((CraftInventoryCustom.MinecraftInventory) inventory).getType(); } public InventoryType getTypeHelper(InventoryEnderChest inventory) { @@ -498,10 +498,12 @@ public void setMaxStackSize(int size) { inventory.setMaxStackSize(size); } + @Override public int hashCode() { return inventory.hashCode(); } + @Override public boolean equals(final Object obj) { return obj instanceof CraftInventory && ((CraftInventory) obj).inventory.equals(this.inventory); } From 63db698e6aa89db09eb478bd5bf0f75c999b443d Mon Sep 17 00:00:00 2001 From: EmmaIreland Date: Thu, 24 Apr 2014 11:18:12 -0500 Subject: [PATCH 3/3] Moved an import that Eclipse moved out of place. --- .../java/org/bukkit/craftbukkit/inventory/CraftInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index efe53bc5f8..e82e6baf5f 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -19,12 +19,12 @@ import net.minecraft.server.TileEntityFurnace; import org.apache.commons.lang.Validate; -import org.bukkit.Material; import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; +import org.bukkit.Material; public class CraftInventory implements Inventory { protected final IInventory inventory;