fix: 支持样板供应器轮询

This commit is contained in:
C-H716 2025-08-29 19:27:03 +08:00
parent 09d1bf195c
commit 956729412e
8 changed files with 51 additions and 114 deletions

View File

@ -1,15 +0,0 @@
package com.extendedae_plus.mixin.autopattern;
import appeng.api.stacks.AEKey;
import appeng.crafting.CraftingCalculation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(CraftingCalculation.class)
public interface CraftingCalculationAccessor {
@Accessor("output")
AEKey extendedae_plus$getOutput();
@Accessor("requestedAmount")
long extendedae_plus$getRequestedAmount();
}

View File

@ -0,0 +1,12 @@
package com.extendedae_plus.mixin.autopattern;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import java.util.List;
@Mixin(targets = "appeng.me.service.helpers.NetworkCraftingProviders.CraftingProviderList")
public interface CraftingProviderListAccessor {
@Accessor("providers")
List<?> eap$getProviders();
}

View File

@ -16,7 +16,7 @@ public class CraftingServiceGetProvidersMixin {
@ModifyArg(method = "getProviders(Lappeng/api/crafting/IPatternDetails;)Ljava/lang/Iterable;",
at = @At(value = "INVOKE", target = "Lappeng/me/service/helpers/NetworkCraftingProviders;getMediums(Lappeng/api/crafting/IPatternDetails;)Ljava/lang/Iterable;"),
index = 0)
private IPatternDetails extendedae_plus$modifyGetProvidersArg(IPatternDetails original) {
private IPatternDetails eap$modifyGetProvidersArg(IPatternDetails original) {
IPatternDetails base = null;
if (original instanceof ScaledProcessingPattern scaledProcessingPattern) {
base = scaledProcessingPattern.getOriginal();

View File

@ -7,10 +7,6 @@ import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(CraftingTreeNode.class)
public interface CraftingTreeNodeAccessor {
@Accessor("what")
AEKey extendedae_plus$getWhat();
@Accessor("amount")
long extendedae_plus$getAmount();
AEKey eap$getWhat();
}

View File

@ -1,23 +1,25 @@
package com.extendedae_plus.mixin.autopattern;
import appeng.api.crafting.IPatternDetails;
import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.networking.crafting.ICraftingService;
import appeng.api.stacks.AEKey;
import appeng.crafting.CraftBranchFailure;
import appeng.crafting.CraftingCalculation;
import appeng.crafting.CraftingTreeNode;
import appeng.crafting.CraftingTreeProcess;
import appeng.crafting.inv.CraftingSimulationState;
import appeng.crafting.pattern.AEProcessingPattern;
import com.extendedae_plus.util.PatternScaler;
import com.extendedae_plus.util.RequestedAmountHolder;
import appeng.me.service.CraftingService;
import com.extendedae_plus.api.SmartDoublingAwarePattern;
import com.extendedae_plus.content.ScaledProcessingPattern;
import com.extendedae_plus.util.PatternScaler;
import com.extendedae_plus.util.RequestedAmountHolder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import java.util.List;
import java.util.stream.StreamSupport;
import static com.extendedae_plus.util.ExtendedAELogger.LOGGER;
/**
@ -27,14 +29,12 @@ import static com.extendedae_plus.util.ExtendedAELogger.LOGGER;
@Mixin(CraftingTreeProcess.class)
public abstract class CraftingTreeProcessMixin {
@Shadow abstract void request(CraftingSimulationState inv, long times) throws CraftBranchFailure, InterruptedException;
@ModifyVariable(
method = "<init>(Lappeng/api/networking/crafting/ICraftingService;Lappeng/crafting/CraftingCalculation;Lappeng/api/crafting/IPatternDetails;Lappeng/crafting/CraftingTreeNode;)V",
at = @At("HEAD"),
argsOnly = true
)
private static IPatternDetails extendedae_plus$replaceDetailsAtHead(IPatternDetails original, ICraftingService cc, CraftingCalculation job, IPatternDetails details, CraftingTreeNode craftingTreeNode) {
private static IPatternDetails eap$replaceDetailsAtHead(IPatternDetails original, ICraftingService cc, CraftingCalculation job, IPatternDetails details, CraftingTreeNode craftingTreeNode) {
try {
// 若传入的 details 已经是缩放样板且原始样板不允许缩放则直接解包为原始样板
if (details instanceof ScaledProcessingPattern sp) {
@ -52,13 +52,35 @@ public abstract class CraftingTreeProcessMixin {
}
CraftingTreeNodeAccessor parentAcc = (CraftingTreeNodeAccessor) craftingTreeNode;
AEKey parentTarget = parentAcc.extendedae_plus$getWhat();
AEKey parentTarget = parentAcc.eap$getWhat();
long requested = RequestedAmountHolder.get();
// 使用当前线程栈顶的值进行缩放不在此处清理构造完成后应该由调用方的 pop 恢复状态
var scaled = PatternScaler.scale(proc, parentTarget, requested);
CraftingService craftingService = (CraftingService) cc;
Iterable<ICraftingProvider> providers = craftingService.getProviders(original);
// 计算 provider 数量优先使用 mixin accessor 以避免消费迭代器
int size;
if (providers instanceof CraftingProviderListAccessor acc) {
List<?> list = acc.eap$getProviders();
size = list == null ? 0 : list.size();
} else {
// 回退为遍历计数会消费迭代器
size = (int) StreamSupport.stream(providers.spliterator(), false).count();
}
// requested providers 间均分仅在 providers 数量大于1时均分保证整数且总量不少于 requested
long perProvider = requested;
if (size > 1) {
perProvider = requested / size + ((requested % size) == 0 ? 0 : 1);
if (perProvider <= 0) perProvider = 1L;
}
// 使用每-provider 的分配量来缩放样板
var scaled = PatternScaler.scale(proc, parentTarget, perProvider);
return scaled != null ? scaled : original;
} catch (Exception e) {
LOGGER.warn("构建倍增样板出错", e);
e.printStackTrace();
return original;
}
}

View File

@ -21,7 +21,7 @@ public class PatternProviderLogicContainsRedirectMixin {
value = "INVOKE",
target = "Ljava/util/List;contains(Ljava/lang/Object;)Z")
)
private boolean extendedae_plus$patternsContains(List<?> list, Object o) {
private boolean eap$patternsContains(List<?> list, Object o) {
try {
if (o instanceof ScaledProcessingPattern scaled) {
IPatternDetails base = scaled.getOriginal();

View File

@ -1,78 +0,0 @@
package com.extendedae_plus.util;
import java.util.Arrays;
public class ArraySimplifier {
// 计算两个数的GCD using Euclidean algorithm (long版本)
public static long gcd(long a, long b) {
while (b != 0) {
long temp = b;
b = a % b;
a = temp;
}
return a;
}
// 计算整个数组的GCD
public static long findGCD(long[] arr) {
if (arr.length == 0) {
return 0;
}
long result = arr[0];
for (int i = 1; i < arr.length; i++) {
result = gcd(result, arr[i]);
// 如果已经找到GCD为1可以提前终止
if (result == 1) {
break;
}
}
return result;
}
// 简化数组每个元素除以数组的GCD
public static long[] simplifyFraction(long[] arr) {
if (arr.length == 0) {
return new long[0];
}
long gcd = findGCD(arr);
if (gcd == 0) {
// 如果GCD为0所有元素为0返回原数组的副本
return Arrays.copyOf(arr, arr.length);
}
long[] simplified = new long[arr.length];
for (int i = 0; i < arr.length; i++) {
simplified[i] = arr[i] / gcd;
}
return simplified;
}
// 将两个数组合并为一个新数组先放 a 后放 b
public static long[] combine(long[] a, long[] b) {
long[] out = new long[a.length + b.length];
System.arraycopy(a, 0, out, 0, a.length);
System.arraycopy(b, 0, out, a.length, b.length);
return out;
}
// 寻找数组的 GCD遇到 1 则立即返回 1早期退出优化
public static long findGCDWithEarlyExit(long[] arr) {
if (arr.length == 0) return 0;
long result = 0;
for (long v : arr) {
if (v == 1) return 1; // already irreducible
if (v == 0) continue;
if (result == 0) result = v; else result = gcd(result, v);
if (result == 1) return 1;
}
return result == 0 ? 0 : Math.abs(result);
}
// 根据给定的 gcd 返回一个已除以 gcd 的新数组如果 gcd==1 返回原数组避免不必要的分配
public static long[] simplifyByGcd(long[] arr, long gcd) {
if (gcd <= 1) return arr;
long[] out = new long[arr.length];
for (int i = 0; i < arr.length; i++) out[i] = arr[i] / gcd;
return out;
}
}

View File

@ -26,15 +26,15 @@
"jei.EncodePatternTransferHandlerMixin"
],
"mixins": [
"ae2.AEProcessingPatternMixin",
"ae2.ContainerPatternEncodingTermMenuMixin",
"ae2.CraftingCPUClusterMixin",
"ae2.MEStorageMenuMixin",
"ae2.PatternEncodingTermMenuMixin",
"ae2.PatternProviderLogicAdvancedMixin",
"ae2.PatternProviderMenuAdvancedMixin",
"ae2.PatternProviderLogicDoublingMixin",
"ae2.PatternProviderMenuAdvancedMixin",
"ae2.PatternProviderMenuDoublingMixin",
"ae2.AEProcessingPatternMixin",
"ae2.accessor.MEStorageMenuAccessor",
"ae2.accessor.PatternEncodingTermMenuAccessor",
"ae2.accessor.PatternProviderLogicAccessor",
@ -42,7 +42,7 @@
"ae2.accessor.PatternProviderLogicPatternsAccessor",
"ae2.accessor.PatternProviderMenuAdvancedAccessor",
"ae2WTlib.ContainerUWirelessExPatternTerminalMixin",
"autopattern.CraftingCalculationAccessor",
"autopattern.CraftingProviderListAccessor",
"autopattern.CraftingServiceGetProvidersMixin",
"autopattern.CraftingTreeNodeAccessor",
"autopattern.CraftingTreeNodeMixin",