diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DispenserMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DispenserMovementBehaviour.java index c69cff722..f49e1df97 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DispenserMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DispenserMovementBehaviour.java @@ -31,12 +31,10 @@ public class DispenserMovementBehaviour extends DropperMovementBehaviour { @Override protected void activate(MovementContext context, BlockPos pos) { - int i = getDispenseSlot(context); - if (i < 0) { + ItemStack itemstack = getDispenseStack(context); + if (itemstack.isEmpty()) { context.world.playEvent(1001, pos, 0); } else { - ItemStack itemstack = getStacks(context).get(i); - // Special dispense item behaviour for moving contraptions if (MOVED_DISPENSE_ITEM_BEHAVIOURS.containsKey(itemstack.getItem())) { MOVED_DISPENSE_ITEM_BEHAVIOURS.get(itemstack.getItem()).dispense(itemstack, context, pos); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DropperMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DropperMovementBehaviour.java index e6d7d962f..7a45b5d4f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DropperMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/dispenser/DropperMovementBehaviour.java @@ -10,6 +10,8 @@ import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.server.ServerWorld; +import java.util.ArrayList; +import java.util.List; import java.util.Random; public class DropperMovementBehaviour extends MovementBehaviour { @@ -17,11 +19,11 @@ public class DropperMovementBehaviour extends MovementBehaviour { private static final Random RNG = new Random(); protected void activate(MovementContext context, BlockPos pos) { - int i = getDispenseSlot(context); - if (i < 0) { + ItemStack itemstack = getDispenseStack(context); + if (itemstack.isEmpty()) { context.world.playEvent(1001, pos, 0); } else { - defaultBehaviour.dispense(getStacks(context).get(i), context, pos); + defaultBehaviour.dispense(itemstack, context, pos); } } @@ -39,7 +41,7 @@ public class DropperMovementBehaviour extends MovementBehaviour { } @SuppressWarnings("unchecked") - protected NonNullList getStacks(MovementContext context) { + private NonNullList getStacks(MovementContext context) { if (!(context.temporaryData instanceof NonNullList) && context.world instanceof ServerWorld) { NonNullList stacks = NonNullList.withSize(9, ItemStack.EMPTY); ItemStackHelper.loadAllItems(context.tileData, stacks); @@ -48,6 +50,21 @@ public class DropperMovementBehaviour extends MovementBehaviour { return (NonNullList) context.temporaryData; } + private ArrayList getUseableStacks(MovementContext context) { + ArrayList useable = new ArrayList<>(); + for (ItemStack testStack : getStacks(context)) { + if (testStack == null || testStack.isEmpty()) + continue; + if (testStack.getMaxStackSize() == 1) { + ItemStack stack = ItemHelper.findFirstMatch(context.contraption.inventory, testStack::isItemEqual); + if (!stack.isEmpty()) + useable.add(stack); + } else if (testStack.getCount() >= 2) + useable.add(testStack); + } + return useable; + } + @Override public void writeExtraData(MovementContext context) { NonNullList stacks = getStacks(context); @@ -62,15 +79,18 @@ public class DropperMovementBehaviour extends MovementBehaviour { writeExtraData(context); } - protected int getDispenseSlot(MovementContext context) { + protected ItemStack getDispenseStack(MovementContext context) { int i = -1; int j = 1; - NonNullList stacks = getStacks(context); + List stacks = getUseableStacks(context); for (int k = 0; k < stacks.size(); ++k) { - if (!stacks.get(k).isEmpty() && RNG.nextInt(j++) == 0 && stacks.get(k).getCount() >= 2) { + if (RNG.nextInt(j++) == 0) { i = k; } } - return i; + if (i < 0) + return ItemStack.EMPTY; + else + return stacks.get(i); } } 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 0ba705310..9a57db79a 100644 --- a/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java +++ b/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java @@ -241,4 +241,12 @@ public class ItemHelper { return extracting; } + public static ItemStack findFirstMatch(IItemHandler inv, Predicate test) { + for (int i = 0; i < inv.getSlots(); i++) { + ItemStack toTest = inv.getStackInSlot(i); + if (test.test(toTest)) + return toTest; + } + return ItemStack.EMPTY; + } }