From 4eeceacde7626d8809ded485d007ad43bc523393 Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Sun, 9 Jan 2011 23:41:05 +0100 Subject: [PATCH] Fixed some bugs with inventory --- .../bukkit/craftbukkit/CraftInventory.java | 65 +++++++++++-------- .../craftbukkit/CraftInventoryPlayer.java | 26 ++++---- .../bukkit/craftbukkit/CraftItemStack.java | 2 +- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/CraftInventory.java index c74ecea373..b79413d689 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftInventory.java @@ -26,12 +26,12 @@ public class CraftInventory implements org.bukkit.Inventory { return getInventory().b(); } - public ItemStack getItem(int index) { + public CraftItemStack getItem(int index) { return new CraftItemStack(getInventory().a(index)); } - public ItemStack[] getContents() { - ItemStack[] items = new ItemStack[getSize()]; + public CraftItemStack[] getContents() { + CraftItemStack[] items = new CraftItemStack[getSize()]; net.minecraft.server.ItemStack[] mcItems = getInventory().getContents(); for (int i = 0; i < mcItems.length; i++ ) { @@ -127,7 +127,8 @@ public class CraftInventory implements org.bukkit.Inventory { public int firstPartial(int materialId) { ItemStack[] inventory = getContents(); for (int i = 0; i < inventory.length; i++) { - if (inventory[i].getAmount() <= inventory[i].getMaxStackSize()) { + ItemStack item = inventory[i]; + if (item != null && item.getTypeID() == materialId && item.getAmount() < item.getMaxStackSize()) { return i; } } @@ -141,8 +142,8 @@ public class CraftInventory implements org.bukkit.Inventory { public int firstPartial(ItemStack item) { return firstPartial(item.getTypeID()); } - - public HashMap addItem(ItemStack... items) { + + public HashMap addItem(ItemStack... items) { HashMap leftover = new HashMap(); /* TODO: some optimization @@ -165,31 +166,43 @@ public class CraftInventory implements org.bukkit.Inventory { if (firstFree == -1) { // No space at all! leftover.put(i, item); + break; } else { - // Just store it - setItem( firstFree, item ); + // More than a single stack! + if (item.getAmount() > getMaxItemStack()) { + setItem( firstFree, new ItemStack(item.getTypeID(), getMaxItemStack())); + item.setAmount(item.getAmount() - getMaxItemStack()); + } else { + // Just store it + setItem( firstFree, item ); + break; + } } - break; + } else { + // So, apparently it might only partially fit, well lets do just that + ItemStack partialItem = getItem(firstPartial); + + int amount = item.getAmount(); + int partialAmount = partialItem.getAmount(); + int maxAmount = partialItem.getMaxStackSize(); + + // Check if it fully fits + if (amount + partialAmount <= maxAmount) { + partialItem.setAmount( amount + partialAmount ); + break; + } + + // It fits partially + partialItem.setAmount( maxAmount ); + item.setAmount( amount + partialAmount - maxAmount ); } - - // So, apparently it might only partially fit, well lets do just that - ItemStack partialItem = getItem(firstPartial); - - int amount = item.getAmount(); - int partialAmount = partialItem.getAmount(); - int maxAmount = partialItem.getMaxStackSize(); - - // Check if it fully fits - if (amount + partialAmount <= maxAmount) { - partialItem.setAmount( amount + partialAmount ); - break; - } - - // It fits partially - partialItem.setAmount( maxAmount ); - item.setAmount( amount + partialAmount - maxAmount ); } } return leftover; } + + private int getMaxItemStack() { + return getInventory().c(); + } + } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java index 3ee882fc60..666698b90b 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftInventoryPlayer.java @@ -1,7 +1,5 @@ package org.bukkit.craftbukkit; -import java.util.ArrayList; - import net.minecraft.server.InventoryPlayer; import org.bukkit.ItemStack; @@ -16,36 +14,34 @@ public class CraftInventoryPlayer extends CraftInventory implements PlayerInvent return (InventoryPlayer) inventory; } - public ArrayList getArmorContents() { - ArrayList items = new ArrayList(); - for (net.minecraft.server.ItemStack item : getInventory().getArmorContents()) { - ItemStack i = null; - if (item != null) { - i = new CraftItemStack(item); - } - items.add(i); + public CraftItemStack[] getArmorContents() { + net.minecraft.server.ItemStack[] mcItems = getInventory().getArmorContents(); + CraftItemStack[] items = new CraftItemStack[mcItems.length]; + + for (int i = 0; i < mcItems.length; i++ ) { + items[i] = new CraftItemStack(mcItems[i]); } return items; } - public ItemStack getItemInHand() { + public CraftItemStack getItemInHand() { return new CraftItemStack( getInventory().e() ); } - public ItemStack getHelmet() { + public CraftItemStack getHelmet() { return getItem( getSize() - 4 ); } - public ItemStack getChestplate() { + public CraftItemStack getChestplate() { return getItem( getSize() - 3 ); } - public ItemStack getLeggings() { + public CraftItemStack getLeggings() { return getItem( getSize() - 2 ); } - public ItemStack getBoots() { + public CraftItemStack getBoots() { return getItem( getSize() - 1 ); } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java index d1571ac46f..710bad4f53 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftItemStack.java @@ -48,7 +48,7 @@ public class CraftItemStack extends ItemStack { @Override public int getAmount() { super.setAmount(item != null ? item.a : 0); // sync, needed? - return item.a; + return (item != null ? item.a : 0); } @Override