Respackopts/src/main/java/io/gitlab/jfronny/respackopts/model/ConfigFile.java

44 lines
1.4 KiB
Java
Raw Normal View History

2021-11-14 15:37:01 +01:00
package io.gitlab.jfronny.respackopts.model;
2021-09-15 18:37:07 +02:00
import io.gitlab.jfronny.respackopts.Respackopts;
2021-09-15 18:37:07 +02:00
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ConfigFile {
public boolean debugCommands = false;
public boolean debugLogs = false;
public boolean dashloaderCompat = true;
private static final Path rpoPath = Respackopts.FALLBACK_CONF_DIR.resolve("_respackopts.conf");
2021-09-15 18:37:07 +02:00
public static ConfigFile load() {
if (!Files.exists(rpoPath))
new ConfigFile().save();
try (BufferedReader br = Files.newBufferedReader(rpoPath)) {
return Respackopts.GSON.fromJson(br, ConfigFile.class);
2021-09-15 18:37:07 +02:00
} catch (IOException e) {
Respackopts.LOGGER.error("Could not load config, using default", e);
2021-09-15 18:37:07 +02:00
return new ConfigFile();
}
}
public void save() {
if (!Files.exists(Respackopts.FALLBACK_CONF_DIR)) {
2021-09-15 18:37:07 +02:00
try {
Files.createDirectories(Respackopts.FALLBACK_CONF_DIR);
2021-09-15 18:37:07 +02:00
} catch (IOException e) {
Respackopts.LOGGER.error("Could not create config dir", e);
2021-09-15 18:37:07 +02:00
return;
}
}
try (BufferedWriter bw = Files.newBufferedWriter(rpoPath)) {
Respackopts.GSON.toJson(this, bw);
2021-09-15 18:37:07 +02:00
} catch (IOException e) {
Respackopts.LOGGER.error("Could not save", e);
2021-09-15 18:37:07 +02:00
}
}
}