50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
package wtf.hak.survivalfabric.config;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
|
|
public class ConfigManager {
|
|
|
|
private static final File CONFIG_FILE = FabricLoader.getInstance().getConfigDir().resolve("survivalfabric.json").toFile();
|
|
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
private static Config INSTANCE;
|
|
|
|
public static Config getConfig() {
|
|
if (INSTANCE == null) {
|
|
return load();
|
|
} else
|
|
return INSTANCE;
|
|
}
|
|
|
|
public static Config load() {
|
|
try(FileReader reader = new FileReader(CONFIG_FILE)) {
|
|
INSTANCE = GSON.fromJson(reader, Config.class);
|
|
if (INSTANCE.configVersion.equalsIgnoreCase(new Config().configVersion)) {
|
|
return INSTANCE;
|
|
}
|
|
INSTANCE.configVersion = new Config().configVersion;
|
|
save(INSTANCE);
|
|
return INSTANCE;
|
|
} catch (IOException e) {
|
|
Config config = new Config();
|
|
INSTANCE = config;
|
|
save(config);
|
|
return config;
|
|
}
|
|
}
|
|
|
|
public static void save(Config config) {
|
|
try(FileWriter writer = new FileWriter(CONFIG_FILE)) {
|
|
GSON.toJson(config, writer);
|
|
} catch (IOException e) {
|
|
System.out.println("Error saving config: " + e.getMessage());
|
|
}
|
|
}
|
|
} |