From 7a8187da06fb8baa47e2b1cfa58d4e01a4e21350 Mon Sep 17 00:00:00 2001 From: screret <68943070+screret@users.noreply.github.com> Date: Mon, 24 Jul 2023 14:23:04 +0300 Subject: [PATCH] fix custom builders, because I found a couple issues (#195) Co-authored-by: Screret Co-authored-by: KilaBash --- .../machine/CustomTieredMachineBuilder.java | 19 ++++++++++++++----- .../machine/MachineFunctionPresets.java | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/CustomTieredMachineBuilder.java b/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/CustomTieredMachineBuilder.java index df44f12749..9fc928eeea 100644 --- a/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/CustomTieredMachineBuilder.java +++ b/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/CustomTieredMachineBuilder.java @@ -7,6 +7,9 @@ import com.gregtechceu.gtceu.api.machine.MachineDefinition; import com.gregtechceu.gtceu.api.machine.MetaMachine; import com.gregtechceu.gtceu.api.registry.registrate.MachineBuilder; +import dev.latvian.mods.kubejs.script.ScriptType; +import dev.latvian.mods.kubejs.util.UtilsJS; +import dev.latvian.mods.rhino.BaseFunction; import java.util.function.BiFunction; import java.util.function.Function; @@ -25,9 +28,10 @@ private static CustomTieredMachineBuilder[] customTiered(String name, BiFunction machineConstructor, Integer... tiers) { CustomTieredMachineBuilder[] builders = new CustomTieredMachineBuilder[tiers.length]; - for (int tier : tiers) { + for (int i = 0; i < tiers.length; i++) { + int tier = tiers[i]; CustomTieredMachineBuilder register = new CustomTieredMachineBuilder(GTValues.VN[tier].toLowerCase() + "_" + name, holder -> machineConstructor.apply(holder, tier)).tier(tier); - builders[tier] = register; + builders[i] = register; } return builders; } @@ -35,9 +39,14 @@ private static CustomTieredMachineBuilder[] customTiered(String name, @SuppressWarnings("unchecked") public static MachineBuilder createAll(String name, Object... args) { CustomTieredMachineBuilder[] builders = new CustomTieredMachineBuilder[0]; - if (args.length > 1 && args[1] instanceof BiFunction machineFunction) { - Object[] tiers = MachineFunctionPresets.copyArgs(args, 1); - builders = customTiered(name, (BiFunction) machineFunction, MachineFunctionPresets.mapTierArray(tiers)); + if (args.length > 1) { + Integer[] tiers = MachineFunctionPresets.mapTierArray(MachineFunctionPresets.copyArgs(args, 1)); + if (args[0] instanceof BiFunction machineFunction) { + builders = customTiered(name, (BiFunction) machineFunction, tiers); + } else if (args[0] instanceof BaseFunction machineFunction) { + //builders = customTiered(name, (BiFunction) NativeJavaObject.createInterfaceAdapter(ScriptType.STARTUP.manager.get().context, BiFunction.class, machineFunction), MachineFunctionPresets.mapTierArray(tiers)); + builders = customTiered(name, UtilsJS.makeFunctionProxy(ScriptType.STARTUP, BiFunction.class, machineFunction), tiers); + } } return MachineFunctionPresets.builder(name, builders, CustomTieredMachineBuilder.class, MachineDefinition::createDefinition, MetaMachineBlock::new, MetaMachineBlockEntity::createBlockEntity); } diff --git a/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/MachineFunctionPresets.java b/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/MachineFunctionPresets.java index 6094a1eff6..3d8e983e5c 100644 --- a/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/MachineFunctionPresets.java +++ b/common/src/main/java/com/gregtechceu/gtceu/integration/kjs/builders/machine/MachineFunctionPresets.java @@ -301,7 +301,7 @@ public MachineBuilder sidedWorkableCasingRenderer(String basePath, ResourceLo return this; } - public MachineBuilder tier(int tier, Consumer> consumer) { + public MachineBuilder tier(int tier, BuilderConsumer consumer) { for (var builder : builders) { if (builder.tier() == tier) { consumer.accept(builder); @@ -310,7 +310,7 @@ public MachineBuilder tier(int tier, Consumer> consumer) { return this; } - public MachineBuilder allTiers(BiConsumer> consumer) { + public MachineBuilder allTiers(TieredBuilderConsumer consumer) { for (var builder : builders) { consumer.accept(builder.tier(), builder); } @@ -397,4 +397,14 @@ public D register() { } }; } + + @FunctionalInterface + public interface BuilderConsumer extends Consumer> { + void accept(MachineBuilder builder); + } + + @FunctionalInterface + public interface TieredBuilderConsumer { + void accept(int tier, MachineBuilder builder); + } }