Fix bad behavior with small-max-stack-sized items when extracting multiple items at once

This commit is contained in:
reidbhuntley 2021-06-05 00:08:24 -04:00
parent 16cf6a44de
commit dff459a75f
2 changed files with 24 additions and 12 deletions

View file

@ -7,6 +7,8 @@ import java.util.function.Predicate;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import net.minecraft.item.Item;
import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableInt;
import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.config.AllConfigs;
@ -161,7 +163,7 @@ public class ItemHelper {
continue; continue;
if (!test.test(stack)) if (!test.test(stack))
continue; continue;
if (!extracting.isEmpty() && !ItemHandlerHelper.canItemStacksStack(stack, extracting)) { if (!extracting.isEmpty() && !canItemStackAmountsStack(stack, extracting)) {
potentialOtherMatch = true; potentialOtherMatch = true;
continue; continue;
} }
@ -174,8 +176,7 @@ public class ItemHelper {
if (!simulate && hasEnoughItems) if (!simulate && hasEnoughItems)
inv.extractItem(slot, stack.getCount(), false); inv.extractItem(slot, stack.getCount(), false);
if (extracting.getCount() >= maxExtractionCount if (extracting.getCount() >= maxExtractionCount) {
|| extracting.getCount() >= extracting.getMaxStackSize()) {
if (checkHasEnoughItems) { if (checkHasEnoughItems) {
hasEnoughItems = true; hasEnoughItems = true;
checkHasEnoughItems = false; checkHasEnoughItems = false;
@ -225,7 +226,7 @@ public class ItemHelper {
if (!test.test(stack)) if (!test.test(stack))
continue; continue;
if (!extracting.isEmpty() && !ItemHandlerHelper.canItemStacksStack(stack, extracting)) if (!extracting.isEmpty() && !canItemStackAmountsStack(stack, extracting))
continue; continue;
if (extracting.isEmpty()) if (extracting.isEmpty())
@ -235,13 +236,17 @@ public class ItemHelper {
if (!simulate) if (!simulate)
inv.extractItem(slot, stack.getCount(), false); inv.extractItem(slot, stack.getCount(), false);
if (extracting.getCount() >= maxExtractionCount || extracting.getCount() >= extracting.getMaxStackSize()) if (extracting.getCount() >= maxExtractionCount)
break; break;
} }
return extracting; 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<ItemStack> test) { public static ItemStack findFirstMatch(IItemHandler inv, Predicate<ItemStack> test) {
int slot = findFirstMatchingSlotIndex(inv, test); int slot = findFirstMatchingSlotIndex(inv, test);
if (slot == -1) if (slot == -1)

View file

@ -107,12 +107,19 @@ public class InvManipulationBehaviour extends TileEntityBehaviour {
return ItemStack.EMPTY; return ItemStack.EMPTY;
Predicate<ItemStack> test = getFilterTest(filter); Predicate<ItemStack> test = getFilterTest(filter);
ItemStack extract = ItemStack.EMPTY;
if (amount != -1) ItemStack simulatedItems = extractAmountOrThresh(inventory, test, amount, amountThreshold, true);
extract = ItemHelper.extract(inventory, test, amount, shouldSimulate); if (shouldSimulate || simulatedItems.isEmpty())
else return simulatedItems;
extract = ItemHelper.extract(inventory, test, amountThreshold, shouldSimulate);
return extract; return extractAmountOrThresh(inventory, test, amount, amountThreshold, false);
}
private static ItemStack extractAmountOrThresh(IItemHandler inventory, Predicate<ItemStack> test, int amount,
Function<ItemStack, Integer> 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) { public ItemStack insert(ItemStack stack) {