From f8b40558fd370354b70f9a1bbe42074331dca639 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Wed, 29 Aug 2018 21:41:39 -0400 Subject: [PATCH] Add Inventory#removeItemAnySlot Closes GH-1360 This behaves identically to Inventory#removeItem, except it searches all slots rather than just the storage contents. --- .../0145-Inventory-removeItemAnySlot.patch | 47 ++++++++++++++ .../0354-Inventory-removeItemAnySlot.patch | 61 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 Spigot-API-Patches/0145-Inventory-removeItemAnySlot.patch create mode 100644 Spigot-Server-Patches/0354-Inventory-removeItemAnySlot.patch diff --git a/Spigot-API-Patches/0145-Inventory-removeItemAnySlot.patch b/Spigot-API-Patches/0145-Inventory-removeItemAnySlot.patch new file mode 100644 index 0000000000..620ce79566 --- /dev/null +++ b/Spigot-API-Patches/0145-Inventory-removeItemAnySlot.patch @@ -0,0 +1,47 @@ +From 5e044c9dc29d14964fb2823ee23288d6e33a0ec0 Mon Sep 17 00:00:00 2001 +From: Zach Brown +Date: Tue, 28 Aug 2018 23:04:06 -0400 +Subject: [PATCH] Inventory#removeItemAnySlot + + +diff --git a/src/main/java/org/bukkit/inventory/Inventory.java b/src/main/java/org/bukkit/inventory/Inventory.java +index 2dbba001..ec58e972 100644 +--- a/src/main/java/org/bukkit/inventory/Inventory.java ++++ b/src/main/java/org/bukkit/inventory/Inventory.java +@@ -117,6 +117,33 @@ public interface Inventory extends Iterable { + */ + public HashMap removeItem(ItemStack... items) throws IllegalArgumentException; + ++ // Paper start ++ /** ++ * Searches all possible inventory slots in order to remove the given ItemStacks. ++ *

++ * Similar to {@link Inventory#removeItem(ItemStack...)} in behavior, except this ++ * method will check all possible slots in the inventory, rather than just the main ++ * storage contents. ++ *

++ * It will try to remove 'as much as possible' from the types and amounts ++ * you give as arguments. ++ *

++ * The returned HashMap contains what it couldn't remove, where the key is ++ * the index of the parameter, and the value is the ItemStack at that ++ * index of the varargs parameter. If all the given ItemStacks are ++ * removed, it will return an empty HashMap. ++ *

++ * It is known that in some implementations this method will also set the ++ * inputted argument amount to the number of that item not removed from ++ * slots. ++ * ++ * @param items The ItemStacks to remove ++ * @return A HashMap containing items that couldn't be removed. ++ * @throws IllegalArgumentException if items is null ++ */ ++ public HashMap removeItemAnySlot(ItemStack... items) throws IllegalArgumentException; ++ // Paper end ++ + /** + * Returns all ItemStacks from the inventory + * +-- +2.18.0 + diff --git a/Spigot-Server-Patches/0354-Inventory-removeItemAnySlot.patch b/Spigot-Server-Patches/0354-Inventory-removeItemAnySlot.patch new file mode 100644 index 0000000000..9009f8338b --- /dev/null +++ b/Spigot-Server-Patches/0354-Inventory-removeItemAnySlot.patch @@ -0,0 +1,61 @@ +From d29d86fc4ab460dd1f96a04d2323098b0afb66ae Mon Sep 17 00:00:00 2001 +From: Zach Brown +Date: Tue, 28 Aug 2018 23:04:15 -0400 +Subject: [PATCH] Inventory#removeItemAnySlot + + +diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +index dd7b3d766..01af98293 100644 +--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java ++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +@@ -213,10 +213,16 @@ public class CraftInventory implements Inventory { + } + + private int first(ItemStack item, boolean withAmount) { ++ // Paper start ++ return first(item, withAmount, getStorageContents()); ++ } ++ ++ private int first(ItemStack item, boolean withAmount, ItemStack[] inventory) { ++ // Paper end + if (item == null) { + return -1; + } +- ItemStack[] inventory = getStorageContents(); ++ //ItemStack[] inventory = getStorageContents(); // Paper - let param deal + for (int i = 0; i < inventory.length; i++) { + if (inventory[i] == null) continue; + +@@ -331,6 +337,17 @@ public class CraftInventory implements Inventory { + } + + public HashMap removeItem(ItemStack... items) { ++ // Paper start ++ return removeItem(false, items); ++ } ++ ++ @Override ++ public HashMap removeItemAnySlot(ItemStack... items) { ++ return removeItem(true, items); ++ } ++ ++ private HashMap removeItem(boolean searchEntire, ItemStack... items) { ++ // Paper end + Validate.notNull(items, "Items cannot be null"); + HashMap leftover = new HashMap(); + +@@ -341,7 +358,10 @@ public class CraftInventory implements Inventory { + int toDelete = item.getAmount(); + + while (true) { +- int first = first(item, false); ++ // Paper start - Allow searching entire contents ++ ItemStack[] toSearch = searchEntire ? getContents() : getStorageContents(); ++ int first = first(item, false, toSearch); ++ // Paper end + + // Drat! we don't have this type in the inventory + if (first == -1) { +-- +2.18.0 +