From 01ad24cc880b3922e5ee7e20db358c13e36ae5f9 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 27 Feb 2023 03:45:02 +0100 Subject: [PATCH] feat: added Setting builder --- src/main/java/ftbsc/bscv/tools/Setting.java | 207 ++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 src/main/java/ftbsc/bscv/tools/Setting.java diff --git a/src/main/java/ftbsc/bscv/tools/Setting.java b/src/main/java/ftbsc/bscv/tools/Setting.java new file mode 100644 index 0000000..7531e81 --- /dev/null +++ b/src/main/java/ftbsc/bscv/tools/Setting.java @@ -0,0 +1,207 @@ +package ftbsc.bscv.tools; + +import static ftbsc.bscv.BoSCoVicino.log; + +import java.util.Optional; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.BoolArgumentType; +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; + +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.server.command.EnumArgument; + +public abstract class Setting { + + protected Optional name; + protected Optional comment; + protected Optional fallback; + + + Setting() { + this.name = Optional.empty(); + this.comment = Optional.empty(); + this.fallback = Optional.empty(); + } + + public Setting name(String name) { + this.name = Optional.of(name); + return this; + } + + public Setting comment(String comment) { + this.comment = Optional.of(comment); + return this; + } + + public Setting fallback(T fallback) { + this.fallback = Optional.of(fallback); + return this; + } + + abstract ForgeConfigSpec.ConfigValue value(ForgeConfigSpec.Builder builder); + + abstract ArgumentType argument(); + + abstract Class clazz(); + + public ForgeConfigSpec.ConfigValue build(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { + ForgeConfigSpec.ConfigValue conf = this.value(builder); + + dispatcher.register( + Commands.literal(this.name.get().toLowerCase()) + .then( + Commands.literal(this.name.get()) + .then( + Commands.argument(this.name.get(), this.argument()) + .executes( ctx -> { + T value = ctx.getArgument(this.name.get(), this.clazz()); + conf.set(value); + conf.save(); + log(String.format("> %s -> %s <", String.join(".", conf.getPath()), conf.get().toString())); + return 1; + })) + .executes(ctx -> { + log(String.format("> %s: %s <", name, conf.get().toString())); + return 1; + }) + ) + ); + + return conf; + + } + + public static class Bool extends Setting { + public static Bool builder() { return new Bool(); } + + public Class clazz() { return Boolean.class; } + public BoolArgumentType argument() { return BoolArgumentType.bool(); } + + public ForgeConfigSpec.BooleanValue value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .define(this.name.get(), this.fallback.get() ? true : false); + } + } + + + public static class Decimal extends Setting { + protected Optional min; + protected Optional max; + + public static Decimal builder() { return new Decimal(); } + public Decimal() { + super(); + this.min = Optional.of(Double.MIN_VALUE); + this.max = Optional.of(Double.MAX_VALUE); + } + + public Decimal min(double min) { + this.min = Optional.of(min); + return this; + } + + public Decimal max(double max) { + this.max = Optional.of(max); + return this; + } + + public Class clazz() { return Double.class; } + public DoubleArgumentType argument() { return DoubleArgumentType.doubleArg(this.min.get(), this.max.get()); } + + public ForgeConfigSpec.DoubleValue value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .defineInRange(this.name.get(), this.fallback.get(), this.min.get(), this.max.get()); + } + } + + + public static class Number extends Setting { + protected Optional min; + protected Optional max; + + public static Number builder() { return new Number(); } + public Number() { + super(); + this.min = Optional.of(Integer.MIN_VALUE); + this.max = Optional.of(Integer.MAX_VALUE); + } + + public Number min(int min) { + this.min = Optional.of(min); + return this; + } + + public Number max(int max) { + this.max = Optional.of(max); + return this; + } + + public Class clazz() { return Integer.class; } + public IntegerArgumentType argument() { return IntegerArgumentType.integer(this.min.get(), this.max.get()); } + + public ForgeConfigSpec.IntValue value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .defineInRange(this.name.get(), this.fallback.get(), this.min.get(), this.max.get()); + } + } + + + public static class Str extends Setting { + public static Str builder() { return new Str(); } + + protected boolean greedy = false; + + public Str greedy(boolean greedy) { + this.greedy = greedy; + return this; + } + + public Class clazz() { return String.class; } + public StringArgumentType argument() { + return this.greedy ? + StringArgumentType.greedyString() + : StringArgumentType.string(); + } + + public ForgeConfigSpec.ConfigValue value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .define(this.name.get(), this.fallback.get()); + } + } + + + public static class Switch> extends Setting { + private final Class enumClazz; + + public static> Switch builder(Class type) { return new Switch(type); } + + protected Switch(Class type) { + this.enumClazz = type; + } + + public Class clazz() { + return this.enumClazz; + } + + public EnumArgument argument() { + return EnumArgument.enumArgument(this.enumClazz); + } + + public ForgeConfigSpec.EnumValue value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .defineEnum(this.name.get(), this.fallback.get()); + } + } + +}