aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java
diff options
context:
space:
mode:
Diffstat (limited to 'mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java')
-rw-r--r--mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java41
1 files changed, 41 insertions, 0 deletions
diff --git a/mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java b/mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java
new file mode 100644
index 0000000..2774c4d
--- /dev/null
+++ b/mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java
@@ -0,0 +1,41 @@
+package moe.ymc.acron.config;
+
+import com.mojang.brigadier.Command;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import moe.ymc.acron.config.json.ConfigDeserializationException;
+import moe.ymc.acron.config.json.ConfigDeserializer;
+import net.fabricmc.loader.api.FabricLoader;
+import net.minecraft.server.command.ServerCommandSource;
+import net.minecraft.text.LiteralText;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.IOException;
+import java.nio.file.Path;
+
+public class ConfigReloadCmd implements Command<ServerCommandSource> {
+ private static final Logger LOGGER = LogManager.getLogger();
+
+ @Override
+ public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
+ LOGGER.info("Reloading rules.");
+ try {
+ final Path config = FabricLoader
+ .getInstance().getConfigDir()
+ .resolve("acron.json");
+ if (!config.toFile().exists()) {
+ throw new IllegalStateException("Cannot find config/acron.json.");
+ }
+ final Config cfg = ConfigDeserializer.deserialize(config.toFile(), false);
+ Config.setGlobalConfig(cfg);
+ context.getSource().sendFeedback(new LiteralText("Rules reloaded."), true);
+ return 0;
+ } catch (IOException | ConfigDeserializationException e) {
+ LOGGER.error("Cannot reload config.", e);
+ context.getSource().sendError(new LiteralText("Cannot reload rules: " +
+ e.getMessage()));
+ return 1;
+ }
+ }
+}