diff --git a/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/ConfigInstanceAbstract.java b/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/ConfigInstanceAbstract.java index 2c1a94f..195e355 100644 --- a/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/ConfigInstanceAbstract.java +++ b/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/ConfigInstanceAbstract.java @@ -14,6 +14,7 @@ import java.lang.reflect.Method; import java.util.*; public abstract class ConfigInstanceAbstract implements ConfigInstance { + public static final String CONFIG_PRESET_DEFAULT = "libjf-config-v0.default"; public final String modId; private final String categoryPath; public final Class configClass; @@ -43,7 +44,7 @@ public abstract class ConfigInstanceAbstract implements ConfigInstance { } entries.add(info); } - presets.put("libjf-config-v0.default", () -> { + presets.put(CONFIG_PRESET_DEFAULT, () -> { for (EntryInfo entry : entries) { try { entry.field.set(null, entry.defaultValue); diff --git a/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/JfConfigCommand.java b/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/JfConfigCommand.java index 5034744..597e20d 100644 --- a/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/JfConfigCommand.java +++ b/libjf-config-v0/src/main/java/io/gitlab/jfronny/libjf/config/impl/JfConfigCommand.java @@ -5,11 +5,15 @@ import com.mojang.brigadier.arguments.*; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import io.gitlab.jfronny.libjf.LibJf; import io.gitlab.jfronny.libjf.config.api.ConfigHolder; +import io.gitlab.jfronny.libjf.config.api.ConfigInstance; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; +import java.util.function.Consumer; +import java.util.function.Function; + import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -17,8 +21,10 @@ public class JfConfigCommand implements ModInitializer { @Override public void onInitialize() { CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { - LiteralArgumentBuilder c_libjf = literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)); - LiteralArgumentBuilder c_config = literal("config").executes(context -> { + LiteralArgumentBuilder c_libjf = literal(LibJf.MOD_ID); + LiteralArgumentBuilder c_config = literal("config") + .requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)) + .executes(context -> { context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Loaded configs for:"), false); ConfigHolder.getInstance().getRegistered().forEach((s, config) -> { context.getSource().sendFeedback(new LiteralText("- " + s), false); @@ -30,43 +36,82 @@ public class JfConfigCommand implements ModInitializer { context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Reloaded configs"), true); return Command.SINGLE_SUCCESS; }); + LiteralArgumentBuilder c_reset = literal("reset").executes(context -> { + context.getSource().sendError(new LiteralText("[libjf-config-v0] Please specify a config to reset")); + return Command.SINGLE_SUCCESS; + }); ConfigHolder.getInstance().getRegistered().forEach((id, config) -> { - LiteralArgumentBuilder c_instance = literal(id); - for (EntryInfo entry : config.getEntries()) { - LiteralArgumentBuilder c_entry = literal(entry.field.getName()).executes(context -> { - context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] The value of " + entry.field.getName() + " is " + entry.value), false); + registerEntries(config, id, c_config, c_reload, c_reset, cns -> { + LiteralArgumentBuilder c_instance = literal(id); + cns.accept(c_instance); + return c_instance; + }); + }); + dispatcher.register(c_libjf.then(c_config.then(c_reload).then(c_reset))); + }); + } + + private void registerEntries(ConfigInstance config, String subpath, LiteralArgumentBuilder c_config, LiteralArgumentBuilder c_reload, LiteralArgumentBuilder c_reset, Function>, LiteralArgumentBuilder> pathGen) { + c_config.then(pathGen.apply(cns -> { + cns.executes(context -> { + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] " + subpath + " is a category"), false); + return Command.SINGLE_SUCCESS; + }); + for (EntryInfo entry : config.getEntries()) { + LiteralArgumentBuilder c_entry = literal(entry.field.getName()).executes(context -> { + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] The value of " + subpath + entry.field.getName() + " is " + entry.value), false); + return Command.SINGLE_SUCCESS; + }); + ArgumentType type = getType(entry); + if (type != null) { + c_entry.then(argument("value", type).executes(context -> { + Object value = context.getArgument("value", entry.field.getType()); + entry.value = value; + config.syncToClass(); + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Set " + subpath + entry.field.getName() + " to " + value), true); return Command.SINGLE_SUCCESS; - }); - ArgumentType type = getType(entry); - if (type != null) { - c_entry.then(argument("value", type).executes(context -> { - Object value = context.getArgument("value", entry.field.getType()); - entry.value = value; + })); + } + else if (entry.field.getType().isEnum()) { + for (Object enumConstant : entry.field.getType().getEnumConstants()) { + c_entry.then(literal(enumConstant.toString()).executes(context -> { + entry.value = enumConstant; config.syncToClass(); - context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Set " + entry.field.getName() + " to " + value), true); + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Set " + subpath + entry.field.getName() + " to " + enumConstant), true); return Command.SINGLE_SUCCESS; })); } - else if (entry.field.getType().isEnum()) { - for (Object enumConstant : entry.field.getType().getEnumConstants()) { - c_entry.then(literal(enumConstant.toString()).executes(context -> { - entry.value = enumConstant; - config.syncToClass(); - context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Set " + entry.field.getName() + " to " + enumConstant), true); - return Command.SINGLE_SUCCESS; - })); - } - } - c_instance.then(c_entry); } - c_config.then(c_instance); - c_reload.then(literal(id).executes(context -> { - config.load(); - context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Reloaded config for " + id), true); + cns.then(c_entry); + } + })); + c_reload.then(pathGen.apply(cns -> cns.executes(context -> { + config.load(); + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Reloaded config for " + subpath), true); + return Command.SINGLE_SUCCESS; + }))); + c_reset.then(pathGen.apply(cns -> { + cns.executes(context -> { + config.getPresets().get(ConfigInstanceAbstract.CONFIG_PRESET_DEFAULT).run(); + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Reset config for " + subpath), true); + return Command.SINGLE_SUCCESS; + }); + config.getPresets().forEach((id2, preset) -> { + cns.then(literal(id2).executes(context -> { + preset.run(); + context.getSource().sendFeedback(new LiteralText("[libjf-config-v0] Loaded preset " + id2 + " for " + subpath), true); return Command.SINGLE_SUCCESS; })); }); - dispatcher.register(c_libjf.then(c_config.then(c_reload))); + })); + config.getCategories().forEach((id2, cfg) -> { + registerEntries(cfg, cfg.getCategoryPath(), c_config, c_reload, c_reset, cns -> { + return pathGen.apply(cns1 -> { + LiteralArgumentBuilder c_instance2 = literal(id2); + cns.accept(c_instance2); + cns1.then(c_instance2); + }); + }); }); } diff --git a/libjf-web-v0/src/main/java/io/gitlab/jfronny/libjf/web/impl/JfWeb.java b/libjf-web-v0/src/main/java/io/gitlab/jfronny/libjf/web/impl/JfWeb.java index 403b9c9..4b616b6 100644 --- a/libjf-web-v0/src/main/java/io/gitlab/jfronny/libjf/web/impl/JfWeb.java +++ b/libjf-web-v0/src/main/java/io/gitlab/jfronny/libjf/web/impl/JfWeb.java @@ -32,8 +32,7 @@ public class JfWeb implements CoProcess, ModInitializer { public void onInitialize() { if (isEnabled()) { CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { - dispatcher.register(literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)) - .then(literal("web").executes(context -> { + dispatcher.register(literal(LibJf.MOD_ID).then(literal("web").requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)).executes(context -> { if (SERVER.isActive()) { context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false); }