mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2024-12-29 08:26:37 +01:00
Unstackable items are a thing, i guess
This commit is contained in:
parent
59339a7bcb
commit
3b516f5022
3 changed files with 38 additions and 12 deletions
|
@ -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);
|
||||
|
|
|
@ -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<ItemStack> getStacks(MovementContext context) {
|
||||
private NonNullList<ItemStack> getStacks(MovementContext context) {
|
||||
if (!(context.temporaryData instanceof NonNullList) && context.world instanceof ServerWorld) {
|
||||
NonNullList<ItemStack> stacks = NonNullList.withSize(9, ItemStack.EMPTY);
|
||||
ItemStackHelper.loadAllItems(context.tileData, stacks);
|
||||
|
@ -48,6 +50,21 @@ public class DropperMovementBehaviour extends MovementBehaviour {
|
|||
return (NonNullList<ItemStack>) context.temporaryData;
|
||||
}
|
||||
|
||||
private ArrayList<ItemStack> getUseableStacks(MovementContext context) {
|
||||
ArrayList<ItemStack> 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<ItemStack> 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<ItemStack> stacks = getStacks(context);
|
||||
List<ItemStack> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -241,4 +241,12 @@ public class ItemHelper {
|
|||
return extracting;
|
||||
}
|
||||
|
||||
public static ItemStack findFirstMatch(IItemHandler inv, Predicate<ItemStack> test) {
|
||||
for (int i = 0; i < inv.getSlots(); i++) {
|
||||
ItemStack toTest = inv.getStackInSlot(i);
|
||||
if (test.test(toTest))
|
||||
return toTest;
|
||||
}
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue