summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Broes <erikbroes@grum.nl>2011-01-16 16:09:59 +0100
committerErik Broes <erikbroes@grum.nl>2011-01-16 16:09:59 +0100
commit78cd77ef713e9073f2a46c5c2a3fea90db470158 (patch)
treee84c0097b826e1573cc475aaf1b2ef1a2767c168
parentd85a28e97e4036899e9b258dad7f9cb2c077b5a5 (diff)
downloadcraftbukkit-78cd77ef713e9073f2a46c5c2a3fea90db470158.tar
craftbukkit-78cd77ef713e9073f2a46c5c2a3fea90db470158.tar.gz
craftbukkit-78cd77ef713e9073f2a46c5c2a3fea90db470158.tar.lz
craftbukkit-78cd77ef713e9073f2a46c5c2a3fea90db470158.tar.xz
craftbukkit-78cd77ef713e9073f2a46c5c2a3fea90db470158.zip
Implement removeItem(ItemStack...)
-rw-r--r--[-rwxr-xr-x]src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
index 6773ea35..fd24050a 100755..100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
@@ -218,6 +218,47 @@ public class CraftInventory implements org.bukkit.inventory.Inventory {
return leftover;
}
+ public HashMap<Integer, ItemStack> removeItem(ItemStack... items) {
+ HashMap<Integer,ItemStack> leftover = new HashMap<Integer,ItemStack>();
+
+ // TODO: optimization
+
+ for (int i = 0; i < items.length; i++) {
+ ItemStack item = items[i];
+ int toDelete = item.getAmount();
+
+ while (true) {
+ int first = first(item.getType());
+
+ // Drat! we don't have this type in the inventory
+ if (first == -1) {
+ item.setAmount( toDelete );
+ leftover.put(i, item);
+ } else {
+ ItemStack itemStack = getItem(first);
+ int amount = itemStack.getAmount();
+
+ if (amount <= toDelete) {
+ toDelete -= amount;
+ // clear the slot, all used up
+ clear( i );
+ } else {
+ // split the stack and store
+ itemStack.setAmount( amount - toDelete );
+ setItem( i, itemStack );
+ toDelete = 0;
+ }
+ }
+
+ // Bail when done
+ if (toDelete <= 0) {
+ break;
+ }
+ }
+ }
+ return leftover;
+ }
+
private int getMaxItemStack() {
return getInventory().c();
}