Skip to content

Commit 1d6272b

Browse files
committed
fixed recipes with crafting dependencies not waiting for them before emitting a redstone signal
1 parent 1d05d1c commit 1d6272b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/main/java/de/ellpeck/prettypipes/network/ActiveCraft.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ public class ActiveCraft implements INBTSerializable<CompoundTag> {
1717

1818
public BlockPos pipe;
1919
public int moduleSlot;
20-
public List<ItemStack> travelingIngredients = new ArrayList<>();
2120
public List<NetworkLock> ingredientsToRequest;
21+
public List<ItemStack> travelingIngredients;
2222
public BlockPos resultDestPipe;
2323
public ItemStack resultStackRemain;
2424
public boolean inProgress;
2525
// we only remove canceled requests from the queue once their items are fully delivered to the crafting location, so that unfinished recipes don't get stuck in crafters etc.
2626
public boolean canceled;
2727

28-
public ActiveCraft(BlockPos pipe, int moduleSlot, List<NetworkLock> ingredientsToRequest, BlockPos resultDestPipe, ItemStack resultStackRemain) {
28+
public ActiveCraft(BlockPos pipe, int moduleSlot, List<NetworkLock> ingredientsToRequest, List<ItemStack> travelingIngredients, BlockPos resultDestPipe, ItemStack resultStackRemain) {
2929
this.pipe = pipe;
3030
this.moduleSlot = moduleSlot;
3131
this.ingredientsToRequest = ingredientsToRequest;
32+
this.travelingIngredients = travelingIngredients;
3233
this.resultDestPipe = resultDestPipe;
3334
this.resultStackRemain = resultStackRemain;
3435
}

src/main/java/de/ellpeck/prettypipes/pipe/modules/craft/CraftingModuleItem.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,29 @@ public Pair<ItemStack, Collection<ActiveCraft>> craft(ItemStack module, PipeBloc
177177
var allCrafts = new ArrayList<ActiveCraft>();
178178
// if we're ensuring item order, all items for a single recipe should be sent in order first before starting on the next one!
179179
for (var c = contents.ensureItemOrder ? toCraft : 1; c > 0; c--) {
180+
var crafts = new ArrayList<ItemStack>();
180181
var locks = new ArrayList<NetworkLock>();
181182
for (var i = 0; i < contents.input.getSlots(); i++) {
182183
var in = contents.input.getStackInSlot(i);
183184
if (in.isEmpty())
184185
continue;
185-
var copy = in.copy();
186+
var request = in.copy();
186187
if (!contents.ensureItemOrder)
187-
copy.setCount(in.getCount() * toCraft);
188-
var ret = network.requestLocksAndStartCrafting(tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes);
188+
request.setCount(in.getCount() * toCraft);
189+
var ret = network.requestLocksAndStartCrafting(tile.getBlockPos(), items, unavailableConsumer, request, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes);
189190
// set crafting dependencies as in progress immediately so that, when canceling, they don't leave behind half-crafted inbetween dependencies
190191
// TODO to be more optimal, we should really do this when setting the main craft as in progress, but that would require storing references to all of the dependencies
191192
ret.getRight().forEach(a -> a.inProgress = true);
192193
locks.addAll(ret.getLeft());
193194
allCrafts.addAll(ret.getRight());
195+
// the items we started crafting are the ones we didn't request normally (ie ones we didn't create locks for)
196+
var startedCrafting = request.copyWithCount(request.getCount() - ret.getLeft().stream().mapToInt(l -> l.stack.getCount()).sum());
197+
if (!startedCrafting.isEmpty())
198+
crafts.add(startedCrafting);
194199
}
195200
var crafted = contents.ensureItemOrder ? resultAmount : resultAmount * toCraft;
196-
var activeCraft = new ActiveCraft(tile.getBlockPos(), slot, locks, destPipe, stack.copyWithCount(Math.min(crafted, leftOfRequest)));
201+
// items we started craft dependencies for are ones that will be sent to us (so we're waiting for them immediately!)
202+
var activeCraft = new ActiveCraft(tile.getBlockPos(), slot, locks, crafts, destPipe, stack.copyWithCount(Math.min(crafted, leftOfRequest)));
197203
tile.getActiveCrafts().add(activeCraft);
198204
allCrafts.add(activeCraft);
199205
leftOfRequest -= crafted;
@@ -211,7 +217,10 @@ public ItemStack store(ItemStack module, PipeBlockEntity tile, ItemStack stack,
211217
var equalityTypes = ItemFilter.getEqualityTypes(tile);
212218
var allCrafts = tile.getActiveCrafts();
213219
for (var craft : allCrafts.stream().filter(c -> c.moduleSlot == slot && !c.getTravelingIngredient(stack, equalityTypes).isEmpty()).toList()) {
214-
craft.travelingIngredients.remove(craft.getTravelingIngredient(stack, equalityTypes));
220+
var traveling = craft.getTravelingIngredient(stack, equalityTypes);
221+
traveling.shrink(stack.getCount());
222+
if (traveling.isEmpty())
223+
craft.travelingIngredients.remove(traveling);
215224

216225
if (contents.insertSingles) {
217226
var handler = tile.getItemHandler(direction);

0 commit comments

Comments
 (0)