Unicopia/src/main/java/com/minelittlepony/unicopia/UConfig.java

75 lines
1.9 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
public class UConfig {
private static UConfig instance;
private static final Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setPrettyPrinting()
.create();
public static UConfig getInstance() {
return instance;
}
static void init(File directory) {
File file = new File(directory, "unicopia.json");
try {
2019-01-27 18:36:41 +01:00
if (file.exists()) {
try(JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(file)));) {
instance = gson.fromJson(reader, UConfig.class);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (instance == null) {
instance = new UConfig();
}
}
instance.file = file;
2019-01-27 18:36:41 +01:00
instance.save();
}
private File file;
@Expose
private final List<Race> speciesWhiteList = Lists.newArrayList();
public List<Race> getSpeciesWhiteList() {
return speciesWhiteList;
}
public void save() {
if (file.exists()) {
file.delete();
}
try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(file)))) {
2019-01-27 18:36:41 +01:00
writer.setIndent(" ");
gson.toJson(this, UConfig.class, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}