From 4992aa1b5d516c1aa38380347ef2486d1a8f894f Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sun, 24 Nov 2024 11:06:35 +1100 Subject: [PATCH] Fix Inventory#addItem not respecting max stack size By: md_5 --- .../craftbukkit/inventory/CraftInventory.java | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) 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 index 8a17d178f9..820d06ede0 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -255,19 +255,6 @@ public class CraftInventory implements Inventory { return inventory.isEmpty(); } - public int firstPartial(Material material) { - Preconditions.checkArgument(material != null, "Material cannot be null"); - material = CraftLegacy.fromLegacy(material); - ItemStack[] inventory = getStorageContents(); - for (int i = 0; i < inventory.length; i++) { - ItemStack item = inventory[i]; - if (item != null && item.getType() == material && item.getAmount() < item.getMaxStackSize()) { - return i; - } - } - return -1; - } - private int firstPartial(ItemStack item) { ItemStack[] inventory = getStorageContents(); ItemStack filteredItem = CraftItemStack.asCraftCopy(item); @@ -276,7 +263,7 @@ public class CraftInventory implements Inventory { } for (int i = 0; i < inventory.length; i++) { ItemStack cItem = inventory[i]; - if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) { + if (cItem != null && cItem.getAmount() < getMaxItemStack(cItem) && cItem.isSimilar(filteredItem)) { return i; } } @@ -312,11 +299,12 @@ public class CraftInventory implements Inventory { break; } else { // More than a single stack! - if (item.getAmount() > getMaxItemStack()) { + int maxAmount = getMaxItemStack(item); + if (item.getAmount() > maxAmount) { CraftItemStack stack = CraftItemStack.asCraftCopy(item); - stack.setAmount(getMaxItemStack()); + stack.setAmount(maxAmount); setItem(firstFree, stack); - item.setAmount(item.getAmount() - getMaxItemStack()); + item.setAmount(item.getAmount() - maxAmount); } else { // Just store it setItem(firstFree, item); @@ -329,7 +317,7 @@ public class CraftInventory implements Inventory { int amount = item.getAmount(); int partialAmount = partialItem.getAmount(); - int maxAmount = partialItem.getMaxStackSize(); + int maxAmount = getMaxItemStack(partialItem); // Check if it fully fits if (amount + partialAmount <= maxAmount) { @@ -395,8 +383,8 @@ public class CraftInventory implements Inventory { return leftover; } - private int getMaxItemStack() { - return getInventory().getMaxStackSize(); + private int getMaxItemStack(ItemStack itemstack) { + return Math.min(itemstack.getMaxStackSize(), getInventory().getMaxStackSize()); } @Override