mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-29 15:49:00 +01:00
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:
parent
b39ff2b12d
commit
34033480d7
2 changed files with 104 additions and 0 deletions
45
Spigot-API-Patches/Inventory-removeItemAnySlot.patch
Normal file
45
Spigot-API-Patches/Inventory-removeItemAnySlot.patch
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Zach Brown <1254957+zachbr@users.noreply.github.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
|
||||||
|
@@ -0,0 +0,0 @@ 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
|
||||||
|
*
|
||||||
|
--
|
59
Spigot-Server-Patches/Inventory-removeItemAnySlot.patch
Normal file
59
Spigot-Server-Patches/Inventory-removeItemAnySlot.patch
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Zach Brown <1254957+zachbr@users.noreply.github.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
|
||||||
|
@@ -0,0 +0,0 @@ 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;
|
||||||
|
|
||||||
|
@@ -0,0 +0,0 @@ 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>();
|
||||||
|
|
||||||
|
@@ -0,0 +0,0 @@ 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) {
|
||||||
|
--
|
Loading…
Reference in a new issue