Stack of Spades

- Protect item handlers against invalid item stack sizes
- Fix some held or dropped items not updating count when inserted into item handlers
This commit is contained in:
simibubi 2024-10-08 16:43:50 +02:00
parent a99f05d8b0
commit 8e50ad4cfd
8 changed files with 34 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package com.simibubi.create.content.fluids.drain;
import com.simibubi.create.content.fluids.transfer.GenericItemEmptying; import com.simibubi.create.content.fluids.transfer.GenericItemEmptying;
import com.simibubi.create.content.kinetics.belt.transport.TransportedItemStack; import com.simibubi.create.content.kinetics.belt.transport.TransportedItemStack;
import com.simibubi.create.foundation.item.ItemHelper;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -38,7 +39,8 @@ public class ItemDrainItemHandler implements IItemHandler {
if (stack.getCount() > 1 && GenericItemEmptying.canItemBeEmptied(blockEntity.getLevel(), stack)) { if (stack.getCount() > 1 && GenericItemEmptying.canItemBeEmptied(blockEntity.getLevel(), stack)) {
returned = ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - 1); returned = ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - 1);
stack = ItemHandlerHelper.copyStackWithSize(stack, 1); stack = ItemHandlerHelper.copyStackWithSize(stack, 1);
} } else
returned = ItemHelper.limitCountToMaxStackSize(stack, simulate);
if (!simulate) { if (!simulate) {
TransportedItemStack heldItem = new TransportedItemStack(stack); TransportedItemStack heldItem = new TransportedItemStack(stack);

View file

@ -218,6 +218,8 @@ public class BeltBlock extends HorizontalKineticBlock
.copy(), false); .copy(), false);
if (remainder.isEmpty()) if (remainder.isEmpty())
itemEntity.discard(); itemEntity.discard();
else if (remainder.getCount() != itemEntity.getItem().getCount())
itemEntity.setItem(remainder);
}); });
return; return;
} }

View file

@ -1,5 +1,7 @@
package com.simibubi.create.content.kinetics.belt.transport; package com.simibubi.create.content.kinetics.belt.transport;
import com.simibubi.create.foundation.item.ItemHelper;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -29,6 +31,7 @@ public class ItemHandlerBeltSegment implements IItemHandler {
@Override @Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
if (this.beltInventory.canInsertAt(offset)) { if (this.beltInventory.canInsertAt(offset)) {
ItemStack remainder = ItemHelper.limitCountToMaxStackSize(stack, simulate);
if (!simulate) { if (!simulate) {
TransportedItemStack newStack = new TransportedItemStack(stack); TransportedItemStack newStack = new TransportedItemStack(stack);
newStack.insertedAt = offset; newStack.insertedAt = offset;
@ -38,7 +41,7 @@ public class ItemHandlerBeltSegment implements IItemHandler {
this.beltInventory.belt.setChanged(); this.beltInventory.belt.setChanged();
this.beltInventory.belt.sendData(); this.beltInventory.belt.sendData();
} }
return ItemStack.EMPTY; return remainder;
} }
return stack; return stack;
} }

View file

@ -1,6 +1,7 @@
package com.simibubi.create.content.kinetics.deployer; package com.simibubi.create.content.kinetics.deployer;
import com.simibubi.create.foundation.blockEntity.behaviour.filtering.FilteringBehaviour; import com.simibubi.create.foundation.blockEntity.behaviour.filtering.FilteringBehaviour;
import com.simibubi.create.foundation.item.ItemHelper;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -52,9 +53,10 @@ public class DeployerItemHandler implements IItemHandlerModifiable {
ItemStack held = getHeld(); ItemStack held = getHeld();
if (held.isEmpty()) { if (held.isEmpty()) {
ItemStack remainder = ItemHelper.limitCountToMaxStackSize(stack, simulate);
if (!simulate) if (!simulate)
set(stack); set(stack);
return ItemStack.EMPTY; return remainder;
} }
if (!ItemHandlerHelper.canItemStacksStack(held, stack)) if (!ItemHandlerHelper.canItemStacksStack(held, stack))

View file

@ -1,5 +1,7 @@
package com.simibubi.create.content.logistics.chute; package com.simibubi.create.content.logistics.chute;
import com.simibubi.create.foundation.item.ItemHelper;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -25,9 +27,10 @@ public class ChuteItemHandler implements IItemHandler {
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
if (!blockEntity.canAcceptItem(stack)) if (!blockEntity.canAcceptItem(stack))
return stack; return stack;
ItemStack remainder = ItemHelper.limitCountToMaxStackSize(stack, simulate);
if (!simulate) if (!simulate)
blockEntity.setItem(stack); blockEntity.setItem(stack);
return ItemStack.EMPTY; return remainder;
} }
@Override @Override

View file

@ -93,7 +93,7 @@ public abstract class FunnelBlock extends AbstractDirectionalFunnelBlock {
withBlockEntityDo(worldIn, pos, be -> { withBlockEntityDo(worldIn, pos, be -> {
ItemStack toInsert = heldItem.copy(); ItemStack toInsert = heldItem.copy();
ItemStack remainder = tryInsert(worldIn, pos, toInsert, false); ItemStack remainder = tryInsert(worldIn, pos, toInsert, false);
if (!ItemStack.matches(remainder, toInsert)) if (!ItemStack.matches(remainder, toInsert) || remainder.getCount() != heldItem.getCount())
player.setItemInHand(handIn, remainder); player.setItemInHand(handIn, remainder);
}); });
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;

View file

@ -1,5 +1,7 @@
package com.simibubi.create.content.logistics.tunnel; package com.simibubi.create.content.logistics.tunnel;
import com.simibubi.create.foundation.item.ItemHelper;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -33,9 +35,11 @@ public class BrassTunnelItemHandler implements IItemHandler {
if (!blockEntity.canTakeItems()) if (!blockEntity.canTakeItems())
return stack; return stack;
ItemStack remainder = ItemHelper.limitCountToMaxStackSize(stack, simulate);
if (!simulate) if (!simulate)
blockEntity.setStackToDistribute(stack, null); blockEntity.setStackToDistribute(stack, null);
return ItemStack.EMPTY; return remainder;
} }
@Override @Override

View file

@ -276,4 +276,16 @@ public class ItemHelper {
} }
return -1; return -1;
} }
public static ItemStack limitCountToMaxStackSize(ItemStack stack, boolean simulate) {
int count = stack.getCount();
int max = stack.getMaxStackSize();
if (count <= max)
return ItemStack.EMPTY;
ItemStack remainder = ItemHandlerHelper.copyStackWithSize(stack, count - max);
if (!simulate)
stack.setCount(max);
return remainder;
}
} }