From dff459a75f3ce7926d187e80e3c9d3bde63990fa Mon Sep 17 00:00:00 2001 From: reidbhuntley Date: Sat, 5 Jun 2021 00:08:24 -0400 Subject: [PATCH] Fix bad behavior with small-max-stack-sized items when extracting multiple items at once --- .../create/foundation/item/ItemHelper.java | 15 ++++++++----- .../inventory/InvManipulationBehaviour.java | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java b/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java index e8a901d58..2121a6a71 100644 --- a/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java +++ b/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java @@ -7,6 +7,8 @@ import java.util.function.Predicate; import javax.annotation.Nullable; +import net.minecraft.item.Item; + import org.apache.commons.lang3.mutable.MutableInt; import com.simibubi.create.foundation.config.AllConfigs; @@ -161,7 +163,7 @@ public class ItemHelper { continue; if (!test.test(stack)) continue; - if (!extracting.isEmpty() && !ItemHandlerHelper.canItemStacksStack(stack, extracting)) { + if (!extracting.isEmpty() && !canItemStackAmountsStack(stack, extracting)) { potentialOtherMatch = true; continue; } @@ -174,8 +176,7 @@ public class ItemHelper { if (!simulate && hasEnoughItems) inv.extractItem(slot, stack.getCount(), false); - if (extracting.getCount() >= maxExtractionCount - || extracting.getCount() >= extracting.getMaxStackSize()) { + if (extracting.getCount() >= maxExtractionCount) { if (checkHasEnoughItems) { hasEnoughItems = true; checkHasEnoughItems = false; @@ -225,7 +226,7 @@ public class ItemHelper { if (!test.test(stack)) continue; - if (!extracting.isEmpty() && !ItemHandlerHelper.canItemStacksStack(stack, extracting)) + if (!extracting.isEmpty() && !canItemStackAmountsStack(stack, extracting)) continue; if (extracting.isEmpty()) @@ -235,13 +236,17 @@ public class ItemHelper { if (!simulate) inv.extractItem(slot, stack.getCount(), false); - if (extracting.getCount() >= maxExtractionCount || extracting.getCount() >= extracting.getMaxStackSize()) + if (extracting.getCount() >= maxExtractionCount) break; } return extracting; } + public static boolean canItemStackAmountsStack(ItemStack a, ItemStack b) { + return ItemHandlerHelper.canItemStacksStack(a,b) && a.getCount() + b.getCount() <= a.getMaxStackSize(); + } + public static ItemStack findFirstMatch(IItemHandler inv, Predicate test) { int slot = findFirstMatchingSlotIndex(inv, test); if (slot == -1) diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java index eac304882..ba96a9fc2 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java @@ -107,12 +107,19 @@ public class InvManipulationBehaviour extends TileEntityBehaviour { return ItemStack.EMPTY; Predicate test = getFilterTest(filter); - ItemStack extract = ItemStack.EMPTY; - if (amount != -1) - extract = ItemHelper.extract(inventory, test, amount, shouldSimulate); - else - extract = ItemHelper.extract(inventory, test, amountThreshold, shouldSimulate); - return extract; + + ItemStack simulatedItems = extractAmountOrThresh(inventory, test, amount, amountThreshold, true); + if (shouldSimulate || simulatedItems.isEmpty()) + return simulatedItems; + + return extractAmountOrThresh(inventory, test, amount, amountThreshold, false); + } + + private static ItemStack extractAmountOrThresh(IItemHandler inventory, Predicate test, int amount, + Function amountThreshold, boolean shouldSimulate) { + if (amount == -1) + return ItemHelper.extract(inventory, test, amountThreshold, shouldSimulate); + return ItemHelper.extract(inventory, test, amount, shouldSimulate); } public ItemStack insert(ItemStack stack) { @@ -156,7 +163,7 @@ public class InvManipulationBehaviour extends TileEntityBehaviour { if (!targetCapability.isPresent()) findNewCapability(); } - + @Override public void tick() { super.tick();