Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for changing systemsettings file name #1425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down