mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-05 02:22:12 +01:00
Fix Inventory#addItem not respecting max stack size
By: md_5 <git@md-5.net>
This commit is contained in:
parent
f642133483
commit
4992aa1b5d
1 changed files with 8 additions and 20 deletions
|
@ -255,19 +255,6 @@ public class CraftInventory implements Inventory {
|
||||||
return inventory.isEmpty();
|
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) {
|
private int firstPartial(ItemStack item) {
|
||||||
ItemStack[] inventory = getStorageContents();
|
ItemStack[] inventory = getStorageContents();
|
||||||
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
|
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
|
||||||
|
@ -276,7 +263,7 @@ public class CraftInventory implements Inventory {
|
||||||
}
|
}
|
||||||
for (int i = 0; i < inventory.length; i++) {
|
for (int i = 0; i < inventory.length; i++) {
|
||||||
ItemStack cItem = inventory[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;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,11 +299,12 @@ public class CraftInventory implements Inventory {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// More than a single stack!
|
// More than a single stack!
|
||||||
if (item.getAmount() > getMaxItemStack()) {
|
int maxAmount = getMaxItemStack(item);
|
||||||
|
if (item.getAmount() > maxAmount) {
|
||||||
CraftItemStack stack = CraftItemStack.asCraftCopy(item);
|
CraftItemStack stack = CraftItemStack.asCraftCopy(item);
|
||||||
stack.setAmount(getMaxItemStack());
|
stack.setAmount(maxAmount);
|
||||||
setItem(firstFree, stack);
|
setItem(firstFree, stack);
|
||||||
item.setAmount(item.getAmount() - getMaxItemStack());
|
item.setAmount(item.getAmount() - maxAmount);
|
||||||
} else {
|
} else {
|
||||||
// Just store it
|
// Just store it
|
||||||
setItem(firstFree, item);
|
setItem(firstFree, item);
|
||||||
|
@ -329,7 +317,7 @@ public class CraftInventory implements Inventory {
|
||||||
|
|
||||||
int amount = item.getAmount();
|
int amount = item.getAmount();
|
||||||
int partialAmount = partialItem.getAmount();
|
int partialAmount = partialItem.getAmount();
|
||||||
int maxAmount = partialItem.getMaxStackSize();
|
int maxAmount = getMaxItemStack(partialItem);
|
||||||
|
|
||||||
// Check if it fully fits
|
// Check if it fully fits
|
||||||
if (amount + partialAmount <= maxAmount) {
|
if (amount + partialAmount <= maxAmount) {
|
||||||
|
@ -395,8 +383,8 @@ public class CraftInventory implements Inventory {
|
||||||
return leftover;
|
return leftover;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getMaxItemStack() {
|
private int getMaxItemStack(ItemStack itemstack) {
|
||||||
return getInventory().getMaxStackSize();
|
return Math.min(itemstack.getMaxStackSize(), getInventory().getMaxStackSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue