aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/java/moe/ymc/acron/config/ConfigReloadCmd.java
blob: 2774c4dbf31ad1aa506ff7e5d3b5578e4d1c623c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
        }
    }
}