fix: 支持样板供应器轮询
This commit is contained in:
parent
09d1bf195c
commit
956729412e
|
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ public class CraftingServiceGetProvidersMixin {
|
||||||
@ModifyArg(method = "getProviders(Lappeng/api/crafting/IPatternDetails;)Ljava/lang/Iterable;",
|
@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;"),
|
at = @At(value = "INVOKE", target = "Lappeng/me/service/helpers/NetworkCraftingProviders;getMediums(Lappeng/api/crafting/IPatternDetails;)Ljava/lang/Iterable;"),
|
||||||
index = 0)
|
index = 0)
|
||||||
private IPatternDetails extendedae_plus$modifyGetProvidersArg(IPatternDetails original) {
|
private IPatternDetails eap$modifyGetProvidersArg(IPatternDetails original) {
|
||||||
IPatternDetails base = null;
|
IPatternDetails base = null;
|
||||||
if (original instanceof ScaledProcessingPattern scaledProcessingPattern) {
|
if (original instanceof ScaledProcessingPattern scaledProcessingPattern) {
|
||||||
base = scaledProcessingPattern.getOriginal();
|
base = scaledProcessingPattern.getOriginal();
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,6 @@ import org.spongepowered.asm.mixin.gen.Accessor;
|
||||||
|
|
||||||
@Mixin(CraftingTreeNode.class)
|
@Mixin(CraftingTreeNode.class)
|
||||||
public interface CraftingTreeNodeAccessor {
|
public interface CraftingTreeNodeAccessor {
|
||||||
|
|
||||||
@Accessor("what")
|
@Accessor("what")
|
||||||
AEKey extendedae_plus$getWhat();
|
AEKey eap$getWhat();
|
||||||
|
|
||||||
@Accessor("amount")
|
|
||||||
long extendedae_plus$getAmount();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,25 @@
|
||||||
package com.extendedae_plus.mixin.autopattern;
|
package com.extendedae_plus.mixin.autopattern;
|
||||||
|
|
||||||
import appeng.api.crafting.IPatternDetails;
|
import appeng.api.crafting.IPatternDetails;
|
||||||
|
import appeng.api.networking.crafting.ICraftingProvider;
|
||||||
import appeng.api.networking.crafting.ICraftingService;
|
import appeng.api.networking.crafting.ICraftingService;
|
||||||
import appeng.api.stacks.AEKey;
|
import appeng.api.stacks.AEKey;
|
||||||
import appeng.crafting.CraftBranchFailure;
|
|
||||||
import appeng.crafting.CraftingCalculation;
|
import appeng.crafting.CraftingCalculation;
|
||||||
import appeng.crafting.CraftingTreeNode;
|
import appeng.crafting.CraftingTreeNode;
|
||||||
import appeng.crafting.CraftingTreeProcess;
|
import appeng.crafting.CraftingTreeProcess;
|
||||||
import appeng.crafting.inv.CraftingSimulationState;
|
|
||||||
import appeng.crafting.pattern.AEProcessingPattern;
|
import appeng.crafting.pattern.AEProcessingPattern;
|
||||||
import com.extendedae_plus.util.PatternScaler;
|
import appeng.me.service.CraftingService;
|
||||||
import com.extendedae_plus.util.RequestedAmountHolder;
|
|
||||||
import com.extendedae_plus.api.SmartDoublingAwarePattern;
|
import com.extendedae_plus.api.SmartDoublingAwarePattern;
|
||||||
import com.extendedae_plus.content.ScaledProcessingPattern;
|
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.Mixin;
|
||||||
import org.spongepowered.asm.mixin.Shadow;
|
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import static com.extendedae_plus.util.ExtendedAELogger.LOGGER;
|
import static com.extendedae_plus.util.ExtendedAELogger.LOGGER;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -27,14 +29,12 @@ import static com.extendedae_plus.util.ExtendedAELogger.LOGGER;
|
||||||
@Mixin(CraftingTreeProcess.class)
|
@Mixin(CraftingTreeProcess.class)
|
||||||
public abstract class CraftingTreeProcessMixin {
|
public abstract class CraftingTreeProcessMixin {
|
||||||
|
|
||||||
@Shadow abstract void request(CraftingSimulationState inv, long times) throws CraftBranchFailure, InterruptedException;
|
|
||||||
|
|
||||||
@ModifyVariable(
|
@ModifyVariable(
|
||||||
method = "<init>(Lappeng/api/networking/crafting/ICraftingService;Lappeng/crafting/CraftingCalculation;Lappeng/api/crafting/IPatternDetails;Lappeng/crafting/CraftingTreeNode;)V",
|
method = "<init>(Lappeng/api/networking/crafting/ICraftingService;Lappeng/crafting/CraftingCalculation;Lappeng/api/crafting/IPatternDetails;Lappeng/crafting/CraftingTreeNode;)V",
|
||||||
at = @At("HEAD"),
|
at = @At("HEAD"),
|
||||||
argsOnly = true
|
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 {
|
try {
|
||||||
// 若传入的 details 已经是缩放样板,且原始样板不允许缩放,则直接解包为原始样板
|
// 若传入的 details 已经是缩放样板,且原始样板不允许缩放,则直接解包为原始样板
|
||||||
if (details instanceof ScaledProcessingPattern sp) {
|
if (details instanceof ScaledProcessingPattern sp) {
|
||||||
|
|
@ -52,13 +52,35 @@ public abstract class CraftingTreeProcessMixin {
|
||||||
}
|
}
|
||||||
|
|
||||||
CraftingTreeNodeAccessor parentAcc = (CraftingTreeNodeAccessor) craftingTreeNode;
|
CraftingTreeNodeAccessor parentAcc = (CraftingTreeNodeAccessor) craftingTreeNode;
|
||||||
AEKey parentTarget = parentAcc.extendedae_plus$getWhat();
|
AEKey parentTarget = parentAcc.eap$getWhat();
|
||||||
long requested = RequestedAmountHolder.get();
|
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;
|
return scaled != null ? scaled : original;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.warn("构建倍增样板出错", e);
|
LOGGER.warn("构建倍增样板出错", e);
|
||||||
|
e.printStackTrace();
|
||||||
return original;
|
return original;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ public class PatternProviderLogicContainsRedirectMixin {
|
||||||
value = "INVOKE",
|
value = "INVOKE",
|
||||||
target = "Ljava/util/List;contains(Ljava/lang/Object;)Z")
|
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 {
|
try {
|
||||||
if (o instanceof ScaledProcessingPattern scaled) {
|
if (o instanceof ScaledProcessingPattern scaled) {
|
||||||
IPatternDetails base = scaled.getOriginal();
|
IPatternDetails base = scaled.getOriginal();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -26,15 +26,15 @@
|
||||||
"jei.EncodePatternTransferHandlerMixin"
|
"jei.EncodePatternTransferHandlerMixin"
|
||||||
],
|
],
|
||||||
"mixins": [
|
"mixins": [
|
||||||
|
"ae2.AEProcessingPatternMixin",
|
||||||
"ae2.ContainerPatternEncodingTermMenuMixin",
|
"ae2.ContainerPatternEncodingTermMenuMixin",
|
||||||
"ae2.CraftingCPUClusterMixin",
|
"ae2.CraftingCPUClusterMixin",
|
||||||
"ae2.MEStorageMenuMixin",
|
"ae2.MEStorageMenuMixin",
|
||||||
"ae2.PatternEncodingTermMenuMixin",
|
"ae2.PatternEncodingTermMenuMixin",
|
||||||
"ae2.PatternProviderLogicAdvancedMixin",
|
"ae2.PatternProviderLogicAdvancedMixin",
|
||||||
"ae2.PatternProviderMenuAdvancedMixin",
|
|
||||||
"ae2.PatternProviderLogicDoublingMixin",
|
"ae2.PatternProviderLogicDoublingMixin",
|
||||||
|
"ae2.PatternProviderMenuAdvancedMixin",
|
||||||
"ae2.PatternProviderMenuDoublingMixin",
|
"ae2.PatternProviderMenuDoublingMixin",
|
||||||
"ae2.AEProcessingPatternMixin",
|
|
||||||
"ae2.accessor.MEStorageMenuAccessor",
|
"ae2.accessor.MEStorageMenuAccessor",
|
||||||
"ae2.accessor.PatternEncodingTermMenuAccessor",
|
"ae2.accessor.PatternEncodingTermMenuAccessor",
|
||||||
"ae2.accessor.PatternProviderLogicAccessor",
|
"ae2.accessor.PatternProviderLogicAccessor",
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
"ae2.accessor.PatternProviderLogicPatternsAccessor",
|
"ae2.accessor.PatternProviderLogicPatternsAccessor",
|
||||||
"ae2.accessor.PatternProviderMenuAdvancedAccessor",
|
"ae2.accessor.PatternProviderMenuAdvancedAccessor",
|
||||||
"ae2WTlib.ContainerUWirelessExPatternTerminalMixin",
|
"ae2WTlib.ContainerUWirelessExPatternTerminalMixin",
|
||||||
"autopattern.CraftingCalculationAccessor",
|
"autopattern.CraftingProviderListAccessor",
|
||||||
"autopattern.CraftingServiceGetProvidersMixin",
|
"autopattern.CraftingServiceGetProvidersMixin",
|
||||||
"autopattern.CraftingTreeNodeAccessor",
|
"autopattern.CraftingTreeNodeAccessor",
|
||||||
"autopattern.CraftingTreeNodeMixin",
|
"autopattern.CraftingTreeNodeMixin",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user