2019-01-13 21:07:44 +01:00
|
|
|
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 {
|
|
|
|
|
2019-01-27 20:34:02 +01:00
|
|
|
private static UConfig instance = new UConfig();
|
2019-01-13 21:07:44 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
} 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();
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private File file;
|
|
|
|
|
2019-01-27 20:34:02 +01:00
|
|
|
@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.";
|
2019-01-13 21:07:44 +01:00
|
|
|
@Expose
|
|
|
|
private final List<Race> speciesWhiteList = Lists.newArrayList();
|
|
|
|
|
2019-01-27 20:34:02 +01:00
|
|
|
@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
|
2019-01-29 13:13:06 +01:00
|
|
|
private Race preferredRace = Race.EARTH;
|
2019-01-27 20:34:02 +01:00
|
|
|
|
|
|
|
@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;
|
|
|
|
|
2019-01-13 21:07:44 +01:00
|
|
|
public List<Race> getSpeciesWhiteList() {
|
|
|
|
return speciesWhiteList;
|
|
|
|
}
|
|
|
|
|
2019-01-27 20:34:02 +01:00
|
|
|
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) {
|
2019-01-29 13:13:06 +01:00
|
|
|
setPreferredRace(Race.EARTH);
|
2019-01-27 20:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return preferredRace;
|
|
|
|
}
|
|
|
|
|
2019-01-13 21:07:44 +01:00
|
|
|
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(" ");
|
|
|
|
|
2019-01-13 21:07:44 +01:00
|
|
|
gson.toJson(this, UConfig.class, writer);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|