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 { private static final Logger LOGGER = LogManager.getLogger(); @Override public int run(CommandContext 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; } } }