Skip to content

Commit 4eee347

Browse files
authored
Merge pull request #412 from BuddhaBuddy1/main
Backport fix from PR#404 into main (1.20) [Fixes #388]
2 parents f0132c3 + 6c2dd52 commit 4eee347

File tree

1 file changed

+28
-48
lines changed

1 file changed

+28
-48
lines changed

src/main/java/com/buuz135/functionalstorage/block/tile/ItemControllableDrawerTile.java

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ public void initClient() {
4949
super.initClient();
5050
}
5151

52+
private void processUpgrade(Level level, BlockPos pos, ItemStack stack, boolean drawerIsSource) {
53+
Direction direction = UpgradeItem.getDirection(stack);
54+
TileUtil.getTileEntity(level, pos.relative(direction)).ifPresent(blockEntity1 -> {
55+
blockEntity1.getCapability(ForgeCapabilities.ITEM_HANDLER, direction.getOpposite()).ifPresent(otherHandler -> {
56+
IItemHandler source = drawerIsSource ? getStorage() : otherHandler;
57+
IItemHandler destination = drawerIsSource ? otherHandler : getStorage();
58+
for (int sourceSlot = 0; sourceSlot < source.getSlots(); sourceSlot++) {
59+
ItemStack pulledStack = source.extractItem(sourceSlot, FunctionalStorageConfig.UPGRADE_PULL_ITEMS, true);
60+
if (pulledStack.isEmpty())
61+
continue;
62+
for (int destinationSlot = 0; destinationSlot < destination.getSlots(); destinationSlot++) {
63+
if (destination.getStackInSlot(destinationSlot).getCount() >= destination.getSlotLimit(destinationSlot))
64+
continue;
65+
ItemStack remainder = destination.insertItem(destinationSlot, pulledStack, true);
66+
if (remainder.getCount() < pulledStack.getCount()) {
67+
destination.insertItem(destinationSlot, source.extractItem(sourceSlot, pulledStack.getCount() - remainder.getCount(), false), false);
68+
break;
69+
}
70+
}
71+
}
72+
});
73+
});
74+
}
75+
5276
@Override
5377
public void serverTick(Level level, BlockPos pos, BlockState state, T blockEntity) {
5478
super.serverTick(level, pos, state, blockEntity);
@@ -59,54 +83,10 @@ public void serverTick(Level level, BlockPos pos, BlockState state, T blockEntit
5983
ItemStack stack = this.getUtilityUpgrades().getStackInSlot(i);
6084
if (!stack.isEmpty()) {
6185
Item item = stack.getItem();
62-
if (item.equals(FunctionalStorage.PULLING_UPGRADE.get())) {
63-
Direction direction = UpgradeItem.getDirection(stack);
64-
TileUtil.getTileEntity(level, pos.relative(direction)).ifPresent(blockEntity1 -> {
65-
blockEntity1.getCapability(ForgeCapabilities.ITEM_HANDLER, direction.getOpposite()).ifPresent(iItemHandler -> {
66-
for (int otherSlot = 0; otherSlot < iItemHandler.getSlots(); otherSlot++) {
67-
ItemStack pulledStack = iItemHandler.extractItem(otherSlot, FunctionalStorageConfig.UPGRADE_PULL_ITEMS, true);
68-
if (pulledStack.isEmpty()) continue;
69-
boolean hasWorked = false;
70-
for (int ourSlot = 0; ourSlot < this.getStorage().getSlots(); ourSlot++) {
71-
ItemStack simulated = getStorage().insertItem(ourSlot, pulledStack, true);
72-
if (!simulated.equals(pulledStack)) {
73-
ItemStack extracted = iItemHandler.extractItem(otherSlot, pulledStack.getCount() - simulated.getCount(), false);
74-
getStorage().insertItem(ourSlot, extracted, false);
75-
hasWorked = true;
76-
break;
77-
}
78-
}
79-
if (hasWorked) break;
80-
}
81-
});
82-
});
83-
}
84-
if (item.equals(FunctionalStorage.PUSHING_UPGRADE.get())) {
85-
Direction direction = UpgradeItem.getDirection(stack);
86-
TileUtil.getTileEntity(level, pos.relative(direction)).ifPresent(blockEntity1 -> {
87-
blockEntity1.getCapability(ForgeCapabilities.ITEM_HANDLER, direction.getOpposite()).ifPresent(otherHandler -> {
88-
for (int drawerSlot = 0; drawerSlot < getStorage().getSlots(); drawerSlot++) {
89-
ItemStack pulledStack = getStorage().extractItem(drawerSlot, FunctionalStorageConfig.UPGRADE_PUSH_ITEMS, true);
90-
if (pulledStack.isEmpty()) continue;
91-
boolean hasWorked = false;
92-
for (int destinationSlot = 0; destinationSlot < otherHandler.getSlots(); destinationSlot++) {
93-
ItemStack otherHandlerStackInSlot = otherHandler.getStackInSlot(destinationSlot);
94-
if (!otherHandlerStackInSlot.isEmpty() && !ItemStack.isSameItemSameTags(pulledStack, otherHandler.getStackInSlot(destinationSlot)))
95-
continue;
96-
if (otherHandler.getStackInSlot(destinationSlot).getCount() >= otherHandler.getSlotLimit(destinationSlot))
97-
continue;
98-
ItemStack simulated = otherHandler.insertItem(destinationSlot, pulledStack, true);
99-
if (simulated.getCount() <= pulledStack.getCount()) {
100-
otherHandler.insertItem(destinationSlot, getStorage().extractItem(drawerSlot, pulledStack.getCount() - simulated.getCount(), false), false);
101-
hasWorked = true;
102-
break;
103-
}
104-
}
105-
if (hasWorked) break;
106-
}
107-
});
108-
});
109-
}
86+
if (item.equals(FunctionalStorage.PULLING_UPGRADE.get()))
87+
processUpgrade(level, pos, stack, false);
88+
if (item.equals(FunctionalStorage.PUSHING_UPGRADE.get()))
89+
processUpgrade(level, pos, stack, true);
11090
if (item.equals(FunctionalStorage.COLLECTOR_UPGRADE.get())) {
11191
Direction direction = UpgradeItem.getDirection(stack);
11292
AABB box = new AABB(pos.relative(direction));

0 commit comments

Comments
 (0)