aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/client/ConfigManager.java
diff options
context:
space:
mode:
authorUnknown <shekwancheung0528@gmail.com>2019-03-07 22:21:06 +0800
committerUnknown <shekwancheung0528@gmail.com>2019-03-07 22:21:06 +0800
commit8cd1f1a9804f980c1666079c99bafb6330c77723 (patch)
tree449f74c31bf73a4106b2cb9bc7fb5635925b2328 /src/main/java/me/shedaniel/rei/client/ConfigManager.java
parente5909b2fa40428d2a25a4f727a49a4f4fc47ad01 (diff)
downloadRoughlyEnoughItems-8cd1f1a9804f980c1666079c99bafb6330c77723.tar.gz
RoughlyEnoughItems-8cd1f1a9804f980c1666079c99bafb6330c77723.tar.bz2
RoughlyEnoughItems-8cd1f1a9804f980c1666079c99bafb6330c77723.zip
Config with comments
Diffstat (limited to 'src/main/java/me/shedaniel/rei/client/ConfigManager.java')
-rw-r--r--src/main/java/me/shedaniel/rei/client/ConfigManager.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/main/java/me/shedaniel/rei/client/ConfigManager.java b/src/main/java/me/shedaniel/rei/client/ConfigManager.java
new file mode 100644
index 000000000..c05442b41
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/ConfigManager.java
@@ -0,0 +1,111 @@
+package me.shedaniel.rei.client;
+
+import blue.endless.jankson.Jankson;
+import blue.endless.jankson.JsonObject;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import me.shedaniel.rei.RoughlyEnoughItemsCore;
+import net.fabricmc.loader.api.FabricLoader;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+
+public class ConfigManager implements me.shedaniel.rei.api.ConfigManager {
+
+ private static final Gson GSON = new GsonBuilder().create();
+ private static final Jankson JANKSON = Jankson.builder().build();
+ private final File configFile, oldConfigFile;
+ private ConfigObject config;
+ private boolean craftableOnly;
+
+ public ConfigManager() {
+ this.oldConfigFile = new File(FabricLoader.getInstance().getConfigDirectory(), "rei.json");
+ this.configFile = new File(FabricLoader.getInstance().getConfigDirectory(), "roughlyenoughitems/config.json");
+ this.craftableOnly = false;
+ try {
+ loadConfig();
+ RoughlyEnoughItemsCore.LOGGER.info("REI: Config is loaded.");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void saveConfig() throws IOException {
+ configFile.getParentFile().mkdirs();
+ if (!configFile.exists() && !configFile.createNewFile()) {
+ RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to save config! Overwriting with default config.");
+ config = new ConfigObject();
+ return;
+ }
+ try {
+ String result = JANKSON.toJson(config).toJson(true, true, 0);
+ if (!configFile.exists())
+ configFile.createNewFile();
+ FileOutputStream out = new FileOutputStream(configFile, false);
+
+ out.write(result.getBytes());
+ out.flush();
+ out.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to save config! Overwriting with default config.");
+ config = new ConfigObject();
+ return;
+ }
+ }
+
+ @Override
+ public void loadConfig() throws IOException {
+ configFile.getParentFile().mkdirs();
+ if (!configFile.exists() && oldConfigFile.exists()) {
+ RoughlyEnoughItemsCore.LOGGER.info("REI: Detected old config file, trying to move it.");
+ try {
+ Files.move(oldConfigFile.toPath(), configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+ } catch (Exception e) {
+ e.printStackTrace();
+ RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to move config file.");
+ }
+ }
+ if (!configFile.exists() || !configFile.canRead()) {
+ RoughlyEnoughItemsCore.LOGGER.warn("REI: Config not found! Creating one.");
+ config = new ConfigObject();
+ saveConfig();
+ return;
+ }
+ boolean failed = false;
+ try {
+ JsonObject configJson = JANKSON.load(configFile);
+ String regularized = configJson.toJson(false, false, 0);
+
+ config = GSON.fromJson(regularized, ConfigObject.class);
+ } catch (Exception e) {
+ e.printStackTrace();
+ failed = true;
+ }
+ if (failed || config == null) {
+ RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load config! Overwriting with default config.");
+ config = new ConfigObject();
+ }
+ saveConfig();
+ }
+
+ @Override
+ public ConfigObject getConfig() {
+ return config;
+ }
+
+ @Override
+ public boolean isCraftableOnlyEnabled() {
+ return craftableOnly;
+ }
+
+ @Override
+ public void toggleCraftableOnly() {
+ craftableOnly = !craftableOnly;
+ }
+
+}