Add command to reload config

This commit is contained in:
Johannes Frohnmeyer 2022-01-29 12:06:02 +01:00
parent aff658661a
commit 98556c7b93
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 60 additions and 26 deletions

View File

@ -3,4 +3,5 @@ archivesBaseName = "libjf-config-v0"
dependencies {
moduleDependencies(project, ["libjf-base", "libjf-unsafe-v0"])
include(fabricApi.module("fabric-resource-loader-v0", "${project.fabric_version}"))
include modImplementation(fabricApi.module("fabric-command-api-v1", "${project.fabric_version}"))
}

View File

@ -98,8 +98,12 @@ public class Config {
}
}
});
load();
}
public void load() {
try {
LibJf.GSON.fromJson(Files.newBufferedReader(path), config);
LibJf.GSON.fromJson(Files.newBufferedReader(path), configClass);
}
catch (Exception e) {
LibJf.LOGGER.error("Could not read config", e);

View File

@ -0,0 +1,30 @@
package io.gitlab.jfronny.libjf.config.impl;
import com.mojang.brigadier.Command;
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.text.LiteralText;
import static net.minecraft.server.command.CommandManager.literal;
public class ConfigCommand implements ModInitializer {
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
dispatcher.register(literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4))
.then(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;
}).then(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;
}))));
});
}
}

View File

@ -24,6 +24,9 @@
],
"preLaunch": [
"io.gitlab.jfronny.libjf.config.impl.entry.JfConfigSafe"
],
"main": [
"io.gitlab.jfronny.libjf.config.impl.ConfigCommand"
]
},
"depends": {

View File

@ -11,9 +11,9 @@ import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import static net.minecraft.server.command.CommandManager.literal;
public class JfWeb implements ClientModInitializer, DedicatedServerModInitializer, ModInitializer {
public static final WebServer SERVER;
@ -42,30 +42,26 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
public void onInitialize() {
if (isEnabled()) {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
LiteralArgumentBuilder<ServerCommandSource> base = CommandManager.literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4));
LiteralArgumentBuilder<ServerCommandSource> web = CommandManager.literal("web");
base.then(web);
web.executes(context -> {
if (SERVER.isActive()) {
context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false);
}
else {
context.getSource().sendFeedback(new LiteralText("LibWeb is not active. Use libweb restart to reload"), false);
}
return Command.SINGLE_SUCCESS;
});
web.then(CommandManager.literal("restart").executes(context -> {
try {
context.getSource().sendFeedback(new LiteralText("Restarting LibWeb"), true);
SERVER.restart();
}
catch (Exception e) {
LibJf.LOGGER.error("Failed to run restart command", e);
context.getSource().sendError(new LiteralText(e.getMessage()));
}
return Command.SINGLE_SUCCESS;
}));
dispatcher.register(base);
dispatcher.register(literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4))
.then(literal("web").executes(context -> {
if (SERVER.isActive()) {
context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false);
}
else {
context.getSource().sendFeedback(new LiteralText("LibWeb is not active. Use libweb restart to reload"), false);
}
return Command.SINGLE_SUCCESS;
}).then(literal("restart").executes(context -> {
try {
context.getSource().sendFeedback(new LiteralText("Restarting LibWeb"), true);
SERVER.restart();
}
catch (Exception e) {
LibJf.LOGGER.error("Failed to run restart command", e);
context.getSource().sendError(new LiteralText(e.getMessage()));
}
return Command.SINGLE_SUCCESS;
}))));
});
}
Runtime.getRuntime().addShutdownHook(new Thread(SERVER::stop));