feat: allow to add setting changing callback

This commit is contained in:
əlemi 2023-03-01 18:16:06 +01:00
parent 8a3cc80809
commit b8eba78e20
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -7,6 +7,7 @@ import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.server.command.EnumArgument; import net.minecraftforge.server.command.EnumArgument;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;
import static ftbsc.bscv.Boscovicino.log; import static ftbsc.bscv.Boscovicino.log;
@ -15,12 +16,14 @@ public abstract class Setting<T> {
protected Optional<String> name; protected Optional<String> name;
protected Optional<String> comment; protected Optional<String> comment;
protected Optional<T> fallback; protected Optional<T> fallback;
protected Optional<Consumer<T>> callback;
Setting() { Setting() {
this.name = Optional.empty(); this.name = Optional.empty();
this.comment = Optional.empty(); this.comment = Optional.empty();
this.fallback = Optional.empty(); this.fallback = Optional.empty();
this.callback = Optional.empty();
} }
public Setting<T> name(String name) { public Setting<T> name(String name) {
@ -38,6 +41,11 @@ public abstract class Setting<T> {
return this; 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 ForgeConfigSpec.ConfigValue<T> value(ForgeConfigSpec.Builder builder);
abstract ArgumentType<T> argument(); abstract ArgumentType<T> argument();
@ -47,21 +55,29 @@ public abstract class Setting<T> {
public ForgeConfigSpec.ConfigValue<T> build(IModule module) { public ForgeConfigSpec.ConfigValue<T> build(IModule module) {
ForgeConfigSpec.ConfigValue<T> conf = this.value(module.getConfigBuilder()); 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( module.getDispatcher().register(
Commands.literal(module.getName().toLowerCase()) Commands.literal(module.getName().toLowerCase())
.then( .then(
Commands.literal(this.name.get()) Commands.literal(optName)
.then( .then(
Commands.argument(this.name.get(), this.argument()) Commands.argument(optName, arg)
.executes( ctx -> { .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.set(value);
conf.save(); conf.save();
log(String.format("> %s -> %s <", String.join(".", conf.getPath()), conf.get().toString())); log(String.format("> %s -> %s <", String.join(".", conf.getPath()), conf.get().toString()));
return 1; return 1;
})) }))
.executes(ctx -> { .executes(ctx -> {
log(String.format("> %s: %s <", name, conf.get().toString())); log(String.format("> %s: %s <", optName, conf.get().toString()));
return 1; return 1;
}) })
) )