From b8eba78e20219a076048d35b3a7acee4c2a3bd45 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 1 Mar 2023 18:16:06 +0100 Subject: [PATCH] feat: allow to add setting changing callback --- src/main/java/ftbsc/bscv/tools/Setting.java | 24 +++++++++++++++++---- 1 file 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 { protected Optional name; protected Optional comment; protected Optional fallback; + protected Optional> callback; Setting() { this.name = Optional.empty(); this.comment = Optional.empty(); this.fallback = Optional.empty(); + this.callback = Optional.empty(); } public Setting name(String name) { @@ -38,6 +41,11 @@ public abstract class Setting { return this; } + public Setting callback(Consumer callback) { + this.callback = Optional.of(callback); + return this; + } + abstract ForgeConfigSpec.ConfigValue value(ForgeConfigSpec.Builder builder); abstract ArgumentType argument(); @@ -47,21 +55,29 @@ public abstract class Setting { 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(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; }) )