Config refactor

This commit is contained in:
Aleksey 2020-12-12 00:49:35 +03:00
parent 58a389eaf2
commit fd06db2822
6 changed files with 134 additions and 183 deletions

View file

@ -8,19 +8,8 @@ import ru.betterend.world.biome.EndBiome;
public class BiomeConfig extends Config { public class BiomeConfig extends Config {
private final ConfigWriter writer;
private final String group = "biomes";
protected BiomeConfig() { protected BiomeConfig() {
this.writer = new ConfigWriter(group); super("biomes");
this.settings = writer.load();
this.registerEntries();
if (settings.size() > 0) {
this.configKeeper.fromJson(settings);
} else {
this.configKeeper.toJson(settings);
this.writer.save();
}
} }
@Override @Override
@ -28,7 +17,6 @@ public class BiomeConfig extends Config {
@Override @Override
public void saveChanges() { public void saveChanges() {
this.configKeeper.toJson(settings);
this.writer.saveConfig(); this.writer.saveConfig();
} }

View file

@ -14,12 +14,22 @@ import ru.betterend.config.ConfigKeeper.StringEntry;
public abstract class Config { public abstract class Config {
protected final ConfigKeeper configKeeper = new ConfigKeeper(); protected final ConfigKeeper configKeeper;
protected JsonObject settings; protected final ConfigWriter writer;
protected final String group;
public abstract void saveChanges(); public abstract void saveChanges();
protected abstract void registerEntries(); protected abstract void registerEntries();
public Config(String group) {
this.group = group;
this.writer = new ConfigWriter(group);
JsonObject settings = writer.load();
this.configKeeper = new ConfigKeeper(settings);
this.registerEntries();
this.writer.save();
}
@Nullable @Nullable
public <E extends Entry<?>> E getEntry(ConfigKey key) { public <E extends Entry<?>> E getEntry(ConfigKey key) {
return this.configKeeper.getEntry(key); return this.configKeeper.getEntry(key);
@ -34,12 +44,9 @@ public abstract class Config {
public String getString(ConfigKey key, String defaultValue) { public String getString(ConfigKey key, String defaultValue) {
String str = configKeeper.getValue(key); String str = configKeeper.getValue(key);
if (str == null) { if (str == null) {
StringEntry entry = this.configKeeper.registerEntry(key, new StringEntry(defaultValue)); StringEntry entry = configKeeper.registerEntry(key, new StringEntry(defaultValue));
if (settings != null) {
this.configKeeper.loadFromJson(settings, key, entry);
return entry.getValue(); return entry.getValue();
} }
}
return str != null ? str : defaultValue; return str != null ? str : defaultValue;
} }
@ -63,12 +70,9 @@ public abstract class Config {
public int getInt(ConfigKey key, int defaultValue) { public int getInt(ConfigKey key, int defaultValue) {
Integer val = configKeeper.getValue(key); Integer val = configKeeper.getValue(key);
if (val == null) { if (val == null) {
IntegerEntry entry = this.configKeeper.registerEntry(key, new IntegerEntry(defaultValue)); IntegerEntry entry = configKeeper.registerEntry(key, new IntegerEntry(defaultValue));
if (settings != null) {
this.configKeeper.loadFromJson(settings, key, entry);
return entry.getValue(); return entry.getValue();
} }
}
return val != null ? val : defaultValue; return val != null ? val : defaultValue;
} }
@ -104,12 +108,9 @@ public abstract class Config {
public float getFloat(ConfigKey key, float defaultValue) { public float getFloat(ConfigKey key, float defaultValue) {
Float val = configKeeper.getValue(key); Float val = configKeeper.getValue(key);
if (val == null) { if (val == null) {
FloatEntry entry = this.configKeeper.registerEntry(key, new FloatEntry(defaultValue)); FloatEntry entry = configKeeper.registerEntry(key, new FloatEntry(defaultValue));
if (settings != null) {
this.configKeeper.loadFromJson(settings, key, entry);
return entry.getValue(); return entry.getValue();
} }
}
return val != null ? val : defaultValue; return val != null ? val : defaultValue;
} }
@ -133,12 +134,9 @@ public abstract class Config {
public boolean getBoolean(ConfigKey key, boolean defaultValue) { public boolean getBoolean(ConfigKey key, boolean defaultValue) {
Boolean val = configKeeper.getValue(key); Boolean val = configKeeper.getValue(key);
if (val == null) { if (val == null) {
BooleanEntry entry = this.configKeeper.registerEntry(key, new BooleanEntry(defaultValue)); BooleanEntry entry = configKeeper.registerEntry(key, new BooleanEntry(defaultValue));
if (settings != null) {
this.configKeeper.loadFromJson(settings, key, entry);
return entry.getValue(); return entry.getValue();
} }
}
return val != null ? val : defaultValue; return val != null ? val : defaultValue;
} }

View file

@ -1,32 +1,38 @@
package ru.betterend.config; package ru.betterend.config;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper; import net.minecraft.util.JsonHelper;
import ru.betterend.BetterEnd;
public final class ConfigKeeper { public final class ConfigKeeper {
private Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap(); private Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
private final JsonObject configObject;
public ConfigKeeper(JsonObject config) {
this.configObject = config;
}
private <T, E extends Entry<T>> void storeValue(ConfigKey key, E entry, T value) {
if (configObject == null) return;
public JsonElement toJson(JsonObject jsonObject) {
this.configEntries.forEach((key, entry) -> {
Identifier categoryId = key.getCategory(); Identifier categoryId = key.getCategory();
Identifier paramId = key.getParameter(); Identifier paramId = key.getParameter();
String group = categoryId.getPath(); String group = categoryId.getPath();
JsonObject jsonGroup; JsonObject jsonGroup;
if (jsonObject.has(group)) { if (configObject.has(group)) {
jsonGroup = JsonHelper.getObject(jsonObject, group); jsonGroup = JsonHelper.getObject(configObject, group);
} else { } else {
jsonGroup = new JsonObject(); jsonGroup = new JsonObject();
jsonObject.add(group, jsonGroup); configObject.add(group, jsonGroup);
} }
String category = paramId.getNamespace(); String category = paramId.getNamespace();
JsonObject jsonCategory; JsonObject jsonCategory;
@ -38,33 +44,43 @@ public final class ConfigKeeper {
} }
String paramKey = paramId.getPath(); String paramKey = paramId.getPath();
paramKey += " [default: " + entry.getDefault() + "]"; paramKey += " [default: " + entry.getDefault() + "]";
jsonCategory.addProperty(paramKey, entry.asString()); if (value instanceof Boolean) {
}); jsonCategory.addProperty(paramKey, (Boolean) value);
} else if (value instanceof Number) {
return jsonObject; jsonCategory.addProperty(paramKey, (Number) value);
} else {
jsonCategory.addProperty(paramKey, entry.asString(value));
}
} }
public void fromJson(JsonObject jsonObject) { @SuppressWarnings("unchecked")
if (jsonObject.size() == 0) return; private <T, E extends Entry<T>> T getValue(ConfigKey key, E entry) {
this.configEntries.forEach((key, entry) -> { T defaultVal = entry.getDefault();
this.loadFromJson(jsonObject, key, entry); if (configObject == null) return defaultVal;
});
}
public <E extends Entry<?>> void loadFromJson(JsonObject jsonObject, ConfigKey key, E entry) {
Identifier categoryId = key.getCategory(); Identifier categoryId = key.getCategory();
Identifier paramId = key.getParameter(); Identifier paramId = key.getParameter();
String group = categoryId.getPath(); String group = categoryId.getPath();
if (!jsonObject.has(group)) return; if (!configObject.has(group)) return defaultVal;
JsonObject jsonGroup = JsonHelper.getObject(jsonObject, group); JsonObject jsonGroup = JsonHelper.getObject(configObject, group);
String category = paramId.getNamespace(); String category = paramId.getNamespace();
if (jsonGroup.has(category)) return; if (!jsonGroup.has(category)) return defaultVal;
JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category); JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category);
String paramKey = paramId.getPath(); String paramKey = paramId.getPath();
paramKey += " [default: " + entry.getDefault() + "]"; paramKey += " [default: " + entry.getDefault() + "]";
entry.fromString(JsonHelper.getString(jsonCategory, paramKey)); if (!jsonCategory.has(paramKey)) return defaultVal;
if (defaultVal instanceof Boolean) {
return (T) (Object) jsonCategory.get(paramKey).getAsBoolean();
} else if (defaultVal instanceof Integer) {
return (T) (Object) jsonCategory.get(paramKey).getAsInt();
} else if (defaultVal instanceof Float) {
return (T) (Object) jsonCategory.get(paramKey).getAsFloat();
}
return entry.fromString(JsonHelper.getString(jsonCategory, paramKey));
} }
@Nullable @Nullable
@ -82,7 +98,10 @@ public final class ConfigKeeper {
return entry.getValue(); return entry.getValue();
} }
public <T extends Entry<?>> T registerEntry(ConfigKey key, T entry) { public <T, E extends Entry<T>> 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());
this.configEntries.put(key, entry); this.configEntries.put(key, entry);
return entry; return entry;
} }
@ -94,15 +113,14 @@ public final class ConfigKeeper {
} }
@Override @Override
public String asString() { public String asString(Boolean value) {
return this.getValue() ? "true" : "false"; return value ? "true" : "false";
} }
@Override @Override
public void fromString(String value) { public Boolean fromString(String value) {
this.setValue(value.equals("true") ? true : false); return value.equals("true") ? true : false;
} }
} }
public static class FloatEntry extends Entry<Float> { public static class FloatEntry extends Entry<Float> {
@ -112,15 +130,14 @@ public final class ConfigKeeper {
} }
@Override @Override
public String asString() { public Float fromString(String value) {
return Float.toString(getValue()); return Float.valueOf(value);
} }
@Override @Override
public void fromString(String value) { public String asString(Float value) {
this.setValue(Float.valueOf(value)); return Float.toString(value);
} }
} }
public static class FloatRange extends RangeEntry<Float> { public static class FloatRange extends RangeEntry<Float> {
@ -130,15 +147,14 @@ public final class ConfigKeeper {
} }
@Override @Override
public void fromString(String value) { public Float fromString(String value) {
this.setValue(Float.valueOf(value)); return Float.valueOf(value);
} }
@Override @Override
public String asString() { public String asString(Float value) {
return Float.toString(getValue()); return Float.toString(value);
} }
} }
public static class IntegerEntry extends Entry<Integer> { public static class IntegerEntry extends Entry<Integer> {
@ -153,15 +169,14 @@ public final class ConfigKeeper {
} }
@Override @Override
public String asString() { public Integer fromString(String value) {
return Integer.toString(getValue()); return Integer.parseInt(value);
} }
@Override @Override
public void fromString(String value) { public String asString(Integer value) {
this.setValue(Integer.valueOf(value)); return Integer.toString(value);
} }
} }
public static class IntegerRange extends RangeEntry<Integer> { public static class IntegerRange extends RangeEntry<Integer> {
@ -171,15 +186,14 @@ public final class ConfigKeeper {
} }
@Override @Override
public String asString() { public Integer fromString(String value) {
return Integer.toString(getValue()); return Integer.parseInt(value);
} }
@Override @Override
public void fromString(String value) { public String asString(Integer value) {
this.setValue(Integer.valueOf(value)); return Integer.toString(value);
} }
} }
public static class StringEntry extends Entry<String> { public static class StringEntry extends Entry<String> {
@ -189,13 +203,13 @@ public final class ConfigKeeper {
} }
@Override @Override
public String asString() { public String fromString(String value) {
return this.getValue(); return value;
} }
@Override @Override
public void fromString(String value) { public String asString(String value) {
this.setValue(value); return value;
} }
} }
@ -206,31 +220,20 @@ public final class ConfigKeeper {
super(defaultValue); super(defaultValue);
} }
@SuppressWarnings("unchecked")
public boolean setValue(String name) {
try {
this.setValue((T) Enum.valueOf(this.defaultValue.getClass(), name));
return true;
} catch(IllegalArgumentException ex) {
BetterEnd.LOGGER.catching(ex);
}
return false;
}
@Override @Override
public T getDefault() { public T getDefault() {
return this.defaultValue; return this.defaultValue;
} }
@Override @Override
public String asString() { @SuppressWarnings("unchecked")
return getValue().name(); public T fromString(String value) {
return (T) Enum.valueOf(defaultValue.getClass(), value);
} }
@Override @Override
public void fromString(String value) { public String asString(T value) {
this.setValue(value); return value.name();
} }
} }
@ -246,7 +249,7 @@ public final class ConfigKeeper {
@Override @Override
public void setValue(T value) { public void setValue(T value) {
this.value = (value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value); super.setValue(value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
} }
public T minValue() { public T minValue() {
@ -261,22 +264,30 @@ public final class ConfigKeeper {
public static abstract class Entry<T> { public static abstract class Entry<T> {
protected final T defaultValue; protected final T defaultValue;
protected T value; protected Consumer<T> writer;
protected Supplier<T> reader;
public abstract void fromString(String value); public abstract T fromString(String value);
public abstract String asString(); public abstract String asString(T value);
public Entry (T defaultValue) { public Entry (T defaultValue) {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.value = defaultValue; }
protected void setWriter(Consumer<T> writer) {
this.writer = writer;
}
protected void setReader(Supplier<T> reader) {
this.reader = reader;
} }
public T getValue() { public T getValue() {
return this.value; return this.reader.get();
} }
public void setValue(T value) { public void setValue(T value) {
this.value = value; this.writer.accept(value);
} }
public T getDefault() { public T getDefault() {
@ -284,7 +295,7 @@ public final class ConfigKeeper {
} }
public void setDefault() { public void setDefault() {
this.value = defaultValue; this.setValue(defaultValue);
} }
} }
} }

View file

@ -7,19 +7,8 @@ import ru.betterend.config.ConfigKeeper.Entry;
public class ItemConfig extends Config { public class ItemConfig extends Config {
private final ConfigWriter writer;
private final String group = "settings";
protected ItemConfig() { protected ItemConfig() {
this.writer = new ConfigWriter(group); super("settings");
this.settings = this.writer.load();
this.registerEntries();
if (settings.size() > 0) {
this.configKeeper.fromJson(settings);
} else {
this.configKeeper.toJson(settings);
this.writer.save();
}
} }
@Override @Override
@ -27,7 +16,6 @@ public class ItemConfig extends Config {
@Override @Override
public void saveChanges() { public void saveChanges() {
this.configKeeper.toJson(settings);
this.writer.save(); this.writer.save();
} }

View file

@ -268,7 +268,7 @@ public class EndBiomes {
private static void registerBiomeDirect(EndBiome biome) { private static void registerBiomeDirect(EndBiome biome) {
fillSet(); fillSet();
int possibleID = incID++; int possibleID = incID++;
Configs.BIOME_CONFIG.getBoolean(biome, "enabled"); Configs.BIOME_CONFIG.getBoolean(biome, "enabled", true);
if (occupiedIDs.contains(possibleID)) { if (occupiedIDs.contains(possibleID)) {
String message = "ID for biome " + biome.getID() + " is already occupied, changing biome ID from " + possibleID + " to "; String message = "ID for biome " + biome.getID() + " is already occupied, changing biome ID from " + possibleID + " to ";
while (occupiedIDs.contains(possibleID)) { while (occupiedIDs.contains(possibleID)) {
@ -287,7 +287,8 @@ public class EndBiomes {
public static EndBiome getRenderBiome(Biome biome) { public static EndBiome getRenderBiome(Biome biome) {
EndBiome endBiome = CLIENT.get(biome); EndBiome endBiome = CLIENT.get(biome);
if (endBiome == null) { if (endBiome == null) {
Identifier id = MinecraftClient.getInstance().world.getRegistryManager().get(Registry.BIOME_KEY).getId(biome); MinecraftClient minecraft = MinecraftClient.getInstance();
Identifier id = minecraft.world.getRegistryManager().get(Registry.BIOME_KEY).getId(biome);
endBiome = id == null ? END : ID_MAP.getOrDefault(id, END); endBiome = id == null ? END : ID_MAP.getOrDefault(id, END);
CLIENT.put(biome, endBiome); CLIENT.put(biome, endBiome);
} }

View file

@ -13,45 +13,12 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.resource.Resource;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
public class JsonFactory { public class JsonFactory {
public final static Gson GSON = new GsonBuilder().setPrettyPrinting().create(); public final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
// Unused
@Deprecated
public static JsonObject getJsonObject(String path) throws IOException {
try (InputStream is = JsonFactory.class.getResourceAsStream(path)) {
Reader reader = new InputStreamReader(is);
JsonObject jsonObject = loadJson(reader).getAsJsonObject();
if (jsonObject == null) {
return new JsonObject();
}
return jsonObject;
}
}
// Unused
@Deprecated
public static JsonObject getJsonObject(Resource jsonSource) {
if (jsonSource != null) {
try (InputStream is = jsonSource.getInputStream()) {
Reader reader = new InputStreamReader(is);
JsonElement json = loadJson(reader);
if (json != null && json.isJsonObject()) {
JsonObject jsonObject = json.getAsJsonObject();
return jsonObject != null ? jsonObject : new JsonObject();
}
}
catch (IOException ex) {
BetterEnd.LOGGER.catching(ex);
}
}
return new JsonObject();
}
public static JsonObject getJsonObject(InputStream stream) { public static JsonObject getJsonObject(InputStream stream) {
try { try {
Reader reader = new InputStreamReader(stream); Reader reader = new InputStreamReader(stream);
@ -60,8 +27,7 @@ public class JsonFactory {
JsonObject jsonObject = json.getAsJsonObject(); JsonObject jsonObject = json.getAsJsonObject();
return jsonObject != null ? jsonObject : new JsonObject(); return jsonObject != null ? jsonObject : new JsonObject();
} }
} } catch (Exception ex) {
catch (Exception ex) {
BetterEnd.LOGGER.catching(ex); BetterEnd.LOGGER.catching(ex);
} }
return new JsonObject(); return new JsonObject();
@ -75,7 +41,6 @@ public class JsonFactory {
return jsonObject != null ? jsonObject : new JsonObject(); return jsonObject != null ? jsonObject : new JsonObject();
} }
} }
return new JsonObject(); return new JsonObject();
} }