Skip to content

Commit

Permalink
fix custom builders, because I found a couple issues (#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Screret <[email protected]>
Co-authored-by: KilaBash <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2023
1 parent 36fb651 commit 7a8187d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,19 +28,25 @@ private static CustomTieredMachineBuilder[] customTiered(String name,
BiFunction<IMachineBlockEntity, Integer, MetaMachine> 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;
}

@SuppressWarnings("unchecked")
public static MachineBuilder<MachineDefinition> 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<IMachineBlockEntity, Integer, MetaMachine>) 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<IMachineBlockEntity, Integer, MetaMachine>) machineFunction, tiers);
} else if (args[0] instanceof BaseFunction machineFunction) {
//builders = customTiered(name, (BiFunction<IMachineBlockEntity, Integer, MetaMachine>) 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public MachineBuilder<D> sidedWorkableCasingRenderer(String basePath, ResourceLo
return this;
}

public MachineBuilder<D> tier(int tier, Consumer<MachineBuilder<D>> consumer) {
public MachineBuilder<D> tier(int tier, BuilderConsumer<D> consumer) {
for (var builder : builders) {
if (builder.tier() == tier) {
consumer.accept(builder);
Expand All @@ -310,7 +310,7 @@ public MachineBuilder<D> tier(int tier, Consumer<MachineBuilder<D>> consumer) {
return this;
}

public MachineBuilder<D> allTiers(BiConsumer<Integer, MachineBuilder<D>> consumer) {
public MachineBuilder<D> allTiers(TieredBuilderConsumer<D> consumer) {
for (var builder : builders) {
consumer.accept(builder.tier(), builder);
}
Expand Down Expand Up @@ -397,4 +397,14 @@ public D register() {
}
};
}

@FunctionalInterface
public interface BuilderConsumer<D extends MachineDefinition> extends Consumer<MachineBuilder<D>> {
void accept(MachineBuilder<D> builder);
}

@FunctionalInterface
public interface TieredBuilderConsumer<D extends MachineDefinition> {
void accept(int tier, MachineBuilder<D> builder);
}
}

0 comments on commit 7a8187d

Please sign in to comment.