fix Pattern with recycling ingredient broken (#1230)

Co-authored-by: Kogepan229 <kogepan25629@gmail.com>
Co-authored-by: Martin Robertz <dream-master@gmx.net>
This commit is contained in:
authored and GitHub committed 2026-04-25 10:54:47 +00:00
1 parent e12daaf039
commit ac4e32aab4
1 file changed
+96 -17
@@ -359,11 +359,16 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
if (is != null && is.getStackSize() > 0) {
if (is.getStackSize() >= what.getStackSize()) {
if (this.finalOutput.isFinalOutput(what)) {
if (this.myLastLink != null) {
return ((CraftingLink) this.myLastLink).injectItems(what.copy(), type);
final IAEStack<?> outputToSend = this.finalOutput.splitOutputToIngredient(what, type);
if (outputToSend == null) {
return null;
}
return what; // ignore it.
if (this.myLastLink != null) {
return ((CraftingLink) this.myLastLink).injectItems(outputToSend.copy(), type);
}
return outputToSend; // ignore it.
}
return null;
@@ -375,13 +380,24 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
final IAEStack<?> used = what.copy();
used.setStackSize(is.getStackSize());
if (this.finalOutput.isFinalOutput(what)) {
if (this.myLastLink != null) {
leftOver.add(((CraftingLink) this.myLastLink).injectItems(used.copy(), type));
if (this.finalOutput.isFinalOutput(used)) {
final IAEStack<?> outputToSend = this.finalOutput.splitOutputToIngredient(used, type);
if (outputToSend == null) {
return leftOver;
}
return what; // ignore it.
if (this.myLastLink != null) {
final IAEStack<?> linkLeftOver = ((CraftingLink) this.myLastLink)
.injectItems(outputToSend.copy(), type);
if (linkLeftOver != null) {
leftOver.add(linkLeftOver);
}
return leftOver;
}
leftOver.add(outputToSend);
return leftOver; // ignore it.
}
return leftOver;
@@ -405,13 +421,16 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
}
if (this.finalOutput.isFinalOutput(what)) {
IAEStack<?> leftover = what;
final IAEStack<?> outputToSend = this.finalOutput.splitOutputToIngredient(what, type);
IAEStack<?> leftover = outputToSend;
IAEStack<?> finalOutput = this.finalOutput.findPrecise(what);
finalOutput.decStackSize(what.getStackSize());
if (outputToSend != null) {
finalOutput.decStackSize(outputToSend.getStackSize());
}
if (this.myLastLink != null) {
leftover = ((CraftingLink) this.myLastLink).injectItems(what, type);
if (outputToSend != null && this.myLastLink != null) {
leftover = ((CraftingLink) this.myLastLink).injectItems(outputToSend, type);
}
if (this.finalOutput.isEmpty()) {
@@ -436,14 +455,24 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
if (ism != null) ism.setStackSize(0);
if (this.finalOutput.isFinalOutput(insert)) {
IAEStack<?> leftover = input;
IAEStack<?> finalOutput = this.finalOutput.findPrecise(what);
final IAEStack<?> outputToSend = this.finalOutput.splitOutputToIngredient(insert, type);
IAEStack<?> leftover = what;
IAEStack<?> finalOutput = this.finalOutput.findPrecise(insert);
finalOutput.decStackSize(insert.getStackSize());
if (outputToSend != null) {
finalOutput.decStackSize(outputToSend.getStackSize());
}
if (this.myLastLink != null) {
what.add(((CraftingLink) this.myLastLink).injectItems(insert.copy(), type));
leftover = what;
if (outputToSend != null) {
if (this.myLastLink != null) {
final IAEStack<?> linkLeftOver = ((CraftingLink) this.myLastLink)
.injectItems(outputToSend.copy(), type);
if (linkLeftOver != null) {
what.add(linkLeftOver);
}
} else {
what.add(outputToSend);
}
}
if (this.finalOutput.isEmpty()) {
@@ -2073,6 +2102,56 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU {
return this.outputs.findPrecise(aes) != null;
}
/**
* Reserves the portion of a final output that is still needed as an ingredient, returning only the surplus.
*/
private IAEStack<?> splitOutputToIngredient(final IAEStack<?> output, final Actionable type) {
final long ingredientAmount = this.getRemainingIngredientAmount(output);
if (ingredientAmount <= 0) {
return output;
}
final IAEStack<?> ingredientOutput = output.copy();
ingredientOutput.setStackSize(ingredientAmount);
if (type == Actionable.MODULATE) {
inventory.injectItems(ingredientOutput, Actionable.MODULATE);
}
if (ingredientAmount >= output.getStackSize()) {
return null;
}
final IAEStack<?> remainingOutput = output.copy();
remainingOutput.decStackSize(ingredientAmount);
return remainingOutput;
}
/**
* Calculates how many items of this output are still required by pending crafting tasks.
*/
private long getRemainingIngredientAmount(final IAEStack<?> output) {
long required = 0;
for (Entry<ICraftingPatternDetails, TaskProgress> e : tasks.entrySet()) {
if (e.getValue().value <= 0) {
continue;
}
for (final IAEStack<?> aes : e.getKey().getCondensedAEInputs()) {
if (aes.equals(output)) {
required += aes.getStackSize() * e.getValue().value;
}
}
}
@SuppressWarnings("rawtypes, unchecked")
final IAEStack available = inventory.findPrecise((IAEStack) output);
if (available != null) {
required -= available.getStackSize();
}
return Math.max(0, Math.min(required, output.getStackSize()));
}
public IAEStack<?> findPrecise(IAEStack<?> aes) {
return this.outputs.findPrecise(aes);
}