diff options
author | alemi <me@alemi.dev> | 2023-03-01 18:16:06 +0100 |
---|---|---|
committer | alemi <me@alemi.dev> | 2023-03-01 18:16:06 +0100 |
commit | b8eba78e20219a076048d35b3a7acee4c2a3bd45 (patch) | |
tree | 49ad65c2146b365f731f1f8bf34358e32e82112b /src/main/java | |
parent | 8a3cc808093e5fa1e87558a45386f0cbdf0f3145 (diff) |
feat: allow to add setting changing callback
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/ftbsc/bscv/tools/Setting.java | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/main/java/ftbsc/bscv/tools/Setting.java b/src/main/java/ftbsc/bscv/tools/Setting.java index 7f4d627..99f5480 100644 --- a/src/main/java/ftbsc/bscv/tools/Setting.java +++ b/src/main/java/ftbsc/bscv/tools/Setting.java @@ -7,6 +7,7 @@ import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.server.command.EnumArgument; import java.util.Optional; +import java.util.function.Consumer; import static ftbsc.bscv.Boscovicino.log; @@ -15,12 +16,14 @@ public abstract class Setting<T> { protected Optional<String> name; protected Optional<String> comment; protected Optional<T> fallback; + protected Optional<Consumer<T>> callback; Setting() { this.name = Optional.empty(); this.comment = Optional.empty(); this.fallback = Optional.empty(); + this.callback = Optional.empty(); } public Setting<T> name(String name) { @@ -38,6 +41,11 @@ public abstract class Setting<T> { return this; } + public Setting<T> callback(Consumer<T> callback) { + this.callback = Optional.of(callback); + return this; + } + abstract ForgeConfigSpec.ConfigValue<T> value(ForgeConfigSpec.Builder builder); abstract ArgumentType<T> argument(); @@ -47,21 +55,29 @@ public abstract class Setting<T> { public ForgeConfigSpec.ConfigValue<T> build(IModule module) { ForgeConfigSpec.ConfigValue<T> conf = this.value(module.getConfigBuilder()); + String optName = this.name.get(); + Class<T> clazz = this.clazz(); + ArgumentType<T> arg = this.argument(); + Consumer<T> cb = this.callback.isPresent() ? this.callback.get() : null; + module.getDispatcher().register( Commands.literal(module.getName().toLowerCase()) .then( - Commands.literal(this.name.get()) + Commands.literal(optName) .then( - Commands.argument(this.name.get(), this.argument()) + Commands.argument(optName, arg) .executes( ctx -> { - T value = ctx.getArgument(this.name.get(), this.clazz()); + 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 <", name, conf.get().toString())); + log(String.format("> %s: %s <", optName, conf.get().toString())); return 1; }) ) |