From 8a6494913324e60189bcea0d680aacad6defcfc8 Mon Sep 17 00:00:00 2001 From: KamilKurde Date: Fri, 18 Aug 2023 16:46:39 +0200 Subject: [PATCH] Allow for changing systemsettings file name --- .../jmusicbot/settings/SettingsManager.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java b/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java index 4a70a0fba..1305a4dcc 100644 --- a/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java +++ b/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java @@ -19,7 +19,9 @@ import com.jagrosh.jmusicbot.utils.OtherUtil; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; +import com.typesafe.config.ConfigFactory; import net.dv8tion.jda.api.entities.Guild; import org.json.JSONException; import org.json.JSONObject; @@ -38,7 +40,7 @@ public SettingsManager() { this.settings = new HashMap<>(); try { - JSONObject loadedSettings = new JSONObject(new String(Files.readAllBytes(OtherUtil.getPath("serversettings.json")))); + JSONObject loadedSettings = new JSONObject(new String(Files.readAllBytes(getServerSettingsPath()))); loadedSettings.keySet().forEach((id) -> { JSONObject o = loadedSettings.getJSONObject(id); @@ -83,7 +85,19 @@ private Settings createDefaultSettings() { return new Settings(this, 0, 0, 0, 100, null, RepeatMode.OFF, null, SKIP_RATIO); } - + + private static Path getServerSettingsPath() + { + Path path = OtherUtil.getPath(System.getProperty("serversettings.file", System.getProperty("serversettings", "serversettings.json"))); + if(path.toFile().exists()) + { + if(System.getProperty("serversettings.file") == null) + System.setProperty("serversettings.file", System.getProperty("serversettings", path.toAbsolutePath().toString())); + ConfigFactory.invalidateCaches(); + } + return path; + } + protected void writeSettings() { JSONObject obj = new JSONObject(); @@ -109,7 +123,7 @@ protected void writeSettings() obj.put(Long.toString(key), o); }); try { - Files.write(OtherUtil.getPath("serversettings.json"), obj.toString(4).getBytes()); + Files.write(getServerSettingsPath(), obj.toString(4).getBytes()); } catch(IOException ex){ LoggerFactory.getLogger("Settings").warn("Failed to write to file: "+ex); }