|
| 1 | +package com.ref.aea.api.mixin.ae.crafting.color; |
| 2 | + |
| 3 | +import appeng.api.crafting.IPatternDetails; |
| 4 | +import appeng.api.stacks.AEKey; |
| 5 | +import appeng.api.stacks.GenericStack; |
| 6 | +import appeng.api.stacks.KeyCounter; |
| 7 | +import com.ref.aea.core.color.AEANetworkCraftingProviders; |
| 8 | +import java.util.*; |
| 9 | +import org.jetbrains.annotations.Nullable; |
| 10 | + |
| 11 | +public record CraftingPlanCompressedRing( |
| 12 | + KeyCounter netInputs, |
| 13 | + KeyCounter netOutputs, |
| 14 | + KeyCounter catalysts, |
| 15 | + Map<IPatternDetails, Integer> executionRatio, |
| 16 | + Set<AEKey> entryPoints, |
| 17 | + boolean isCalculable) { |
| 18 | + |
| 19 | + /** 计算并缓存环的属性。 假设:默认环已经被玩家配平(各模式执行比例为 1:1)。 */ |
| 20 | + @Nullable |
| 21 | + public static CraftingPlanCompressedRing calculateAndCacheRingProperties( |
| 22 | + int color, AEANetworkCraftingProviders providers) { |
| 23 | + |
| 24 | + var patternsInRing = providers.getPatternsByColor(color); |
| 25 | + if (patternsInRing == null || patternsInRing.isEmpty()) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + KeyCounter totalInputs = new KeyCounter(); |
| 30 | + KeyCounter totalOutputs = new KeyCounter(); |
| 31 | + |
| 32 | + Map<IPatternDetails, Integer> executionRatio = new HashMap<>(patternsInRing.size()); |
| 33 | + |
| 34 | + for (IPatternDetails pattern : patternsInRing) { |
| 35 | + executionRatio.put(pattern, 1); |
| 36 | + |
| 37 | + for (GenericStack output : pattern.getOutputs()) { |
| 38 | + if (output != null && output.amount() > 0) { |
| 39 | + totalOutputs.add(output.what(), output.amount()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + for (var input : pattern.getInputs()) { |
| 44 | + GenericStack[] possibleInputs = input.getPossibleInputs(); |
| 45 | + if (possibleInputs != null && possibleInputs.length > 0) { |
| 46 | + GenericStack baseStack = possibleInputs[0]; |
| 47 | + if (baseStack != null && baseStack.amount() > 0) { |
| 48 | + long requiredAmount = baseStack.amount() * input.getMultiplier(); |
| 49 | + totalInputs.add(baseStack.what(), requiredAmount); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + KeyCounter netInputs = new KeyCounter(); |
| 56 | + KeyCounter netOutputs = new KeyCounter(); |
| 57 | + KeyCounter catalysts = new KeyCounter(); |
| 58 | + |
| 59 | + Set<AEKey> allKeys = new HashSet<>(totalInputs.keySet()); |
| 60 | + allKeys.addAll(totalOutputs.keySet()); |
| 61 | + |
| 62 | + for (AEKey key : allKeys) { |
| 63 | + long in = totalInputs.get(key); |
| 64 | + long out = totalOutputs.get(key); |
| 65 | + |
| 66 | + if (in > 0 && out > 0) { |
| 67 | + if (out > in) { |
| 68 | + netOutputs.add(key, out - in); |
| 69 | + catalysts.add(key, in); |
| 70 | + } else if (in > out) { |
| 71 | + netInputs.add(key, in - out); |
| 72 | + } else { |
| 73 | + catalysts.add(key, 1); |
| 74 | + } |
| 75 | + } else if (in > 0) { |
| 76 | + netInputs.add(key, in); |
| 77 | + } else if (out > 0) { |
| 78 | + netOutputs.add(key, out); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Set<AEKey> entryPoints = new HashSet<>(netOutputs.keySet()); |
| 83 | + |
| 84 | + return new CraftingPlanCompressedRing( |
| 85 | + netInputs, netOutputs, catalysts, executionRatio, entryPoints, true); |
| 86 | + } |
| 87 | +} |
0 commit comments