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 net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; 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).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)); LiteralArgumentBuilder c_config = literal("config").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; }); 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); 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 " + 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 " + 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); return Command.SINGLE_SUCCESS; })); }); dispatcher.register(c_libjf.then(c_config.then(c_reload))); }); } 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; } }