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