diff options
-rw-r--r-- | src/main/java/ftbsc/bscv/tools/Setting.java | 207 |
1 files changed, 207 insertions, 0 deletions
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<T> { + + protected Optional<String> name; + protected Optional<String> comment; + protected Optional<T> fallback; + + + Setting() { + this.name = Optional.empty(); + this.comment = Optional.empty(); + this.fallback = Optional.empty(); + } + + public Setting<T> name(String name) { + this.name = Optional.of(name); + return this; + } + + public Setting<T> comment(String comment) { + this.comment = Optional.of(comment); + return this; + } + + public Setting<T> fallback(T fallback) { + this.fallback = Optional.of(fallback); + return this; + } + + abstract ForgeConfigSpec.ConfigValue<T> value(ForgeConfigSpec.Builder builder); + + abstract ArgumentType<T> argument(); + + abstract Class<T> clazz(); + + public ForgeConfigSpec.ConfigValue<T> build(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { + ForgeConfigSpec.ConfigValue<T> 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<Boolean> { + public static Bool builder() { return new Bool(); } + + public Class<Boolean> 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<Double> { + protected Optional<Double> min; + protected Optional<Double> 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<Double> 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<Integer> { + protected Optional<Integer> min; + protected Optional<Integer> 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<Integer> 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<String> { + public static Str builder() { return new Str(); } + + protected boolean greedy = false; + + public Str greedy(boolean greedy) { + this.greedy = greedy; + return this; + } + + public Class<String> clazz() { return String.class; } + public StringArgumentType argument() { + return this.greedy ? + StringArgumentType.greedyString() + : StringArgumentType.string(); + } + + public ForgeConfigSpec.ConfigValue<String> value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .define(this.name.get(), this.fallback.get()); + } + } + + + public static class Switch<T extends Enum<T>> extends Setting<T> { + private final Class<T> enumClazz; + + public static<T extends Enum<T>> Switch<T> builder(Class<T> type) { return new Switch<T>(type); } + + protected Switch(Class<T> type) { + this.enumClazz = type; + } + + public Class<T> clazz() { + return this.enumClazz; + } + + public EnumArgument<T> argument() { + return EnumArgument.enumArgument(this.enumClazz); + } + + public ForgeConfigSpec.EnumValue<T> value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .defineEnum(this.name.get(), this.fallback.get()); + } + } + +} |