Add Inventory#removeItemAnySlot

Closes GH-1360

This behaves identically to Inventory#removeItem, except it
searches all slots rather than just the storage contents.
This commit is contained in:
Zach Brown 2018-08-29 21:41:39 -04:00
parent 5ad02bdff5
commit f8b40558fd
No known key found for this signature in database
GPG Key ID: CC9DA35FC5450B76
2 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,47 @@
From 5e044c9dc29d14964fb2823ee23288d6e33a0ec0 Mon Sep 17 00:00:00 2001
From: Zach Brown <zach.brown@destroystokyo.com>
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<ItemStack> {
*/
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) throws IllegalArgumentException;
+ // Paper start
+ /**
+ * Searches all possible inventory slots in order to remove the given ItemStacks.
+ * <p>
+ * 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.
+ * <p>
+ * It will try to remove 'as much as possible' from the types and amounts
+ * you give as arguments.
+ * <p>
+ * 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.
+ * <p>
+ * 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<Integer, ItemStack> removeItemAnySlot(ItemStack... items) throws IllegalArgumentException;
+ // Paper end
+
/**
* Returns all ItemStacks from the inventory
*
--
2.18.0

View File

@ -0,0 +1,61 @@
From d29d86fc4ab460dd1f96a04d2323098b0afb66ae Mon Sep 17 00:00:00 2001
From: Zach Brown <zach.brown@destroystokyo.com>
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<Integer, ItemStack> removeItem(ItemStack... items) {
+ // Paper start
+ return removeItem(false, items);
+ }
+
+ @Override
+ public HashMap<Integer, ItemStack> removeItemAnySlot(ItemStack... items) {
+ return removeItem(true, items);
+ }
+
+ private HashMap<Integer, ItemStack> removeItem(boolean searchEntire, ItemStack... items) {
+ // Paper end
Validate.notNull(items, "Items cannot be null");
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
@@ -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