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

120 lines
3.4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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;
2020-01-17 14:27:26 +01:00
public class Config {
2020-01-17 14:27:26 +01:00
private static Config instance = new Config();
private static final Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setPrettyPrinting()
.create();
2020-04-15 18:12:00 +02:00
public static Config getInstance() {
return instance;
}
2020-04-15 18:12:00 +02:00
static void init(Path directory) {
Path file = directory.resolve("unicopia.json");
try {
if (Files.exists(file)) {
try(JsonReader reader = new JsonReader(Files.newBufferedReader(file))) {
2020-01-17 14:27:26 +01:00
instance = gson.fromJson(reader, Config.class);
2019-01-27 18:36:41 +01:00
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (instance == null) {
2020-01-17 14:27:26 +01:00
instance = new Config();
}
}
instance.file = file;
2019-01-27 18:36:41 +01:00
instance.save();
}
private Path file;
@Expose(deserialize = false)
private final String speciesWhiteListComment =
"A whitelist of races permitted on the server. " +
"Races added to this list can be used by anyone, whilst any ones left off are not permitted. " +
"An empty list disables whitelisting entirely.";
@Expose
private final List<Race> speciesWhiteList = Lists.newArrayList();
@Expose(deserialize = false)
private final String preferredRaceComment =
"The default preferred race. " +
"This is the race a client requests when first joining a game. " +
"It is the default used both when Mine Little Pony is not installed and when they respond with a human race.";
@Expose
private Race preferredRace = Race.EARTH;
@Expose(deserialize = false)
private final String ignoreMineLPComment =
"If true Mine Little Pony will not be considered when determining the race to use. " +
"The result will always be what is set by this config file.";
@Expose
private boolean ignoreMineLP = false;
public List<Race> getSpeciesWhiteList() {
return speciesWhiteList;
}
public boolean ignoresMineLittlePony() {
return ignoreMineLP;
}
public void setIgnoreMineLittlePony(boolean value) {
if (value != ignoreMineLP) {
ignoreMineLP = value;
save();
}
}
public void setPreferredRace(Race race) {
if (preferredRace != race) {
preferredRace = race;
save();
}
}
public Race getPrefferedRace() {
if (preferredRace == null) {
setPreferredRace(Race.EARTH);
}
return preferredRace;
}
public void save() {
try {
Files.deleteIfExists(file);
} catch (IOException e1) {
e1.printStackTrace();
}
try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(file))) {
2019-01-27 18:36:41 +01:00
writer.setIndent(" ");
2020-01-17 14:27:26 +01:00
gson.toJson(this, Config.class, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}