Config refactor
This commit is contained in:
parent
58a389eaf2
commit
fd06db2822
6 changed files with 134 additions and 183 deletions
|
@ -8,19 +8,8 @@ import ru.betterend.world.biome.EndBiome;
|
|||
|
||||
public class BiomeConfig extends Config {
|
||||
|
||||
private final ConfigWriter writer;
|
||||
private final String group = "biomes";
|
||||
|
||||
protected BiomeConfig() {
|
||||
this.writer = new ConfigWriter(group);
|
||||
this.settings = writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
super("biomes");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -28,7 +17,6 @@ public class BiomeConfig extends Config {
|
|||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.saveConfig();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,12 +14,22 @@ import ru.betterend.config.ConfigKeeper.StringEntry;
|
|||
|
||||
public abstract class Config {
|
||||
|
||||
protected final ConfigKeeper configKeeper = new ConfigKeeper();
|
||||
protected JsonObject settings;
|
||||
protected final ConfigKeeper configKeeper;
|
||||
protected final ConfigWriter writer;
|
||||
protected final String group;
|
||||
|
||||
public abstract void saveChanges();
|
||||
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
|
||||
public <E extends Entry<?>> E getEntry(ConfigKey key) {
|
||||
return this.configKeeper.getEntry(key);
|
||||
|
@ -34,11 +44,8 @@ public abstract class Config {
|
|||
public String getString(ConfigKey key, String defaultValue) {
|
||||
String str = configKeeper.getValue(key);
|
||||
if (str == null) {
|
||||
StringEntry entry = this.configKeeper.registerEntry(key, new StringEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
StringEntry entry = configKeeper.registerEntry(key, new StringEntry(defaultValue));
|
||||
return entry.getValue();
|
||||
}
|
||||
return str != null ? str : defaultValue;
|
||||
}
|
||||
|
@ -63,11 +70,8 @@ public abstract class Config {
|
|||
public int getInt(ConfigKey key, int defaultValue) {
|
||||
Integer val = configKeeper.getValue(key);
|
||||
if (val == null) {
|
||||
IntegerEntry entry = this.configKeeper.registerEntry(key, new IntegerEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
IntegerEntry entry = configKeeper.registerEntry(key, new IntegerEntry(defaultValue));
|
||||
return entry.getValue();
|
||||
}
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
@ -104,11 +108,8 @@ public abstract class Config {
|
|||
public float getFloat(ConfigKey key, float defaultValue) {
|
||||
Float val = configKeeper.getValue(key);
|
||||
if (val == null) {
|
||||
FloatEntry entry = this.configKeeper.registerEntry(key, new FloatEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
FloatEntry entry = configKeeper.registerEntry(key, new FloatEntry(defaultValue));
|
||||
return entry.getValue();
|
||||
}
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
@ -133,11 +134,8 @@ public abstract class Config {
|
|||
public boolean getBoolean(ConfigKey key, boolean defaultValue) {
|
||||
Boolean val = configKeeper.getValue(key);
|
||||
if (val == null) {
|
||||
BooleanEntry entry = this.configKeeper.registerEntry(key, new BooleanEntry(defaultValue));
|
||||
if (settings != null) {
|
||||
this.configKeeper.loadFromJson(settings, key, entry);
|
||||
return entry.getValue();
|
||||
}
|
||||
BooleanEntry entry = configKeeper.registerEntry(key, new BooleanEntry(defaultValue));
|
||||
return entry.getValue();
|
||||
}
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
|
|
@ -1,70 +1,86 @@
|
|||
package ru.betterend.config;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import ru.betterend.BetterEnd;
|
||||
|
||||
public final class ConfigKeeper {
|
||||
|
||||
private Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
|
||||
private final JsonObject configObject;
|
||||
|
||||
public JsonElement toJson(JsonObject jsonObject) {
|
||||
this.configEntries.forEach((key, entry) -> {
|
||||
Identifier categoryId = key.getCategory();
|
||||
Identifier paramId = key.getParameter();
|
||||
String group = categoryId.getPath();
|
||||
JsonObject jsonGroup;
|
||||
if (jsonObject.has(group)) {
|
||||
jsonGroup = JsonHelper.getObject(jsonObject, group);
|
||||
} else {
|
||||
jsonGroup = new JsonObject();
|
||||
jsonObject.add(group, jsonGroup);
|
||||
}
|
||||
String category = paramId.getNamespace();
|
||||
JsonObject jsonCategory;
|
||||
if (jsonGroup.has(category)) {
|
||||
jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||
} else {
|
||||
jsonCategory = new JsonObject();
|
||||
jsonGroup.add(category, jsonCategory);
|
||||
}
|
||||
String paramKey = paramId.getPath();
|
||||
paramKey += " [default: " + entry.getDefault() + "]";
|
||||
jsonCategory.addProperty(paramKey, entry.asString());
|
||||
});
|
||||
|
||||
return jsonObject;
|
||||
public ConfigKeeper(JsonObject config) {
|
||||
this.configObject = config;
|
||||
}
|
||||
|
||||
public void fromJson(JsonObject jsonObject) {
|
||||
if (jsonObject.size() == 0) return;
|
||||
this.configEntries.forEach((key, entry) -> {
|
||||
this.loadFromJson(jsonObject, key, entry);
|
||||
});
|
||||
}
|
||||
private <T, E extends Entry<T>> void storeValue(ConfigKey key, E entry, T value) {
|
||||
if (configObject == null) return;
|
||||
|
||||
public <E extends Entry<?>> void loadFromJson(JsonObject jsonObject, ConfigKey key, E entry) {
|
||||
Identifier categoryId = key.getCategory();
|
||||
Identifier paramId = key.getParameter();
|
||||
String group = categoryId.getPath();
|
||||
if (!jsonObject.has(group)) return;
|
||||
|
||||
JsonObject jsonGroup = JsonHelper.getObject(jsonObject, group);
|
||||
JsonObject jsonGroup;
|
||||
if (configObject.has(group)) {
|
||||
jsonGroup = JsonHelper.getObject(configObject, group);
|
||||
} else {
|
||||
jsonGroup = new JsonObject();
|
||||
configObject.add(group, jsonGroup);
|
||||
}
|
||||
String category = paramId.getNamespace();
|
||||
if (jsonGroup.has(category)) return;
|
||||
JsonObject jsonCategory;
|
||||
if (jsonGroup.has(category)) {
|
||||
jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||
} else {
|
||||
jsonCategory = new JsonObject();
|
||||
jsonGroup.add(category, jsonCategory);
|
||||
}
|
||||
String paramKey = paramId.getPath();
|
||||
paramKey += " [default: " + entry.getDefault() + "]";
|
||||
if (value instanceof Boolean) {
|
||||
jsonCategory.addProperty(paramKey, (Boolean) value);
|
||||
} else if (value instanceof Number) {
|
||||
jsonCategory.addProperty(paramKey, (Number) value);
|
||||
} else {
|
||||
jsonCategory.addProperty(paramKey, entry.asString(value));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T, E extends Entry<T>> T getValue(ConfigKey key, E entry) {
|
||||
T defaultVal = entry.getDefault();
|
||||
if (configObject == null) return defaultVal;
|
||||
|
||||
Identifier categoryId = key.getCategory();
|
||||
Identifier paramId = key.getParameter();
|
||||
String group = categoryId.getPath();
|
||||
if (!configObject.has(group)) return defaultVal;
|
||||
|
||||
JsonObject jsonGroup = JsonHelper.getObject(configObject, group);
|
||||
String category = paramId.getNamespace();
|
||||
if (!jsonGroup.has(category)) return defaultVal;
|
||||
|
||||
JsonObject jsonCategory = JsonHelper.getObject(jsonGroup, category);
|
||||
String paramKey = paramId.getPath();
|
||||
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
|
||||
|
@ -82,7 +98,10 @@ public final class ConfigKeeper {
|
|||
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);
|
||||
return entry;
|
||||
}
|
||||
|
@ -94,15 +113,14 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return this.getValue() ? "true" : "false";
|
||||
public String asString(Boolean value) {
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value.equals("true") ? true : false);
|
||||
public Boolean fromString(String value) {
|
||||
return value.equals("true") ? true : false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FloatEntry extends Entry<Float> {
|
||||
|
@ -112,15 +130,14 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Float.toString(getValue());
|
||||
public Float fromString(String value) {
|
||||
return Float.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Float.valueOf(value));
|
||||
public String asString(Float value) {
|
||||
return Float.toString(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FloatRange extends RangeEntry<Float> {
|
||||
|
@ -130,15 +147,14 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Float.valueOf(value));
|
||||
public Float fromString(String value) {
|
||||
return Float.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Float.toString(getValue());
|
||||
public String asString(Float value) {
|
||||
return Float.toString(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntegerEntry extends Entry<Integer> {
|
||||
|
@ -153,15 +169,14 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
public Integer fromString(String value) {
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
public String asString(Integer value) {
|
||||
return Integer.toString(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IntegerRange extends RangeEntry<Integer> {
|
||||
|
@ -171,15 +186,14 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return Integer.toString(getValue());
|
||||
public Integer fromString(String value) {
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(Integer.valueOf(value));
|
||||
public String asString(Integer value) {
|
||||
return Integer.toString(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class StringEntry extends Entry<String> {
|
||||
|
@ -189,13 +203,13 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return this.getValue();
|
||||
public String fromString(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value);
|
||||
public String asString(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -206,31 +220,20 @@ public final class ConfigKeeper {
|
|||
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
|
||||
public T getDefault() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return getValue().name();
|
||||
@SuppressWarnings("unchecked")
|
||||
public T fromString(String value) {
|
||||
return (T) Enum.valueOf(defaultValue.getClass(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromString(String value) {
|
||||
this.setValue(value);
|
||||
public String asString(T value) {
|
||||
return value.name();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,7 +249,7 @@ public final class ConfigKeeper {
|
|||
|
||||
@Override
|
||||
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() {
|
||||
|
@ -261,22 +264,30 @@ public final class ConfigKeeper {
|
|||
public static abstract class Entry<T> {
|
||||
|
||||
protected final T defaultValue;
|
||||
protected T value;
|
||||
protected Consumer<T> writer;
|
||||
protected Supplier<T> reader;
|
||||
|
||||
public abstract void fromString(String value);
|
||||
public abstract String asString();
|
||||
public abstract T fromString(String value);
|
||||
public abstract String asString(T value);
|
||||
|
||||
public Entry (T 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() {
|
||||
return this.value;
|
||||
return this.reader.get();
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
this.writer.accept(value);
|
||||
}
|
||||
|
||||
public T getDefault() {
|
||||
|
@ -284,7 +295,7 @@ public final class ConfigKeeper {
|
|||
}
|
||||
|
||||
public void setDefault() {
|
||||
this.value = defaultValue;
|
||||
this.setValue(defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,19 +7,8 @@ import ru.betterend.config.ConfigKeeper.Entry;
|
|||
|
||||
public class ItemConfig extends Config {
|
||||
|
||||
private final ConfigWriter writer;
|
||||
private final String group = "settings";
|
||||
|
||||
protected ItemConfig() {
|
||||
this.writer = new ConfigWriter(group);
|
||||
this.settings = this.writer.load();
|
||||
this.registerEntries();
|
||||
if (settings.size() > 0) {
|
||||
this.configKeeper.fromJson(settings);
|
||||
} else {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
super("settings");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,7 +16,6 @@ public class ItemConfig extends Config {
|
|||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.configKeeper.toJson(settings);
|
||||
this.writer.save();
|
||||
}
|
||||
|
||||
|
|
|
@ -268,7 +268,7 @@ public class EndBiomes {
|
|||
private static void registerBiomeDirect(EndBiome biome) {
|
||||
fillSet();
|
||||
int possibleID = incID++;
|
||||
Configs.BIOME_CONFIG.getBoolean(biome, "enabled");
|
||||
Configs.BIOME_CONFIG.getBoolean(biome, "enabled", true);
|
||||
if (occupiedIDs.contains(possibleID)) {
|
||||
String message = "ID for biome " + biome.getID() + " is already occupied, changing biome ID from " + possibleID + " to ";
|
||||
while (occupiedIDs.contains(possibleID)) {
|
||||
|
@ -287,7 +287,8 @@ public class EndBiomes {
|
|||
public static EndBiome getRenderBiome(Biome biome) {
|
||||
EndBiome endBiome = CLIENT.get(biome);
|
||||
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);
|
||||
CLIENT.put(biome, endBiome);
|
||||
}
|
||||
|
|
|
@ -13,45 +13,12 @@ import com.google.gson.GsonBuilder;
|
|||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.resource.Resource;
|
||||
import ru.betterend.BetterEnd;
|
||||
|
||||
public class JsonFactory {
|
||||
|
||||
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) {
|
||||
try {
|
||||
Reader reader = new InputStreamReader(stream);
|
||||
|
@ -60,8 +27,7 @@ public class JsonFactory {
|
|||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
return jsonObject != null ? jsonObject : new JsonObject();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
} catch (Exception ex) {
|
||||
BetterEnd.LOGGER.catching(ex);
|
||||
}
|
||||
return new JsonObject();
|
||||
|
@ -75,7 +41,6 @@ public class JsonFactory {
|
|||
return jsonObject != null ? jsonObject : new JsonObject();
|
||||
}
|
||||
}
|
||||
|
||||
return new JsonObject();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue