2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia;
|
2019-01-13 21:07:44 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-01-27 17:37:22 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2019-01-13 21:07:44 +01:00
|
|
|
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 {
|
2019-01-13 21:07:44 +01:00
|
|
|
|
2020-04-24 15:23:36 +02:00
|
|
|
private static Config INSTANCE = new Config();
|
2019-01-13 21:07:44 +01:00
|
|
|
|
2020-04-24 15:23:36 +02:00
|
|
|
private static final Gson GSON = new GsonBuilder()
|
2019-01-13 21:07:44 +01:00
|
|
|
.excludeFieldsWithoutExposeAnnotation()
|
|
|
|
.setPrettyPrinting()
|
|
|
|
.create();
|
|
|
|
|
2020-04-15 18:12:00 +02:00
|
|
|
public static Config getInstance() {
|
2020-04-24 15:23:36 +02:00
|
|
|
return INSTANCE;
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 18:12:00 +02:00
|
|
|
static void init(Path directory) {
|
2020-01-27 17:37:22 +01:00
|
|
|
Path file = directory.resolve("unicopia.json");
|
2019-01-13 21:07:44 +01:00
|
|
|
|
|
|
|
try {
|
2020-01-27 17:37:22 +01:00
|
|
|
if (Files.exists(file)) {
|
|
|
|
try(JsonReader reader = new JsonReader(Files.newBufferedReader(file))) {
|
2020-04-24 15:23:36 +02:00
|
|
|
INSTANCE = GSON.fromJson(reader, Config.class);
|
2019-01-27 18:36:41 +01:00
|
|
|
}
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
2020-04-24 15:23:36 +02:00
|
|
|
if (INSTANCE == null) {
|
|
|
|
INSTANCE = new Config();
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:23:36 +02:00
|
|
|
INSTANCE.file = file;
|
|
|
|
INSTANCE.save();
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 17:37:22 +01:00
|
|
|
private Path file;
|
2019-01-13 21:07:44 +01:00
|
|
|
|
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() {
|
2020-01-27 17:37:22 +01:00
|
|
|
try {
|
|
|
|
Files.deleteIfExists(file);
|
|
|
|
} catch (IOException e1) {
|
|
|
|
e1.printStackTrace();
|
2019-01-13 21:07:44 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 17:37:22 +01:00
|
|
|
try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(file))) {
|
2019-01-27 18:36:41 +01:00
|
|
|
writer.setIndent(" ");
|
|
|
|
|
2020-04-24 15:23:36 +02:00
|
|
|
GSON.toJson(this, Config.class, writer);
|
2019-01-13 21:07:44 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|