From f6ca9297dd8c672bfbae0981df0c61610a61d2ba Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 27 Nov 2024 21:25:45 +0100 Subject: [PATCH] store craft ingredient requests bundled together --- .../de/ellpeck/prettypipes/pipe/PipeBlock.java | 6 ++++-- .../ellpeck/prettypipes/pipe/PipeBlockEntity.java | 8 ++++---- .../pipe/modules/craft/CraftingModuleItem.java | 15 ++++++++++----- .../terminal/ItemTerminalBlockEntity.java | 6 ++++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java index 171dab5..0a0e769 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java @@ -270,8 +270,10 @@ public class PipeBlock extends BaseEntityBlock implements SimpleWaterloggedBlock network.onPipeChanged(pos, state); if (worldIn.getBlockEntity(pos) instanceof PipeBlockEntity pipe) { pipe.getItems().clear(); - for (var lock : pipe.craftIngredientRequests) - network.resolveNetworkLock(lock.getRight()); + for (var locks : pipe.craftIngredientRequests) { + for (var lock : locks.getRight()) + network.resolveNetworkLock(lock); + } } super.onRemove(state, worldIn, pos, newState, isMoving); } diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java index 24d088d..e499af9 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlockEntity.java @@ -71,8 +71,8 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC PipeBlockEntity.this.setChanged(); } }; - // crafting module slot, ingredient request network lock - public final List> craftIngredientRequests = new ArrayList<>(); + // crafting module slot, ingredient request network locks (one list for each recipe) + public final List>> craftIngredientRequests = new ArrayList<>(); // crafting module slot, destination pipe for the result, result item public final List> craftResultRequests = new ArrayList<>(); public PressurizerBlockEntity pressurizer; @@ -105,7 +105,7 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC for (var tuple : this.craftIngredientRequests) { var nbt = new CompoundTag(); nbt.putInt("module_slot", tuple.getLeft()); - nbt.put("lock", tuple.getRight().serializeNBT(provider)); + nbt.put("locks", Utility.serializeAll(provider, tuple.getRight())); requests.add(nbt); } compound.put("craft_requests", requests); @@ -133,7 +133,7 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC var nbt = requests.getCompound(i); this.craftIngredientRequests.add(Pair.of( nbt.getInt("module_slot"), - new NetworkLock(provider, nbt.getCompound("lock")))); + Utility.deserializeAll(nbt.getList("locks", Tag.TAG_COMPOUND), c -> new NetworkLock(provider, c)))); } this.craftResultRequests.clear(); var results = compound.getList("craft_results", Tag.TAG_COMPOUND); diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java index 3652380..ca27bdf 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java @@ -76,7 +76,8 @@ public class CraftingModuleItem extends ModuleItem { network.startProfile("crafting_ingredients"); var request = tile.craftIngredientRequests.getFirst(); if (request.getLeft() == slot) { - var lock = request.getRight(); + var locks = request.getRight(); + var lock = locks.getFirst(); var equalityTypes = ItemFilter.getEqualityTypes(tile); var dest = tile.getAvailableDestination(Direction.values(), lock.stack, true, true); if (dest != null) { @@ -85,7 +86,7 @@ public class CraftingModuleItem extends ModuleItem { if (!ensureItemOrder || network.getPipeItemsOnTheWay(dest.getLeft()).findAny().isEmpty()) { var requestRemain = network.requestExistingItem(lock.location, tile.getBlockPos(), dest.getLeft(), lock, dest.getRight(), equalityTypes); network.resolveNetworkLock(lock); - tile.craftIngredientRequests.remove(request); + locks.remove(lock); // if we couldn't fit all items into the destination, create another request for the rest var remain = lock.stack.copy(); @@ -94,9 +95,12 @@ public class CraftingModuleItem extends ModuleItem { var remainRequest = new NetworkLock(lock.location, remain); // if we're ensuring item order, we need to insert the remaining request at the start so that it gets processed first var index = ensureItemOrder ? 0 : tile.craftResultRequests.size(); - tile.craftIngredientRequests.add(index, Pair.of(slot, remainRequest)); + locks.add(index, remainRequest); network.createNetworkLock(remainRequest); } + + if (locks.isEmpty()) + tile.craftIngredientRequests.remove(request); } } } @@ -186,6 +190,7 @@ public class CraftingModuleItem extends ModuleItem { var craftableCrafts = Mth.ceil(craftableAmount / (float) resultAmount); var toCraft = Math.min(craftableCrafts, requiredCrafts); + var locks = new ArrayList(); var contents = module.get(Contents.TYPE); // if we're ensuring item order, all items for a single recipe should be sent in order first before starting on the next one! for (var c = contents.ensureItemOrder ? toCraft : 1; c > 0; c--) { @@ -197,10 +202,10 @@ public class CraftingModuleItem extends ModuleItem { if (!contents.ensureItemOrder) copy.setCount(in.getCount() * toCraft); var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes); - for (var lock : ret.getLeft()) - tile.craftIngredientRequests.add(Pair.of(slot, lock)); + locks.addAll(ret.getLeft()); } } + tile.craftIngredientRequests.add(Pair.of(slot, locks)); var remain = stack.copy(); remain.shrink(resultAmount * toCraft); diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java index c02ceea..92f472e 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java @@ -217,8 +217,10 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect for (var craftable : network.getAllCraftables(pipe.getBlockPos())) { var otherPipe = network.getPipe(craftable.getLeft()); if (otherPipe != null) { - for (var lock : otherPipe.craftIngredientRequests) - network.resolveNetworkLock(lock.getRight()); + for (var locks : otherPipe.craftIngredientRequests) { + for (var lock : locks.getRight()) + network.resolveNetworkLock(lock); + } otherPipe.craftIngredientRequests.clear(); otherPipe.craftResultRequests.clear(); }