Fixed VirtualInventory#setItem using the wrong maxStackSize

This commit is contained in:
NichtStudioCode 2021-06-17 22:34:27 +02:00
parent 9a8d46d6c9
commit 488092a8f8

@ -17,6 +17,7 @@ import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
public class VirtualInventory implements ConfigurationSerializable { public class VirtualInventory implements ConfigurationSerializable {
@ -243,6 +244,17 @@ public class VirtualInventory implements ConfigurationSerializable {
else return slotMaxStackSize; else return slotMaxStackSize;
} }
/**
* Gets the maximum stack size for a specific slot while ignoring the {@link ItemStack} on it
* and it's own maximum stack size.
*
* @param slot The slot
* @return The maximum stack size on that slo
*/
public int getMaxSlotStackSize(int slot) {
return stackSizes == null ? 64 : stackSizes[slot];
}
/** /**
* Sets all the maximum allowed stack sizes * Sets all the maximum allowed stack sizes
* *
@ -342,7 +354,7 @@ public class VirtualInventory implements ConfigurationSerializable {
* @return If the action was successful * @return If the action was successful
*/ */
public boolean setItemStack(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack itemStack) { public boolean setItemStack(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack itemStack) {
int maxStackSize = getMaxStackSize(slot, itemStack != null ? itemStack.getMaxStackSize() : -1); int maxStackSize = min(getMaxSlotStackSize(slot), itemStack != null ? itemStack.getMaxStackSize() : 64);
if (itemStack != null && itemStack.getAmount() > maxStackSize) return false; if (itemStack != null && itemStack.getAmount() > maxStackSize) return false;
return forceSetItemStack(updateReason, slot, itemStack); return forceSetItemStack(updateReason, slot, itemStack);
} }
@ -519,7 +531,7 @@ public class VirtualInventory implements ConfigurationSerializable {
ItemStack partialItem = items[partialSlot]; ItemStack partialItem = items[partialSlot];
int maxStackSize = getMaxStackSize(partialSlot, -1); int maxStackSize = getMaxStackSize(partialSlot, -1);
amountLeft = Math.max(0, amountLeft - (maxStackSize - partialItem.getAmount())); amountLeft = max(0, amountLeft - (maxStackSize - partialItem.getAmount()));
} }
// remaining items would be added to empty slots // remaining items would be added to empty slots
@ -527,7 +539,7 @@ public class VirtualInventory implements ConfigurationSerializable {
if (amountLeft == 0) break; if (amountLeft == 0) break;
int maxStackSize = getMaxStackSize(emptySlot, itemStack.getMaxStackSize()); int maxStackSize = getMaxStackSize(emptySlot, itemStack.getMaxStackSize());
amountLeft -= Math.min(amountLeft, maxStackSize); amountLeft -= min(amountLeft, maxStackSize);
} }
return amountLeft; return amountLeft;
@ -608,7 +620,7 @@ public class VirtualInventory implements ConfigurationSerializable {
private int takeFrom(@Nullable UpdateReason updateReason, int index, int maxTake) { private int takeFrom(@Nullable UpdateReason updateReason, int index, int maxTake) {
ItemStack itemStack = items[index]; ItemStack itemStack = items[index];
int amount = itemStack.getAmount(); int amount = itemStack.getAmount();
int take = Math.min(amount, maxTake); int take = min(amount, maxTake);
ItemStack newStack; ItemStack newStack;
if (take != amount) { if (take != amount) {