From ef333d4dca897013e95cadc2a8f110d6e1aab50a Mon Sep 17 00:00:00 2001 From: Aleksey Date: Tue, 29 Dec 2020 22:42:06 +0300 Subject: [PATCH 1/3] One more config --- src/main/java/ru/betterend/config/Config.java | 10 +- .../ru/betterend/config/ConfigKeeper.java | 62 +++- .../java/ru/betterend/config/Configs.java | 12 +- .../ru/betterend/config/DeprecatedConfig.java | 257 +++++++++++++ .../ru/betterend/config/SimpleConfig.java | 344 +++++------------- .../world/generator/TerrainGenerator.java | 4 +- 6 files changed, 399 insertions(+), 290 deletions(-) create mode 100644 src/main/java/ru/betterend/config/DeprecatedConfig.java diff --git a/src/main/java/ru/betterend/config/Config.java b/src/main/java/ru/betterend/config/Config.java index 42a37be1..89fb3147 100644 --- a/src/main/java/ru/betterend/config/Config.java +++ b/src/main/java/ru/betterend/config/Config.java @@ -2,8 +2,6 @@ package ru.betterend.config; import org.jetbrains.annotations.Nullable; -import com.google.gson.JsonObject; - import ru.betterend.BetterEnd; import ru.betterend.config.ConfigKeeper.BooleanEntry; import ru.betterend.config.ConfigKeeper.Entry; @@ -15,20 +13,16 @@ import ru.betterend.config.ConfigKeeper.StringEntry; public abstract class Config { protected final ConfigKeeper keeper; - protected final ConfigWriter writer; protected abstract void registerEntries(); public Config(String group) { - this.writer = new ConfigWriter(group); - JsonObject settings = writer.load(); - this.keeper = new ConfigKeeper(settings); + this.keeper = new ConfigKeeper(group); this.registerEntries(); - this.writer.save(); } public void saveChanges() { - this.writer.save(); + this.keeper.save(); } @Nullable diff --git a/src/main/java/ru/betterend/config/ConfigKeeper.java b/src/main/java/ru/betterend/config/ConfigKeeper.java index 20b110c4..8db36380 100644 --- a/src/main/java/ru/betterend/config/ConfigKeeper.java +++ b/src/main/java/ru/betterend/config/ConfigKeeper.java @@ -18,14 +18,50 @@ import ru.betterend.util.JsonFactory; public final class ConfigKeeper { private Map> configEntries = Maps.newHashMap(); - private final JsonObject configObject; - public ConfigKeeper(JsonObject config) { - this.configObject = config; + private final JsonObject configObject; + private final ConfigWriter writer; + + private boolean changed = false; + + public ConfigKeeper(String group) { + this.writer = new ConfigWriter(group); + this.configObject = writer.load(); + } + + public void save() { + if (!changed) return; + this.writer.save(); + this.changed = false; + } + + private > boolean has(ConfigKey key, E entry) { + if (configObject == null) { + return false; + } + String group = key.getOwner(); + if (!configObject.has(group)) { + return false; + } + JsonObject jsonGroup = JsonHelper.getObject(configObject, group); + String category = key.getCategory(); + if (!jsonGroup.has(category)) { + return false; + } + JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category); + String paramKey = key.getEntry(); + paramKey += " [default: " + entry.getDefault() + "]"; + return jsonCategory.has(paramKey); } private > void storeValue(ConfigKey key, E entry, T value) { - if (configObject == null) return; + if (configObject == null) { + return; + } + if (has(key, entry)) { + T val = entry.getValue(); + if (value.equals(val)) return; + } String group = key.getOwner(); JsonObject jsonGroup; @@ -46,30 +82,20 @@ public final class ConfigKeeper { String paramKey = key.getEntry(); paramKey += " [default: " + entry.getDefault() + "]"; entry.toJson(jsonCategory, paramKey, value); + this.changed = true; } private > T getValue(ConfigKey key, E entry) { - if (configObject == null) { + if (!has(key, entry)) { return entry.getDefault(); } - String group = key.getOwner(); - if (!configObject.has(group)) { - return entry.getDefault(); - } - - JsonObject jsonGroup = JsonHelper.getObject(configObject, group); String category = key.getCategory(); - if (!jsonGroup.has(category)) { - return entry.getDefault(); - } - + JsonObject jsonGroup = JsonHelper.getObject(configObject, group); JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category); + String paramKey = key.getEntry(); paramKey += " [default: " + entry.getDefault() + "]"; - if (!jsonCategory.has(paramKey)) { - return entry.getDefault(); - } return entry.fromJson(jsonCategory.get(paramKey)); } diff --git a/src/main/java/ru/betterend/config/Configs.java b/src/main/java/ru/betterend/config/Configs.java index 6bef1de2..ddee0536 100644 --- a/src/main/java/ru/betterend/config/Configs.java +++ b/src/main/java/ru/betterend/config/Configs.java @@ -1,17 +1,17 @@ package ru.betterend.config; public class Configs { - public static final IdConfig ITEM_CONFIG = new CategoryConfig("items"); - public static final IdConfig BLOCK_CONFIG = new CategoryConfig("blocks"); public static final IdConfig ENTITY_CONFIG = new CategoryConfig("entities"); + public static final IdConfig BLOCK_CONFIG = new CategoryConfig("blocks"); + public static final SimpleConfig GENERAL = new SimpleConfig("settings"); + public static final IdConfig ITEM_CONFIG = new CategoryConfig("items"); public static final IdConfig BIOME_CONFIG = new EntryConfig("biomes"); - public static final SimpleConfig GENERATOR_CONFIG = new SimpleConfig("generator"); public static void saveConfigs() { - ITEM_CONFIG.saveChanges(); + ENTITY_CONFIG.saveChanges(); BLOCK_CONFIG.saveChanges(); BIOME_CONFIG.saveChanges(); - ENTITY_CONFIG.saveChanges(); - GENERATOR_CONFIG.save(); + ITEM_CONFIG.saveChanges(); + GENERAL.saveChanges(); } } diff --git a/src/main/java/ru/betterend/config/DeprecatedConfig.java b/src/main/java/ru/betterend/config/DeprecatedConfig.java new file mode 100644 index 00000000..f1f109e4 --- /dev/null +++ b/src/main/java/ru/betterend/config/DeprecatedConfig.java @@ -0,0 +1,257 @@ +package ru.betterend.config; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map.Entry; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import ru.betterend.BetterEnd; + +@Deprecated +public final class DeprecatedConfig { + private boolean rewrite = false; + private final String name; + private JsonObject config; + + public DeprecatedConfig(String name) { + this.name = name; + } + + private void load() { + if (config == null) { + File file = getFolder(); + if (!file.exists()) + file.mkdirs(); + file = getFile(); + if (file.exists()) { + Gson gson = new Gson(); + try { + Reader reader = new FileReader(file); + config = gson.fromJson(reader, JsonObject.class); + if (config == null) { + config = new JsonObject(); + rewrite = true; + } + else { + rewrite = false; + } + } + catch (FileNotFoundException e) { + e.printStackTrace(); + config = new JsonObject(); + rewrite = true; + } + } + else { + config = new JsonObject(); + rewrite = true; + } + } + } + + public void save() { + if (rewrite) { + File file = getFolder(); + if (!file.exists()) + file.mkdirs(); + file = getFile(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + try { + FileWriter writer = new FileWriter(file); + String gstring = gson.toJson(config); + writer.write(gstring); + writer.flush(); + writer.close(); + rewrite = false; + } + catch (IOException e) { + e.printStackTrace(); + } + } + } + + private File getFile() { + return new File(String.format("./config/%s/%s.json", BetterEnd.MOD_ID, name)); + } + + private File getFolder() { + return new File("./config/" + BetterEnd.MOD_ID + "/"); + } + + public boolean getBoolean(String groups, String name, boolean def) { + load(); + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + JsonElement element = group.get(name); + + if (element != null) { + return element.getAsBoolean(); + } + else { + group.addProperty(name, def); + rewrite = true; + return def; + } + } + + public void setBoolean(String groups, String name, boolean def, boolean value) { + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + group.addProperty(name, value); + + rewrite = true; + } + + public float getFloat(String groups, String name, float def) { + load(); + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + JsonElement element = group.get(name); + + if (element != null) { + return element.getAsFloat(); + } + else { + group.addProperty(name, def); + rewrite = true; + return def; + } + } + + public void setFloat(String groups, String name, float def, float value) { + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + group.addProperty(name, value); + + rewrite = true; + } + + public int getInt(String groups, String name, int def) { + load(); + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + JsonElement element = group.get(name); + + if (element != null) { + return element.getAsInt(); + } + else { + group.addProperty(name, def); + rewrite = true; + return def; + } + } + + public String getString(String groups, String name, String def) { + load(); + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + JsonElement element = group.get(name); + + if (element != null) { + return element.getAsString(); + } + else { + group.addProperty(name, def); + rewrite = true; + return def; + } + } + + public void setInt(String groups, String name, int def, int value) { + name += "[def: " + def + "]"; + + JsonObject group = getGroup(groups); + group.addProperty(name, value); + + rewrite = true; + } + + public void setStringLoad(String groups, String name, String value) { + JsonObject group = getGroup(groups); + group.addProperty(name, value); + } + + public String[] getStringArray(String groups, String name, String[] def) { + load(); + + JsonObject group = getGroup(groups); + JsonElement element = group.get(name); + + if (element != null) { + return toStringArray(element.getAsJsonArray()); + } + else { + group.add(name, toJsonArray(def)); + rewrite = true; + return def; + } + } + + private String[] toStringArray(JsonArray array) { + load(); + String[] result = new String[array.size()]; + for (int i = 0; i < array.size(); i++) + result[i] = array.get(i).getAsString(); + return result; + } + + private JsonArray toJsonArray(String[] array) { + load(); + JsonArray result = new JsonArray(); + for (String s : array) + result.add(s); + return result; + } + + public JsonObject getGroup(String groups) { + JsonObject obj = config; + String[] groupsArr = groups.split("\\."); + for (String group : groupsArr) { + JsonObject jGroup = obj.getAsJsonObject(group); + if (jGroup == null) { + jGroup = new JsonObject(); + obj.add(group, jGroup); + } + obj = jGroup; + } + return obj; + } + + public List getBaseGroups() { + List groups = new ArrayList(); + Iterator> iterator = config.entrySet().iterator(); + iterator.forEachRemaining((element) -> { + groups.add(element.getKey()); + }); + return groups; + } + + public List> getGroupMembers(JsonObject group) { + List> result = new ArrayList>(); + result.addAll(group.entrySet()); + return result; + } + + public void markToSave() { + rewrite = true; + } +} diff --git a/src/main/java/ru/betterend/config/SimpleConfig.java b/src/main/java/ru/betterend/config/SimpleConfig.java index 47974e0b..463857a6 100644 --- a/src/main/java/ru/betterend/config/SimpleConfig.java +++ b/src/main/java/ru/betterend/config/SimpleConfig.java @@ -1,256 +1,88 @@ -package ru.betterend.config; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import ru.betterend.BetterEnd; - -public final class SimpleConfig { - private boolean rewrite = false; - private final String name; - private JsonObject config; - - public SimpleConfig(String name) { - this.name = name; - } - - private void load() { - if (config == null) { - File file = getFolder(); - if (!file.exists()) - file.mkdirs(); - file = getFile(); - if (file.exists()) { - Gson gson = new Gson(); - try { - Reader reader = new FileReader(file); - config = gson.fromJson(reader, JsonObject.class); - if (config == null) { - config = new JsonObject(); - rewrite = true; - } - else { - rewrite = false; - } - } - catch (FileNotFoundException e) { - e.printStackTrace(); - config = new JsonObject(); - rewrite = true; - } - } - else { - config = new JsonObject(); - rewrite = true; - } - } - } - - public void save() { - if (rewrite) { - File file = getFolder(); - if (!file.exists()) - file.mkdirs(); - file = getFile(); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - try { - FileWriter writer = new FileWriter(file); - String gstring = gson.toJson(config); - writer.write(gstring); - writer.flush(); - writer.close(); - rewrite = false; - } - catch (IOException e) { - e.printStackTrace(); - } - } - } - - private File getFile() { - return new File(String.format("./config/%s/%s.json", BetterEnd.MOD_ID, name)); - } - - private File getFolder() { - return new File("./config/" + BetterEnd.MOD_ID + "/"); - } - - public boolean getBoolean(String groups, String name, boolean def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsBoolean(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public void setBoolean(String groups, String name, boolean def, boolean value) { - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - group.addProperty(name, value); - - rewrite = true; - } - - public float getFloat(String groups, String name, float def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsFloat(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public void setFloat(String groups, String name, float def, float value) { - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - group.addProperty(name, value); - - rewrite = true; - } - - public int getInt(String groups, String name, int def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsInt(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public String getString(String groups, String name, String def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsString(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public void setInt(String groups, String name, int def, int value) { - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - group.addProperty(name, value); - - rewrite = true; - } - - public void setStringLoad(String groups, String name, String value) { - JsonObject group = getGroup(groups); - group.addProperty(name, value); - } - - public String[] getStringArray(String groups, String name, String[] def) { - load(); - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return toStringArray(element.getAsJsonArray()); - } - else { - group.add(name, toJsonArray(def)); - rewrite = true; - return def; - } - } - - private String[] toStringArray(JsonArray array) { - load(); - String[] result = new String[array.size()]; - for (int i = 0; i < array.size(); i++) - result[i] = array.get(i).getAsString(); - return result; - } - - private JsonArray toJsonArray(String[] array) { - load(); - JsonArray result = new JsonArray(); - for (String s : array) - result.add(s); - return result; - } - - public JsonObject getGroup(String groups) { - JsonObject obj = config; - String[] groupsArr = groups.split("\\."); - for (String group : groupsArr) { - JsonObject jGroup = obj.getAsJsonObject(group); - if (jGroup == null) { - jGroup = new JsonObject(); - obj.add(group, jGroup); - } - obj = jGroup; - } - return obj; - } - - public List getBaseGroups() { - List groups = new ArrayList(); - Iterator> iterator = config.entrySet().iterator(); - iterator.forEachRemaining((element) -> { - groups.add(element.getKey()); - }); - return groups; - } - - public List> getGroupMembers(JsonObject group) { - List> result = new ArrayList>(); - result.addAll(group.entrySet()); - return result; - } - - public void markToSave() { - rewrite = true; - } -} +package ru.betterend.config; + +import org.jetbrains.annotations.Nullable; + +import ru.betterend.BetterEnd; +import ru.betterend.config.ConfigKeeper.Entry; +import ru.betterend.config.ConfigKeeper.FloatRange; +import ru.betterend.config.ConfigKeeper.IntegerRange; + +public class SimpleConfig extends Config { + + public SimpleConfig(String group) { + super(group); + } + + @Override + protected void registerEntries() {} + + protected ConfigKey createKey(String category, String key) { + return new ConfigKey(BetterEnd.MOD_ID, category, key); + } + + @Nullable + public > E getEntry(String category, String key, Class type) { + return this.getEntry(createKey(category, key), type); + } + + @Nullable + public > T getDefault(String category, String key, Class type) { + return this.getDefault(createKey(category, key), type); + } + + public String getString(String category, String key, String defaultValue) { + return this.getString(createKey(category, key), defaultValue); + } + + public String getString(String category, String key) { + return this.getString(createKey(category, key)); + } + + public boolean setString(String category, String key, String value) { + return this.setString(createKey(category, key), value); + } + + public int getInt(String category, String key, int defaultValue) { + return this.getInt(createKey(category, key), defaultValue); + } + + public int getInt(String category, String key) { + return this.getInt(createKey(category, key)); + } + + public boolean setInt(String category, String key, int value) { + return this.setInt(createKey(category, key), value); + } + + public boolean setRangedInt(String category, String key, int value) { + return this.setRanged(createKey(category, key), value, IntegerRange.class); + } + + public boolean setRangedFloat(String category, String key, float value) { + return this.setRanged(createKey(category, key), value, FloatRange.class); + } + + public float getFloat(String category, String key, float defaultValue) { + return this.getFloat(createKey(category, key), defaultValue); + } + + public float getFloat(String category, String key) { + return this.getFloat(createKey(category, key)); + } + + public boolean setFloat(String category, String key, float value) { + return this.setFloat(createKey(category, key), value); + } + + public boolean getBoolean(String category, String key, boolean defaultValue) { + return this.getBoolean(createKey(category, key), defaultValue); + } + + public boolean getBoolean(String category, String key) { + return this.getBoolean(createKey(category, key)); + } + + public boolean setBoolean(String category, String key, boolean value) { + return this.setBoolean(createKey(category, key), value); + } +} diff --git a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java index abe22eca..6ce5b425 100644 --- a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java +++ b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java @@ -22,8 +22,8 @@ public class TerrainGenerator { private static boolean noRingVoid; public static void init() { - newGenerator = Configs.GENERATOR_CONFIG.getBoolean("generator", "useNewGenerator", false); - noRingVoid = Configs.GENERATOR_CONFIG.getBoolean("generator", "noRingVoid", false); + newGenerator = Configs.GENERAL.getBoolean("generator", "useNewGenerator", false); + noRingVoid = Configs.GENERAL.getBoolean("generator", "noRingVoid", false); } public static void initNoise(long seed) { From ed777b59551e2fd5d406e0c2ae6393f2e99d846a Mon Sep 17 00:00:00 2001 From: Aleksey Date: Wed, 30 Dec 2020 21:58:05 +0300 Subject: [PATCH 2/3] Config optimization --- .../ru/betterend/config/ConfigKeeper.java | 135 +++++---- .../ru/betterend/config/DeprecatedConfig.java | 257 ------------------ 2 files changed, 64 insertions(+), 328 deletions(-) delete mode 100644 src/main/java/ru/betterend/config/DeprecatedConfig.java diff --git a/src/main/java/ru/betterend/config/ConfigKeeper.java b/src/main/java/ru/betterend/config/ConfigKeeper.java index 8db36380..f35babc1 100644 --- a/src/main/java/ru/betterend/config/ConfigKeeper.java +++ b/src/main/java/ru/betterend/config/ConfigKeeper.java @@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable; import com.google.common.collect.Maps; import com.google.common.reflect.TypeToken; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.util.JsonHelper; @@ -35,34 +34,10 @@ public final class ConfigKeeper { this.changed = false; } - private > boolean has(ConfigKey key, E entry) { - if (configObject == null) { - return false; - } - String group = key.getOwner(); - if (!configObject.has(group)) { - return false; - } - JsonObject jsonGroup = JsonHelper.getObject(configObject, group); - String category = key.getCategory(); - if (!jsonGroup.has(category)) { - return false; - } - JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category); - String paramKey = key.getEntry(); - paramKey += " [default: " + entry.getDefault() + "]"; - return jsonCategory.has(paramKey); - } - - private > void storeValue(ConfigKey key, E entry, T value) { + private > void initializeEntry(ConfigKey key, E entry) { if (configObject == null) { return; } - if (has(key, entry)) { - T val = entry.getValue(); - if (value.equals(val)) return; - } - String group = key.getOwner(); JsonObject jsonGroup; if (configObject.has(group)) { @@ -81,23 +56,24 @@ public final class ConfigKeeper { } String paramKey = key.getEntry(); paramKey += " [default: " + entry.getDefault() + "]"; - entry.toJson(jsonCategory, paramKey, value); + this.changed = entry.setLocation(jsonCategory, paramKey); + } + + private > void storeValue(E entry, T value) { + if (configObject == null) { + return; + } + T val = entry.getValue(); + if (value.equals(val)) return; + entry.toJson(value); this.changed = true; } - private > T getValue(ConfigKey key, E entry) { - if (!has(key, entry)) { + private > T getValue(E entry) { + if (!entry.hasLocation()) { return entry.getDefault(); } - String group = key.getOwner(); - String category = key.getCategory(); - JsonObject jsonGroup = JsonHelper.getObject(configObject, group); - JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category); - - String paramKey = key.getEntry(); - paramKey += " [default: " + entry.getDefault() + "]"; - - return entry.fromJson(jsonCategory.get(paramKey)); + return entry.fromJson(); } @Nullable @@ -119,9 +95,9 @@ public final class ConfigKeeper { } public > E registerEntry(ConfigKey key, E entry) { - entry.setWriter(value -> this.storeValue(key, entry, value)); - entry.setReader(() -> { return this.getValue(key, entry); }); - this.storeValue(key, entry, entry.getValue()); + entry.setWriter(value -> this.storeValue(entry, value)); + entry.setReader(() -> { return this.getValue(entry); }); + this.initializeEntry(key, entry); this.configEntries.put(key, entry); return entry; } @@ -133,13 +109,13 @@ public final class ConfigKeeper { } @Override - public Boolean fromJson(JsonElement json) { - return json.getAsBoolean(); + public Boolean fromJson() { + return JsonHelper.getBoolean(location, key, defaultValue); } @Override - public void toJson(JsonObject json, String key, Boolean value) { - json.addProperty(key, value); + public void toJson(Boolean value) { + this.location.addProperty(key, value); } } @@ -150,13 +126,13 @@ public final class ConfigKeeper { } @Override - public Float fromJson(JsonElement json) { - return json.getAsFloat(); + public Float fromJson() { + return JsonHelper.getFloat(location, key, defaultValue); } @Override - public void toJson(JsonObject json, String key, Float value) { - json.addProperty(key, value); + public void toJson(Float value) { + this.location.addProperty(key, value); } } @@ -167,13 +143,13 @@ public final class ConfigKeeper { } @Override - public Float fromJson(JsonElement json) { - return json.getAsFloat(); + public Float fromJson() { + return JsonHelper.getFloat(location, key, defaultValue); } @Override - public void toJson(JsonObject json, String key, Float value) { - json.addProperty(key, value); + public void toJson(Float value) { + this.location.addProperty(key, value); } } @@ -189,13 +165,13 @@ public final class ConfigKeeper { } @Override - public Integer fromJson(JsonElement json) { - return json.getAsInt(); + public Integer fromJson() { + return JsonHelper.getInt(location, key, defaultValue); } @Override - public void toJson(JsonObject json, String key, Integer value) { - json.addProperty(key, value); + public void toJson(Integer value) { + this.location.addProperty(key, value); } } @@ -206,13 +182,13 @@ public final class ConfigKeeper { } @Override - public Integer fromJson(JsonElement json) { - return json.getAsInt(); + public Integer fromJson() { + return JsonHelper.getInt(location, key, defaultValue); } @Override - public void toJson(JsonObject json, String key, Integer value) { - json.addProperty(key, value); + public void toJson(Integer value) { + this.location.addProperty(key, value); } } @@ -223,13 +199,13 @@ public final class ConfigKeeper { } @Override - public String fromJson(JsonElement json) { - return json.getAsString(); + public String fromJson() { + return JsonHelper.getString(location, key, defaultValue); } @Override - public void toJson(JsonObject json, String key, String value) { - json.addProperty(key, value); + public void toJson(String value) { + this.location.addProperty(key, value); } } @@ -252,13 +228,13 @@ public final class ConfigKeeper { } @Override - public T fromJson(JsonElement json) { - return JsonFactory.GSON.fromJson(json, type); + public T fromJson() { + return JsonFactory.GSON.fromJson(location.get(key), type); } @Override - public void toJson(JsonObject json, String key, T value) { - json.addProperty(key, JsonFactory.GSON.toJson(json, type)); + public void toJson(T value) { + location.addProperty(key, JsonFactory.GSON.toJson(value, type)); } } @@ -291,9 +267,11 @@ public final class ConfigKeeper { protected final T defaultValue; protected Consumer writer; protected Supplier reader; + protected JsonObject location; + protected String key; - public abstract T fromJson(JsonElement json); - public abstract void toJson(JsonObject json, String key, T value); + public abstract T fromJson(); + public abstract void toJson(T value); public Entry (T defaultValue) { this.defaultValue = defaultValue; @@ -307,6 +285,21 @@ public final class ConfigKeeper { this.reader = reader; } + protected boolean setLocation(JsonObject location, String key) { + this.location = location; + this.key = key; + if (!location.has(key)) { + this.toJson(defaultValue); + return true; + } + return false; + } + + protected boolean hasLocation() { + return this.location != null && + this.key != null; + } + public T getValue() { return this.reader.get(); } diff --git a/src/main/java/ru/betterend/config/DeprecatedConfig.java b/src/main/java/ru/betterend/config/DeprecatedConfig.java deleted file mode 100644 index f1f109e4..00000000 --- a/src/main/java/ru/betterend/config/DeprecatedConfig.java +++ /dev/null @@ -1,257 +0,0 @@ -package ru.betterend.config; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import ru.betterend.BetterEnd; - -@Deprecated -public final class DeprecatedConfig { - private boolean rewrite = false; - private final String name; - private JsonObject config; - - public DeprecatedConfig(String name) { - this.name = name; - } - - private void load() { - if (config == null) { - File file = getFolder(); - if (!file.exists()) - file.mkdirs(); - file = getFile(); - if (file.exists()) { - Gson gson = new Gson(); - try { - Reader reader = new FileReader(file); - config = gson.fromJson(reader, JsonObject.class); - if (config == null) { - config = new JsonObject(); - rewrite = true; - } - else { - rewrite = false; - } - } - catch (FileNotFoundException e) { - e.printStackTrace(); - config = new JsonObject(); - rewrite = true; - } - } - else { - config = new JsonObject(); - rewrite = true; - } - } - } - - public void save() { - if (rewrite) { - File file = getFolder(); - if (!file.exists()) - file.mkdirs(); - file = getFile(); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - try { - FileWriter writer = new FileWriter(file); - String gstring = gson.toJson(config); - writer.write(gstring); - writer.flush(); - writer.close(); - rewrite = false; - } - catch (IOException e) { - e.printStackTrace(); - } - } - } - - private File getFile() { - return new File(String.format("./config/%s/%s.json", BetterEnd.MOD_ID, name)); - } - - private File getFolder() { - return new File("./config/" + BetterEnd.MOD_ID + "/"); - } - - public boolean getBoolean(String groups, String name, boolean def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsBoolean(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public void setBoolean(String groups, String name, boolean def, boolean value) { - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - group.addProperty(name, value); - - rewrite = true; - } - - public float getFloat(String groups, String name, float def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsFloat(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public void setFloat(String groups, String name, float def, float value) { - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - group.addProperty(name, value); - - rewrite = true; - } - - public int getInt(String groups, String name, int def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsInt(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public String getString(String groups, String name, String def) { - load(); - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return element.getAsString(); - } - else { - group.addProperty(name, def); - rewrite = true; - return def; - } - } - - public void setInt(String groups, String name, int def, int value) { - name += "[def: " + def + "]"; - - JsonObject group = getGroup(groups); - group.addProperty(name, value); - - rewrite = true; - } - - public void setStringLoad(String groups, String name, String value) { - JsonObject group = getGroup(groups); - group.addProperty(name, value); - } - - public String[] getStringArray(String groups, String name, String[] def) { - load(); - - JsonObject group = getGroup(groups); - JsonElement element = group.get(name); - - if (element != null) { - return toStringArray(element.getAsJsonArray()); - } - else { - group.add(name, toJsonArray(def)); - rewrite = true; - return def; - } - } - - private String[] toStringArray(JsonArray array) { - load(); - String[] result = new String[array.size()]; - for (int i = 0; i < array.size(); i++) - result[i] = array.get(i).getAsString(); - return result; - } - - private JsonArray toJsonArray(String[] array) { - load(); - JsonArray result = new JsonArray(); - for (String s : array) - result.add(s); - return result; - } - - public JsonObject getGroup(String groups) { - JsonObject obj = config; - String[] groupsArr = groups.split("\\."); - for (String group : groupsArr) { - JsonObject jGroup = obj.getAsJsonObject(group); - if (jGroup == null) { - jGroup = new JsonObject(); - obj.add(group, jGroup); - } - obj = jGroup; - } - return obj; - } - - public List getBaseGroups() { - List groups = new ArrayList(); - Iterator> iterator = config.entrySet().iterator(); - iterator.forEachRemaining((element) -> { - groups.add(element.getKey()); - }); - return groups; - } - - public List> getGroupMembers(JsonObject group) { - List> result = new ArrayList>(); - result.addAll(group.entrySet()); - return result; - } - - public void markToSave() { - rewrite = true; - } -} From 42f04b9614501f7a85313b5ca9152f1762fdad70 Mon Sep 17 00:00:00 2001 From: Aleksey Date: Wed, 30 Dec 2020 23:13:39 +0300 Subject: [PATCH 3/3] A little bit refactoring --- .../{compat => integration}/rei/REIAlloyingCategory.java | 2 +- .../{compat => integration}/rei/REIAlloyingDisplay.java | 2 +- .../{compat => integration}/rei/REIAnvilCategory.java | 2 +- .../{compat => integration}/rei/REIAnvilDisplay.java | 2 +- .../betterend/{compat => integration}/rei/REIContainer.java | 2 +- .../{compat => integration}/rei/REIInfusionCategory.java | 2 +- .../{compat => integration}/rei/REIInfusionDisplay.java | 2 +- .../ru/betterend/{compat => integration}/rei/REIPlugin.java | 2 +- src/main/resources/fabric.mod.json | 4 ++-- 9 files changed, 10 insertions(+), 10 deletions(-) rename src/main/java/ru/betterend/{compat => integration}/rei/REIAlloyingCategory.java (98%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIAlloyingDisplay.java (98%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIAnvilCategory.java (98%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIAnvilDisplay.java (97%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIContainer.java (92%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIInfusionCategory.java (99%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIInfusionDisplay.java (98%) rename src/main/java/ru/betterend/{compat => integration}/rei/REIPlugin.java (98%) diff --git a/src/main/java/ru/betterend/compat/rei/REIAlloyingCategory.java b/src/main/java/ru/betterend/integration/rei/REIAlloyingCategory.java similarity index 98% rename from src/main/java/ru/betterend/compat/rei/REIAlloyingCategory.java rename to src/main/java/ru/betterend/integration/rei/REIAlloyingCategory.java index f3ea48b9..67ddf9ba 100644 --- a/src/main/java/ru/betterend/compat/rei/REIAlloyingCategory.java +++ b/src/main/java/ru/betterend/integration/rei/REIAlloyingCategory.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import java.text.DecimalFormat; import java.util.List; diff --git a/src/main/java/ru/betterend/compat/rei/REIAlloyingDisplay.java b/src/main/java/ru/betterend/integration/rei/REIAlloyingDisplay.java similarity index 98% rename from src/main/java/ru/betterend/compat/rei/REIAlloyingDisplay.java rename to src/main/java/ru/betterend/integration/rei/REIAlloyingDisplay.java index b5a91a95..cecd66b0 100644 --- a/src/main/java/ru/betterend/compat/rei/REIAlloyingDisplay.java +++ b/src/main/java/ru/betterend/integration/rei/REIAlloyingDisplay.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import java.util.Collections; import java.util.List; diff --git a/src/main/java/ru/betterend/compat/rei/REIAnvilCategory.java b/src/main/java/ru/betterend/integration/rei/REIAnvilCategory.java similarity index 98% rename from src/main/java/ru/betterend/compat/rei/REIAnvilCategory.java rename to src/main/java/ru/betterend/integration/rei/REIAnvilCategory.java index 82172fc7..88c9363b 100644 --- a/src/main/java/ru/betterend/compat/rei/REIAnvilCategory.java +++ b/src/main/java/ru/betterend/integration/rei/REIAnvilCategory.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import java.util.Collections; import java.util.List; diff --git a/src/main/java/ru/betterend/compat/rei/REIAnvilDisplay.java b/src/main/java/ru/betterend/integration/rei/REIAnvilDisplay.java similarity index 97% rename from src/main/java/ru/betterend/compat/rei/REIAnvilDisplay.java rename to src/main/java/ru/betterend/integration/rei/REIAnvilDisplay.java index 2de2d777..df870fc7 100644 --- a/src/main/java/ru/betterend/compat/rei/REIAnvilDisplay.java +++ b/src/main/java/ru/betterend/integration/rei/REIAnvilDisplay.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import java.util.Collections; import java.util.List; diff --git a/src/main/java/ru/betterend/compat/rei/REIContainer.java b/src/main/java/ru/betterend/integration/rei/REIContainer.java similarity index 92% rename from src/main/java/ru/betterend/compat/rei/REIContainer.java rename to src/main/java/ru/betterend/integration/rei/REIContainer.java index dae79d5f..9d6a0118 100644 --- a/src/main/java/ru/betterend/compat/rei/REIContainer.java +++ b/src/main/java/ru/betterend/integration/rei/REIContainer.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import me.shedaniel.rei.plugin.containers.CraftingContainerInfoWrapper; import me.shedaniel.rei.server.ContainerInfoHandler; diff --git a/src/main/java/ru/betterend/compat/rei/REIInfusionCategory.java b/src/main/java/ru/betterend/integration/rei/REIInfusionCategory.java similarity index 99% rename from src/main/java/ru/betterend/compat/rei/REIInfusionCategory.java rename to src/main/java/ru/betterend/integration/rei/REIInfusionCategory.java index c304659b..6821a790 100644 --- a/src/main/java/ru/betterend/compat/rei/REIInfusionCategory.java +++ b/src/main/java/ru/betterend/integration/rei/REIInfusionCategory.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import java.util.List; diff --git a/src/main/java/ru/betterend/compat/rei/REIInfusionDisplay.java b/src/main/java/ru/betterend/integration/rei/REIInfusionDisplay.java similarity index 98% rename from src/main/java/ru/betterend/compat/rei/REIInfusionDisplay.java rename to src/main/java/ru/betterend/integration/rei/REIInfusionDisplay.java index 644b0d3c..9e77e114 100644 --- a/src/main/java/ru/betterend/compat/rei/REIInfusionDisplay.java +++ b/src/main/java/ru/betterend/integration/rei/REIInfusionDisplay.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import java.util.Collections; import java.util.List; diff --git a/src/main/java/ru/betterend/compat/rei/REIPlugin.java b/src/main/java/ru/betterend/integration/rei/REIPlugin.java similarity index 98% rename from src/main/java/ru/betterend/compat/rei/REIPlugin.java rename to src/main/java/ru/betterend/integration/rei/REIPlugin.java index d4b2365d..ee84dea5 100644 --- a/src/main/java/ru/betterend/compat/rei/REIPlugin.java +++ b/src/main/java/ru/betterend/integration/rei/REIPlugin.java @@ -1,4 +1,4 @@ -package ru.betterend.compat.rei; +package ru.betterend.integration.rei; import me.shedaniel.rei.api.EntryStack; import me.shedaniel.rei.api.RecipeHelper; diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 91334346..f7500c1d 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -26,10 +26,10 @@ "ru.betterend.client.BetterEndClient" ], "rei_plugins": [ - "ru.betterend.compat.rei.REIPlugin" + "ru.betterend.integration.rei.REIPlugin" ], "rei_containers": [ - "ru.betterend.compat.rei.REIContainer" + "ru.betterend.integration.rei.REIContainer" ] }, "mixins": [