From 1cee70cec09be1936e6454f526560237d0869e0c Mon Sep 17 00:00:00 2001 From: Major- Date: Sat, 15 Feb 2014 00:35:22 +0000 Subject: [PATCH] Add additional utility method to Inventory. --- src/org/apollo/game/model/Inventory.java | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/org/apollo/game/model/Inventory.java b/src/org/apollo/game/model/Inventory.java index e66b0a16..a2c9d7a9 100644 --- a/src/org/apollo/game/model/Inventory.java +++ b/src/org/apollo/game/model/Inventory.java @@ -462,6 +462,38 @@ public final class Inventory implements Cloneable { listeners.remove(listener); } + /** + * Removes {@code amount} of the item at the specified {@code slot}, under the condition that this item matches the + * specified {@code id}. If the item is not stacked, it will only remove the single item at the slot (meaning it + * will ignore any amount higher than 1). This means that this method will under no circumstances make any changes + * to other slots. + * + * @param slot The slot. + * @param id The item id. + * @param amount The amount to remove. + * @return The amount that was removed (0 if nothing was removed). + */ + public int removeSlot(int slot, int id, int amount) { + if (amount == 0) { + return 0; + } + + Item item = items[slot]; + if (item != null && item.getId() == id) { + int currentAmount = item.getAmount(); + int removed = currentAmount; + if (removed > amount) { + removed = amount; + } + + int remainder = currentAmount - removed; + set(slot, remainder > 0 ? new Item(item.getId(), remainder) : null); + + return removed; + } + return 0; + } + /** * Removes the item (if any) that is in the specified slot. *