diff --git a/src/main/java/dev/anvilcraft/rg/RollingGate.java b/src/main/java/dev/anvilcraft/rg/RollingGate.java index bfe882e..257ab93 100644 --- a/src/main/java/dev/anvilcraft/rg/RollingGate.java +++ b/src/main/java/dev/anvilcraft/rg/RollingGate.java @@ -1,12 +1,9 @@ package dev.anvilcraft.rg; -import com.google.gson.JsonObject; import com.mojang.logging.LogUtils; import dev.anvilcraft.rg.api.RGAdditional; import dev.anvilcraft.rg.api.RGRuleManager; -import dev.anvilcraft.rg.util.ConfigUtil; import net.minecraft.server.MinecraftServer; -import net.minecraft.world.level.storage.LevelResource; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModContainer; @@ -18,7 +15,6 @@ import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; -import java.nio.file.Path; import java.util.Optional; @Mod(RollingGate.MODID) @@ -26,7 +22,6 @@ public class RollingGate implements RGAdditional { public static final String MODID = "rolling_gate"; public static final Logger LOGGER = LogUtils.getLogger(); private static final RGRuleManager RULE_MANAGER = new RGRuleManager(); - private static final LevelResource RULE_PATH = new LevelResource("rolling_gate.json"); public RollingGate(@NotNull IEventBus modEventBus, ModContainer modContainer) { NeoForge.EVENT_BUS.addListener(this::reInitRules); @@ -50,8 +45,6 @@ public void loadRGRules(FMLLoadCompleteEvent event) { @SubscribeEvent public void reInitRules(@NotNull ServerStartingEvent event) { MinecraftServer server = event.getServer(); - Path path = server.getWorldPath(RollingGate.RULE_PATH); - JsonObject config = ConfigUtil.getOrCreateContent(path); - RollingGate.RULE_MANAGER.reInit(config); + RollingGate.RULE_MANAGER.reInit(server); } } diff --git a/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java b/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java index 2b66d6d..da60b0c 100644 --- a/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java +++ b/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java @@ -1,16 +1,25 @@ package dev.anvilcraft.rg.api; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import dev.anvilcraft.rg.RollingGate; +import dev.anvilcraft.rg.util.ConfigUtil; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.fml.loading.FMLPaths; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class RGRuleManager { + private static final Path GlobalConfigPath = FMLPaths.CONFIGDIR.get().resolve("rolling_gate.json"); + private static final LevelResource WorldConfigPath = new LevelResource("rolling_gate.json"); private final Map> rules; private String namespace = "rolling_gate"; @@ -18,11 +27,21 @@ public RGRuleManager() { this.rules = new HashMap<>(); } - public void reInit(@NotNull JsonObject config) { - config.entrySet().forEach((entry) -> { + public void reInit(@NotNull MinecraftServer server) { + this.setSaveRules(ConfigUtil.getOrCreateContent(GlobalConfigPath)); + Path path = server.getWorldPath(WorldConfigPath); + this.setSaveRules(ConfigUtil.getOrCreateContent(path)); + } + + private void setSaveRules(JsonObject config) { + for (Map.Entry entry : config.entrySet()) { RGRule rule = this.rules.get(entry.getKey()); - if (rule != null) rule.setFieldValue(entry.getValue()); - }); + if (rule == null) { + RollingGate.LOGGER.warn("{}({}) not exist.", entry.getKey(), entry.getValue()); + continue; + } + rule.setFieldValue(entry.getValue()); + } } public void register(Class rules) {