From 06f8475467af502efce27984b9cf8773948063b0 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sun, 16 Jan 2011 16:09:59 +0100 Subject: [PATCH] Implement removeItem(ItemStack...) By: Erik Broes --- .../craftbukkit/inventory/CraftInventory.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) mode change 100755 => 100644 paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java old mode 100755 new mode 100644 index 6773ea35e3..fd24050a0f --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/paper-server/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 removeItem(ItemStack... items) { + HashMap leftover = new HashMap(); + + // 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(); }