From bfc6d4fef6804b0050537228ffdabc63b0f6683b Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 13 Nov 2023 04:24:49 +0100 Subject: feat: added Many setting for collections still very raw and not super helpful but kind of usable? --- src/main/java/ftbsc/bscv/tools/Setting.java | 196 ++++++++++++++++++++++------ 1 file changed, 153 insertions(+), 43 deletions(-) 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(); + + public abstract ForgeConfigSpec.ConfigValue build(IModule module); + - abstract Class clazz(); - public ForgeConfigSpec.ConfigValue build(IModule module) { - ForgeConfigSpec.ConfigValue conf = this.value(module.getConfigBuilder()); + private static abstract class ValueSetting extends Setting { + + abstract Class clazz(); - String optName = this.name.get(); - Class clazz = this.clazz(); - ArgumentType arg = this.argument(); - Consumer cb = this.callback.isPresent() ? this.callback.get() : null; + public ForgeConfigSpec.ConfigValue build(IModule module) { + ForgeConfigSpec.ConfigValue conf = this.value(module.getConfigBuilder()); - module.getDispatcher().register( - Commands.literal(module.getName().toLowerCase()) - .then( - Commands.literal(optName) + 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; - }) - ) - ); - - return conf; + 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; + } } - 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; + } + } } -- cgit v1.2.3-56-ga3b1