feat(config-commands): Write on change and work in singleplayer

This commit is contained in:
Johannes Frohnmeyer 2023-07-09 13:42:43 +02:00
parent e2a891c2e2
commit e4d510f164
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 9 additions and 3 deletions

View File

@ -111,7 +111,10 @@ public class JfConfigCommand implements ModInitializer {
if (type != null) { if (type != null) {
c_entry.then(argument("value", type).executes(context -> { c_entry.then(argument("value", type).executes(context -> {
@SuppressWarnings("unchecked") T value = context.getArgument("value", (Class<T>) entry.getValueType().asClass()); @SuppressWarnings("unchecked") T value = context.getArgument("value", (Class<T>) entry.getValueType().asClass());
tryRun(() -> entry.setValue(value)); tryRun(() -> {
entry.setValue(value);
config.getRoot().write();
});
context.getSource().sendFeedback(() -> Text.literal("[libjf-config-v1] Set " + subpath + "." + entry.getName() + " to " + value), true); context.getSource().sendFeedback(() -> Text.literal("[libjf-config-v1] Set " + subpath + "." + entry.getName() + " to " + value), true);
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
})); }));
@ -119,7 +122,10 @@ public class JfConfigCommand implements ModInitializer {
else if (entry.getValueType().isEnum()) { else if (entry.getValueType().isEnum()) {
for (T enumConstant : ((Type.TEnum<T>)entry.getValueType()).options()) { for (T enumConstant : ((Type.TEnum<T>)entry.getValueType()).options()) {
c_entry.then(literal(enumConstant.toString()).executes(context -> { c_entry.then(literal(enumConstant.toString()).executes(context -> {
tryRun(() -> entry.setValue(enumConstant)); tryRun(() -> {
entry.setValue(enumConstant);
config.getRoot().write();
});
context.getSource().sendFeedback(() -> Text.literal("[libjf-config-v1] Set " + subpath + "." + entry.getName() + " to " + enumConstant), true); context.getSource().sendFeedback(() -> Text.literal("[libjf-config-v1] Set " + subpath + "." + entry.getName() + " to " + enumConstant), true);
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
})); }));

View File

@ -11,7 +11,7 @@
"sources": "https://git.frohnmeyer-wds.de/JfMods/LibJF" "sources": "https://git.frohnmeyer-wds.de/JfMods/LibJF"
}, },
"license": "MIT", "license": "MIT",
"environment": "server", "environment": "*",
"entrypoints": { "entrypoints": {
"main": ["io.gitlab.jfronny.libjf.config.impl.commands.JfConfigCommand"] "main": ["io.gitlab.jfronny.libjf.config.impl.commands.JfConfigCommand"]
}, },