package io.gitlab.jfronny.libjf.config.impl; import com.mojang.brigadier.Command; 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; public class JfConfigCommand implements ModInitializer { @Override public void onInitialize() { CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { 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); }); return Command.SINGLE_SUCCESS; }); LiteralArgumentBuilder c_reload = literal("reload").executes(context -> { ConfigHolder.getInstance().getRegistered().forEach((mod, config) -> config.load()); 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) -> { 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; })); } 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 " + subpath + entry.field.getName() + " to " + enumConstant), true); return Command.SINGLE_SUCCESS; })); } } 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; })); }); })); 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); }); }); }); } private ArgumentType getType(EntryInfo info) { Class type = info.field.getType(); if (type == int.class || type == Integer.class) return IntegerArgumentType.integer((int) info.entry.min(), (int) info.entry.max()); else if (type == float.class || type == Float.class) return FloatArgumentType.floatArg((float) info.entry.min(), (float) info.entry.max()); else if (type == double.class || type == Double.class) return DoubleArgumentType.doubleArg(info.entry.min(), info.entry.max()); else if (type == String.class) return StringArgumentType.greedyString(); else if (type == boolean.class || type == Boolean.class) return BoolArgumentType.bool(); else return null; } }