Fix Inventory#addItem not respecting max stack size

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot 2024-11-24 11:06:35 +11:00
parent f642133483
commit 4992aa1b5d

View file

@ -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