1010import org .cyclops .commoncapabilities .api .ingredient .PrototypedIngredient ;
1111
1212import javax .annotation .Nullable ;
13- import java .util .Collection ;
14- import java .util .List ;
15- import java .util .Map ;
13+ import java .util .*;
1614
1715/**
1816 * @author rubensworks
@@ -141,7 +139,8 @@ public void setError(String unlocalizedError) {
141139 public ITerminalCraftingPlanFlat <I > flatten () {
142140 // Group dependencies by prototype
143141 IndexedEntries indexedEntries = new IndexedEntries ();
144- groupDependenciesByPrototype (indexedEntries , this );
142+ Set <I > handledPlans = new HashSet <>();
143+ groupDependenciesByPrototype (indexedEntries , handledPlans , this );
145144
146145 // Make plan
147146 TerminalCraftingPlanFlatStatic <I > planFlat = new TerminalCraftingPlanFlatStatic <>(
@@ -203,7 +202,13 @@ public Collection<TerminalCraftingPlanFlatStatic.Entry> getEntries() {
203202 }
204203 }
205204
206- protected static <I > void groupDependenciesByPrototype (IndexedEntries indexedEntries , ITerminalCraftingPlan <I > plan ) {
205+ protected static <I > void groupDependenciesByPrototype (IndexedEntries indexedEntries , Set <I > handledPlans , ITerminalCraftingPlan <I > plan ) {
206+ // Since jobs can have multiple dependents due to job splitting, we only consider each job once during flattening.
207+ if (handledPlans .contains (plan .getId ())) {
208+ return ;
209+ }
210+ handledPlans .add (plan .getId ());
211+
207212 // Determine outputs that are invalid or will be crafted
208213 for (IPrototypedIngredient <?, ?> output : plan .getOutputs ()) {
209214 TerminalCraftingPlanFlatStatic .Entry entry = indexedEntries .get (output );
@@ -221,7 +226,6 @@ protected static <I> void groupDependenciesByPrototype(IndexedEntries indexedEnt
221226 if (plan .getStatus () == TerminalCraftingJobStatus .QUEUEING
222227 || plan .getStatus () == TerminalCraftingJobStatus .PENDING_DEPENDENCIES
223228 || plan .getStatus () == TerminalCraftingJobStatus .PENDING_INPUTS
224- || plan .getStatus () == TerminalCraftingJobStatus .CRAFTING
225229 || plan .getStatus () == TerminalCraftingJobStatus .UNSTARTED ) {
226230 entry .setQuantityToCraft (entry .getQuantityToCraft () + quantity );
227231 }
@@ -248,7 +252,7 @@ protected static <I> void groupDependenciesByPrototype(IndexedEntries indexedEnt
248252
249253 // Recurse into dependencies
250254 for (ITerminalCraftingPlan <I > dependency : plan .getDependencies ()) {
251- groupDependenciesByPrototype (indexedEntries , dependency );
255+ groupDependenciesByPrototype (indexedEntries , handledPlans , dependency );
252256 }
253257 }
254258
0 commit comments