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

@ -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());
});
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;
return jsonObject;
}
public void fromJson(JsonObject jsonObject) {
if (jsonObject.size() == 0) return;
this.configEntries.forEach((key, entry) -> {
this.loadFromJson(jsonObject, key, entry);
});
}
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);
}
}
}