diff --git a/common/src/main/java/cn/zbx1425/worldcomment/Config.java b/common/src/main/java/cn/zbx1425/worldcomment/Config.java index 06af51b..3834d82 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/Config.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/Config.java @@ -1,5 +1,37 @@ package cn.zbx1425.worldcomment; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + public class Config { - + + public String redisUrl = ""; + + public String uplinkUrl = ""; + public String uplinkAuthKey = ""; + + public void load(Path configPath) throws IOException { + if (!Files.exists(configPath)) { + return; + } + + JsonObject json = JsonParser.parseString(Files.readString(configPath)).getAsJsonObject(); + redisUrl = json.get("redisUrl").getAsString(); + uplinkUrl = json.get("uplinkUrl").getAsString(); + uplinkAuthKey = json.get("uplinkAuthKey").getAsString(); + } + + public void save(Path configPath) throws IOException { + JsonObject json = new JsonObject(); + json.addProperty("redisUrl", redisUrl); + json.addProperty("uplinkUrl", uplinkUrl); + json.addProperty("uplinkAuthKey", uplinkAuthKey); + + Files.writeString(configPath, new GsonBuilder().setPrettyPrinting().create().toJson(json.toString())); + } } diff --git a/common/src/main/java/cn/zbx1425/worldcomment/Main.java b/common/src/main/java/cn/zbx1425/worldcomment/Main.java index 089f5d0..6ef0503 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/Main.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/Main.java @@ -27,6 +27,8 @@ public class Main { public static ServerWorldData DATABASE; + public static Config CONFIG = new Config(); + public static final RegistryObject ITEM_COMMENT_TOOL = new RegistryObject<>(CommentToolItem::new); public static void init(RegistriesWrapper registries) { @@ -43,9 +45,13 @@ public static void init(RegistriesWrapper registries) { ServerPlatform.registerServerStartingEvent(server -> { try { - //Todo: config inject here + CONFIG.load(server.getServerDirectory().toPath() + .resolve("config").resolve("world-comment.json")); + DATABASE = new ServerWorldData(server); - DATABASE.peerChannel = new RedisSynchronizer("redis://192.168.1.148:6379/0", DATABASE); + if (!CONFIG.redisUrl.isEmpty()) { + DATABASE.peerChannel = new RedisSynchronizer(CONFIG.redisUrl, DATABASE); + } DATABASE.load(); } catch (IOException e) { LOGGER.error("Failed to open data storage", e); diff --git a/common/src/main/java/cn/zbx1425/worldcomment/data/sync/NoopSynchronizer.java b/common/src/main/java/cn/zbx1425/worldcomment/data/sync/NoopSynchronizer.java index a4d3ba9..56c7ad4 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/data/sync/NoopSynchronizer.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/data/sync/NoopSynchronizer.java @@ -4,7 +4,6 @@ import cn.zbx1425.worldcomment.data.CommentEntry; import it.unimi.dsi.fastutil.longs.Long2ObjectSortedMap; -//Todo: doing nothing currently public class NoopSynchronizer implements Synchronizer { public NoopSynchronizer() {