From ce7ca854cbb090d67bd9de2784dbcca8c10c11a9 Mon Sep 17 00:00:00 2001 From: Cjsah Date: Tue, 17 Sep 2024 20:54:35 +0800 Subject: [PATCH] =?UTF-8?q?manager=E4=BC=98=E5=85=88=E8=AE=BE=E7=BD=AEglob?= =?UTF-8?q?al=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/anvilcraft/rg/RollingGate.java | 2 +- .../dev/anvilcraft/rg/api/RGRuleManager.java | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/anvilcraft/rg/RollingGate.java b/src/main/java/dev/anvilcraft/rg/RollingGate.java index 619bc33..71d193b 100644 --- a/src/main/java/dev/anvilcraft/rg/RollingGate.java +++ b/src/main/java/dev/anvilcraft/rg/RollingGate.java @@ -12,7 +12,7 @@ @Mod(RollingGate.MODID) public class RollingGate { public static final String MODID = "rolling_gate"; - private static final Logger LOGGER = LogUtils.getLogger(); + public static final Logger LOGGER = LogUtils.getLogger(); public RollingGate(IEventBus modEventBus, ModContainer modContainer) { NeoForge.EVENT_BUS.addListener((ServerStartingEvent event) -> RGRuleManager.reInitSaveRules(event.getServer())); diff --git a/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java b/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java index 0217dbb..a300206 100644 --- a/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java +++ b/src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java @@ -1,9 +1,12 @@ package dev.anvilcraft.rg.api; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import dev.anvilcraft.rg.RollingGate; import net.minecraft.server.MinecraftServer; import net.minecraft.util.GsonHelper; import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.fml.loading.FMLPaths; import org.apache.commons.io.FileUtils; import org.jetbrains.annotations.NotNull; import org.spongepowered.include.com.google.common.base.Charsets; @@ -20,7 +23,8 @@ public class RGRuleManager { private static final RGRuleManager INSTANCE = new RGRuleManager(); - private static final LevelResource path = new LevelResource("rolling_gate.json"); + 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; public RGRuleManager() { @@ -28,12 +32,20 @@ public RGRuleManager() { } public static void reInitSaveRules(MinecraftServer server) { - Path path = server.getWorldPath(RGRuleManager.path); - JsonObject config = getOrCreateContent(path); - config.entrySet().forEach((entry) -> { + Path path = server.getWorldPath(RGRuleManager.WorldConfigPath); + setSaveRules(getOrCreateContent(GlobalConfigPath)); + setSaveRules(getOrCreateContent(path)); + } + + private static void setSaveRules(JsonObject config) { + for (Map.Entry entry : config.entrySet()) { RGRule rule = INSTANCE.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(RGRule rule) {