Skip to content

Commit

Permalink
manager优先设置global config
Browse files Browse the repository at this point in the history
  • Loading branch information
Cjsah committed Sep 17, 2024
1 parent cbe0e05 commit ce7ca85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/anvilcraft/rg/RollingGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,20 +23,29 @@

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<String, RGRule<?>> rules;

public RGRuleManager() {
this.rules = new HashMap<>();
}

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<String, JsonElement> 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) {
Expand Down

0 comments on commit ce7ca85

Please sign in to comment.