[config] Expose new features through /libjf config command

This commit is contained in:
Johannes Frohnmeyer 2022-04-02 22:43:31 +02:00
parent 900ac479eb
commit 5dc4198077
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 77 additions and 32 deletions

View File

@ -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);

View File

@ -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<ServerCommandSource> c_libjf = literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4));
LiteralArgumentBuilder<ServerCommandSource> c_config = literal("config").executes(context -> {
LiteralArgumentBuilder<ServerCommandSource> c_libjf = literal(LibJf.MOD_ID);
LiteralArgumentBuilder<ServerCommandSource> 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<ServerCommandSource> 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<ServerCommandSource> c_instance = literal(id);
for (EntryInfo entry : config.getEntries()) {
LiteralArgumentBuilder<ServerCommandSource> 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<ServerCommandSource> 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<ServerCommandSource> c_config, LiteralArgumentBuilder<ServerCommandSource> c_reload, LiteralArgumentBuilder<ServerCommandSource> c_reset, Function<Consumer<LiteralArgumentBuilder<ServerCommandSource>>, LiteralArgumentBuilder<ServerCommandSource>> 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<ServerCommandSource> 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<ServerCommandSource> c_instance2 = literal(id2);
cns.accept(c_instance2);
cns1.then(c_instance2);
});
});
});
}

View File

@ -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);
}