Skip to content

Commit

Permalink
重新添加global config
Browse files Browse the repository at this point in the history
  • Loading branch information
Cjsah committed Sep 17, 2024
1 parent a013f91 commit 396679c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
9 changes: 1 addition & 8 deletions src/main/java/dev/anvilcraft/rg/RollingGate.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,15 +15,13 @@
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.nio.file.Path;
import java.util.Optional;

@Mod(RollingGate.MODID)
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);
Expand All @@ -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);
}
}
27 changes: 23 additions & 4 deletions src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
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<String, RGRule<?>> rules;
private String namespace = "rolling_gate";

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

0 comments on commit 396679c

Please sign in to comment.