diff --git a/src/main/java/ftbsc/bscv/tools/Setting.java b/src/main/java/ftbsc/bscv/tools/Setting.java index 99f5480..427e22e 100644 --- a/src/main/java/ftbsc/bscv/tools/Setting.java +++ b/src/main/java/ftbsc/bscv/tools/Setting.java @@ -1,23 +1,28 @@ package ftbsc.bscv.tools; +import com.google.common.collect.Lists; import com.mojang.brigadier.arguments.*; + import ftbsc.bscv.api.IModule; import net.minecraft.command.Commands; import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.server.command.EnumArgument; +import java.io.Serializable; +import java.util.List; import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; import static ftbsc.bscv.Boscovicino.log; -public abstract class Setting { +public abstract class Setting { protected Optional name; protected Optional comment; protected Optional fallback; - protected Optional> callback; - + protected Optional> callback; Setting() { this.name = Optional.empty(); @@ -26,68 +31,75 @@ public abstract class Setting { this.callback = Optional.empty(); } - public Setting name(String name) { + public Setting name(String name) { this.name = Optional.of(name); return this; } - public Setting comment(String comment) { + public Setting comment(String comment) { this.comment = Optional.of(comment); return this; } - public Setting fallback(T fallback) { + public Setting fallback(T fallback) { this.fallback = Optional.of(fallback); return this; } - public Setting callback(Consumer callback) { + public Setting callback(Consumer callback) { this.callback = Optional.of(callback); return this; } abstract ForgeConfigSpec.ConfigValue value(ForgeConfigSpec.Builder builder); - abstract ArgumentType argument(); + abstract ArgumentType argument(); - abstract Class clazz(); + public abstract ForgeConfigSpec.ConfigValue build(IModule module); - public ForgeConfigSpec.ConfigValue build(IModule module) { - ForgeConfigSpec.ConfigValue conf = this.value(module.getConfigBuilder()); - String optName = this.name.get(); - Class clazz = this.clazz(); - ArgumentType arg = this.argument(); - Consumer cb = this.callback.isPresent() ? this.callback.get() : null; - module.getDispatcher().register( - Commands.literal(module.getName().toLowerCase()) - .then( - Commands.literal(optName) + private static abstract class ValueSetting extends Setting { + + abstract Class clazz(); + + public ForgeConfigSpec.ConfigValue build(IModule module) { + ForgeConfigSpec.ConfigValue conf = this.value(module.getConfigBuilder()); + + String optName = this.name.get(); + Class clazz = this.clazz(); + ArgumentType arg = this.argument(); + Consumer cb = this.callback.isPresent() ? this.callback.get() : null; + + module.getDispatcher().register( + Commands.literal(module.getName().toLowerCase()) .then( - Commands.argument(optName, arg) - .executes( ctx -> { - T value = ctx.getArgument(optName, clazz); - if (cb != null) { - cb.accept(value); - } - 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 <", optName, conf.get().toString())); - return 1; - }) - ) - ); + Commands.literal(optName) + .then( + Commands.argument(optName, arg) + .executes( ctx -> { + T value = ctx.getArgument(optName, clazz); + if (cb != null) { + cb.accept(value); + } + 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 <", optName, conf.get().toString())); + return 1; + }) + ) + ); - return conf; + return conf; + } } - public static class Bool extends Setting { + public static class Bool extends ValueSetting { public static Bool builder() { return new Bool(); } public Class clazz() { return Boolean.class; } @@ -101,7 +113,7 @@ public abstract class Setting { } - public static class Decimal extends Setting { + public static class Decimal extends ValueSetting { protected Optional min; protected Optional max; @@ -133,7 +145,7 @@ public abstract class Setting { } - public static class Number extends Setting { + public static class Number extends ValueSetting { protected Optional min; protected Optional max; @@ -165,7 +177,7 @@ public abstract class Setting { } - public static class Str extends Setting { + public static class Str extends ValueSetting { public static Str builder() { return new Str(); } protected boolean greedy = false; @@ -190,7 +202,7 @@ public abstract class Setting { } - public static class Switch> extends Setting { + public static class Switch> extends ValueSetting { private final Class enumClazz; public static> Switch builder(Class type) { return new Switch(type); } @@ -214,4 +226,102 @@ public abstract class Setting { } } + public static class Many extends Setting, T> { + private Predicate validator; + private Function writer; + private final ArgumentType argument_type; + private final Class clazz; + + // TODO can we infer clazz from the argument type without needing the second argument??? + public Many(ArgumentType argument, Class clazz) { + super(); + this.validator = x -> true; + this.writer = x -> (S) x; // TODO this works ootb if it's just the same type but crashes at runtime for everything else + this.argument_type = argument; + this.clazz = clazz; + } + + public static Many builder(ArgumentType argument, Class clazz) { return new Many(argument, clazz); } + + public Many validator(Predicate validator) { + this.validator = validator; + return this; + } + + public Many writer(Function writer) { + this.writer = writer; + return this; + } + + public ArgumentType argument() { + return this.argument_type; + } + + public ForgeConfigSpec.ConfigValue> value(ForgeConfigSpec.Builder builder) { + return builder + .comment(this.comment.get()) + .defineList( + this.name.get(), + this.fallback.get(), + this.validator + ); + } + + public ForgeConfigSpec.ConfigValue> build(IModule module) { + ForgeConfigSpec.ConfigValue> conf = this.value(module.getConfigBuilder()); + + String optName = this.name.get(); + ArgumentType arg = this.argument(); + Consumer cb = this.callback.isPresent() ? this.callback.get() : null; + + module.getDispatcher().register( + Commands.literal(module.getName().toLowerCase()) + .then( + Commands.literal(optName) + .then( + Commands.literal("add") + .then( + Commands.argument(optName, arg) + .executes( ctx -> { + T value = ctx.getArgument(optName, this.clazz); + if (cb != null) { + cb.accept(value); + } + List botch = Lists.newArrayList(conf.get()); + botch.add(this.writer.apply(value)); + conf.set(botch); + conf.save(); + log(String.format("> %s -++> %s <", String.join(".", conf.getPath()), conf.get().toString())); + return 1; + }) + ) + ) + .then( + Commands.literal("remove") + .then( + Commands.argument(optName, arg) + .executes( ctx -> { + T value = ctx.getArgument(optName, clazz); + if (cb != null) { + cb.accept(value); + } + List botch = Lists.newArrayList(conf.get()); + boolean removed = botch.remove(this.writer.apply(value)); + conf.set(botch); + conf.save(); + log(String.format("> %s -%s> %s <", String.join(".", conf.getPath()), removed ? "//" : "--", conf.get().toString())); + return 1; + }) + ) + ) + .executes(ctx -> { + log(String.format("> %s: %s <", optName, conf.get().toString())); + return 1; + }) + ) + ); + + return conf; + } + } }