Some more work, still doesn't work
This commit is contained in:
parent
68a3bf2d54
commit
a3721174d9
|
@ -23,7 +23,7 @@ import java.util.Optional;
|
||||||
public class GuiFactory {
|
public class GuiFactory {
|
||||||
public void buildCategory(ConfigBranch source, String screenId, JfConfigCategory config, ConfigEntryBuilder entryBuilder, String namePrefix) {
|
public void buildCategory(ConfigBranch source, String screenId, JfConfigCategory config, ConfigEntryBuilder entryBuilder, String namePrefix) {
|
||||||
String b = "respackopts.field." + screenId;
|
String b = "respackopts.field." + screenId;
|
||||||
for (Map.Entry<String, Entry<?>> in : source.getEntries()) {
|
for (Map.Entry<String, Entry<?>> in : source.getValue().entrySet()) {
|
||||||
Entry<?> entry = in.getValue();
|
Entry<?> entry = in.getValue();
|
||||||
String n = ("".equals(namePrefix) ? "" : namePrefix + ".") + in.getKey();
|
String n = ("".equals(namePrefix) ? "" : namePrefix + ".") + in.getKey();
|
||||||
if (entry instanceof ConfigBranch e) {
|
if (entry instanceof ConfigBranch e) {
|
||||||
|
@ -33,50 +33,36 @@ public class GuiFactory {
|
||||||
config.addEntry(sc.build());
|
config.addEntry(sc.build());
|
||||||
}
|
}
|
||||||
else if (entry instanceof ConfigBooleanEntry e) {
|
else if (entry instanceof ConfigBooleanEntry e) {
|
||||||
config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), e.value)
|
config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), e.getValue())
|
||||||
.setDefaultValue(e.defaultValue)
|
.setDefaultValue(e.getDefault())
|
||||||
.setSaveConsumer(v -> {
|
.setSaveConsumer(e::setValue)
|
||||||
/*ConfigBooleanEntry cb = (ConfigBooleanEntry)source.get(e.name);
|
|
||||||
cb.value = v;
|
|
||||||
Respackopts.LOGGER.info(cb == e);
|
|
||||||
Respackopts.LOGGER.info(cb == entry);*/
|
|
||||||
e.value = v;
|
|
||||||
})
|
|
||||||
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
else if (entry instanceof ConfigEnumEntry e) {
|
else if (entry instanceof ConfigEnumEntry e) {
|
||||||
String selected = "";
|
|
||||||
String def = "";
|
|
||||||
for (Map.Entry<String, Integer> en : e.values.entrySet()) {
|
|
||||||
if (en.getValue().equals(e.value))
|
|
||||||
selected = en.getKey();
|
|
||||||
if (en.getValue().equals(e.defaultValue))
|
|
||||||
def = en.getKey();
|
|
||||||
}
|
|
||||||
config.addEntry(entryBuilder.startDropdownMenu(getText(n, b),
|
config.addEntry(entryBuilder.startDropdownMenu(getText(n, b),
|
||||||
DropdownMenuBuilder.TopCellElementBuilder.of(selected, LiteralText::new),
|
DropdownMenuBuilder.TopCellElementBuilder.of(e.getValueName(), LiteralText::new),
|
||||||
new DropdownBoxEntry.DefaultSelectionCellCreator())
|
new DropdownBoxEntry.DefaultSelectionCellCreator())
|
||||||
.setSuggestionMode(false)
|
.setSuggestionMode(false)
|
||||||
.setDefaultValue(def)
|
.setDefaultValue(e.getDefaultName())
|
||||||
.setSelections(() -> e.values.keySet().iterator())
|
.setSelections(() -> e.values.keySet().iterator())
|
||||||
.setSaveConsumer(v -> e.value = e.values.get(v))
|
.setSaveConsumer(v -> e.setValue(e.values.get(v)))
|
||||||
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
else if (entry instanceof ConfigNumericEntry e) {
|
else if (entry instanceof ConfigNumericEntry e) {
|
||||||
if (e.min != null && e.max != null) {
|
if (e.min != null && e.max != null) {
|
||||||
config.addEntry(entryBuilder.startIntSlider(getText(n, b),
|
config.addEntry(entryBuilder.startIntSlider(getText(n, b),
|
||||||
e.value.intValue(), e.min.intValue(), e.max.intValue())
|
e.getValue().intValue(), e.min.intValue(), e.max.intValue())
|
||||||
.setDefaultValue(e.defaultValue.intValue())
|
.setDefaultValue(e.getDefault().intValue())
|
||||||
.setSaveConsumer(v -> e.value = v.doubleValue())
|
.setSaveConsumer(v -> e.setValue(v.doubleValue()))
|
||||||
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
config.addEntry(entryBuilder.startDoubleField(getText(n, b), e.value)
|
config.addEntry(entryBuilder.startDoubleField(getText(n, b), e.getValue())
|
||||||
.setDefaultValue(e.defaultValue)
|
.setDefaultValue(e.getDefault())
|
||||||
.setSaveConsumer(v -> e.value = v)
|
.setSaveConsumer(e::setValue)
|
||||||
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
.setTooltipSupplier(() -> getTooltip(n, screenId))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,7 @@ package io.gitlab.jfronny.respackopts;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import io.gitlab.jfronny.respackopts.data.ConfigBooleanEntry;
|
import io.gitlab.jfronny.respackopts.data.*;
|
||||||
import io.gitlab.jfronny.respackopts.data.ConfigBranch;
|
|
||||||
import io.gitlab.jfronny.respackopts.data.ConfigEnumEntry;
|
|
||||||
import io.gitlab.jfronny.respackopts.data.ConfigNumericEntry;
|
|
||||||
import io.gitlab.jfronny.respackopts.data.in.Respackmeta;
|
import io.gitlab.jfronny.respackopts.data.in.Respackmeta;
|
||||||
import io.gitlab.jfronny.respackopts.filters.conditions.SyntaxError;
|
import io.gitlab.jfronny.respackopts.filters.conditions.SyntaxError;
|
||||||
import io.gitlab.jfronny.respackopts.gson.BooleanEntrySerializer;
|
import io.gitlab.jfronny.respackopts.gson.BooleanEntrySerializer;
|
||||||
|
@ -67,6 +64,7 @@ public class Respackopts implements ClientModInitializer {
|
||||||
public static void save() {
|
public static void save() {
|
||||||
for (String s : resPackMetas.keySet()) {
|
for (String s : resPackMetas.keySet()) {
|
||||||
s = resPackMetas.get(s).id;
|
s = resPackMetas.get(s).id;
|
||||||
|
LOGGER.info(s + ": " + CONFIG_BRANCH.get(s));
|
||||||
try (Writer writer = Files.newBufferedWriter(CONF_DIR.resolve(s + ".json"))) {
|
try (Writer writer = Files.newBufferedWriter(CONF_DIR.resolve(s + ".json"))) {
|
||||||
GSON.toJson(CONFIG_BRANCH.get(s), writer);
|
GSON.toJson(CONFIG_BRANCH.get(s), writer);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
@ -85,10 +83,10 @@ public class Respackopts implements ClientModInitializer {
|
||||||
try (Reader reader = Files.newBufferedReader(q)) {
|
try (Reader reader = Files.newBufferedReader(q)) {
|
||||||
ConfigBranch b = GSON.fromJson(reader, ConfigBranch.class);
|
ConfigBranch b = GSON.fromJson(reader, ConfigBranch.class);
|
||||||
if (CONFIG_BRANCH.containsKey(id))
|
if (CONFIG_BRANCH.containsKey(id))
|
||||||
CONFIG_BRANCH.get(id).loadValues(b, false);
|
CONFIG_BRANCH.get(id).sync(b, SyncMode.CONF_LOAD);
|
||||||
else
|
else
|
||||||
CONFIG_BRANCH.put(id, b);
|
CONFIG_BRANCH.put(id, b);
|
||||||
} catch (IOException | SyntaxError e) {
|
} catch (IOException e) {
|
||||||
LOGGER.error("Failed to load " + id, e);
|
LOGGER.error("Failed to load " + id, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,11 @@ package io.gitlab.jfronny.respackopts.data;
|
||||||
|
|
||||||
public class ConfigBooleanEntry extends Entry<Boolean> {
|
public class ConfigBooleanEntry extends Entry<Boolean> {
|
||||||
public ConfigBooleanEntry(boolean v) {
|
public ConfigBooleanEntry(boolean v) {
|
||||||
value = v;
|
setValue(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean typeMatches(Entry<?> val) {
|
||||||
|
return val instanceof ConfigBooleanEntry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,31 @@
|
||||||
package io.gitlab.jfronny.respackopts.data;
|
package io.gitlab.jfronny.respackopts.data;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
||||||
import io.gitlab.jfronny.respackopts.filters.conditions.SyntaxError;
|
import io.gitlab.jfronny.respackopts.filters.conditions.SyntaxError;
|
||||||
|
|
||||||
import java.io.InvalidClassException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class ConfigBranch extends Entry<Object> {
|
public class ConfigBranch extends Entry<Map<String, Entry<?>>> {
|
||||||
private final Map<String, Entry<?>> entries = new HashMap<>();
|
public ConfigBranch() {
|
||||||
|
setValue(new HashMap<>());
|
||||||
|
}
|
||||||
|
|
||||||
public boolean getBoolean(String name) throws SyntaxError {
|
public boolean getBoolean(String name) throws SyntaxError {
|
||||||
String[] sp = name.split("\\.");
|
String[] sp = name.split("\\.");
|
||||||
if (!entries.containsKey(sp[0]))
|
if (!super.getValue().containsKey(sp[0]))
|
||||||
throw new SyntaxError("Invalid path to key");
|
throw new SyntaxError("Invalid path to key");
|
||||||
Entry<?> e = entries.get(sp[0]);
|
Entry<?> e = super.getValue().get(sp[0]);
|
||||||
if (sp.length == 1) {
|
if (sp.length == 1) {
|
||||||
if (e instanceof ConfigBooleanEntry b)
|
if (e instanceof ConfigBooleanEntry b)
|
||||||
return b.value;
|
return b.getValue();
|
||||||
throw new SyntaxError("Not a boolean");
|
throw new SyntaxError("Not a boolean");
|
||||||
}
|
}
|
||||||
if (sp.length == 2 && e instanceof ConfigEnumEntry en) {
|
if (sp.length == 2 && e instanceof ConfigEnumEntry en) {
|
||||||
for (Map.Entry<String, Integer> entry : en.values.entrySet()) {
|
for (Map.Entry<String, Integer> entry : en.values.entrySet()) {
|
||||||
if (entry.getKey().equals(sp[1]))
|
if (entry.getKey().equals(sp[1]))
|
||||||
return entry.getValue().equals(en.value);
|
return entry.getValue().equals(en.getValue());
|
||||||
}
|
}
|
||||||
throw new SyntaxError("Could not find enum entry");
|
throw new SyntaxError("Could not find enum entry");
|
||||||
}
|
}
|
||||||
|
@ -33,73 +34,62 @@ public class ConfigBranch extends Entry<Object> {
|
||||||
throw new SyntaxError("Invalid path to key");
|
throw new SyntaxError("Invalid path to key");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadValues(ConfigBranch newBranch, boolean additionOnly) throws SyntaxError {
|
@Override
|
||||||
for (Map.Entry<String, Entry<?>> e : newBranch.entries.entrySet()) {
|
public void sync(Entry<Map<String, Entry<?>>> source, SyncMode mode) {
|
||||||
if (!entries.containsKey(e.getKey()))
|
for (Map.Entry<String, Entry<?>> e : source.getValue().entrySet()) {
|
||||||
add(e.getKey(), e.getValue());
|
if (!has(e.getKey())) {
|
||||||
else if (!additionOnly) {
|
if (mode.isAdd)
|
||||||
if (e.getValue() instanceof ConfigEnumEntry ne) {
|
add(e.getKey(), e.getValue());
|
||||||
ConfigEnumEntry ol = (ConfigEnumEntry)entries.get(e.getKey());
|
} else {
|
||||||
if (ne.value != null)
|
Entry<?> current = get(e.getKey());
|
||||||
ol.value = ne.value;
|
if (e.getValue().typeMatches(current)) {
|
||||||
if (ne.values != null && !ne.values.isEmpty())
|
syncSub(current, (Entry)e.getValue(), mode);
|
||||||
ol.values = ne.values;
|
|
||||||
}
|
|
||||||
else if (e.getValue() instanceof ConfigBooleanEntry ne) {
|
|
||||||
ConfigBooleanEntry ol = (ConfigBooleanEntry)entries.get(e.getKey());
|
|
||||||
if (ne.value != null)
|
|
||||||
ol.value = ne.value;
|
|
||||||
}
|
|
||||||
else if (e.getValue() instanceof ConfigNumericEntry ne) {
|
|
||||||
ConfigNumericEntry ol = (ConfigNumericEntry)entries.get(e.getKey());
|
|
||||||
if (ne.value != null)
|
|
||||||
ol.value = ne.value;
|
|
||||||
if (ne.min != null)
|
|
||||||
ol.min = ne.min;
|
|
||||||
if (ne.max != null)
|
|
||||||
ol.max = ne.max;
|
|
||||||
}
|
|
||||||
else if (e.getValue() instanceof ConfigBranch ne) {
|
|
||||||
ConfigBranch ol = (ConfigBranch)entries.get(e.getKey());
|
|
||||||
ol.loadValues(ne, false);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new SyntaxError("Invalid type");
|
Respackopts.LOGGER.warn("Type mismatch in config, ignoring");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Entry<T2>, T2> T add(String name, T val) {
|
@Override
|
||||||
val.name = name;
|
public boolean typeMatches(Entry<?> val) {
|
||||||
val.defaultValue = val.value;
|
return val instanceof ConfigBranch;
|
||||||
entries.put(name, val);
|
}
|
||||||
return val;
|
|
||||||
|
private <T> void syncSub(Entry<T> current, Entry<T> next, SyncMode mode) {
|
||||||
|
current.sync(next, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void add(String name, Entry<T> val) {
|
||||||
|
val.setDefault(val.getValue());
|
||||||
|
super.getValue().put(name, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entry<?> get(String key) {
|
public Entry<?> get(String key) {
|
||||||
return entries.get(key);
|
return super.getValue().get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean has(String key) {
|
public boolean has(String key) {
|
||||||
return entries.containsKey(key);
|
return super.getValue().containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Map.Entry<String, Entry<?>>> getEntries() {
|
@Override
|
||||||
return entries.entrySet();
|
public Map<String, Entry<?>> getValue() {
|
||||||
|
return ImmutableMap.copyOf(super.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildShader(StringBuilder sb, String valuePrefix) throws SyntaxError {
|
public void buildShader(StringBuilder sb, String valuePrefix) throws SyntaxError {
|
||||||
for (Map.Entry<String, Entry<?>> e : entries.entrySet()) {
|
for (Map.Entry<String, Entry<?>> e : super.getValue().entrySet()) {
|
||||||
if (e.getValue() instanceof ConfigNumericEntry n) {
|
if (e.getValue() instanceof ConfigNumericEntry n) {
|
||||||
sb.append("\n#define ");
|
sb.append("\n#define ");
|
||||||
sb.append(valuePrefix);
|
sb.append(valuePrefix);
|
||||||
sb.append(e.getKey());
|
sb.append(e.getKey());
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(n.value.toString());
|
sb.append(n.getValue().toString());
|
||||||
}
|
}
|
||||||
else if (e.getValue() instanceof ConfigBooleanEntry n) {
|
else if (e.getValue() instanceof ConfigBooleanEntry n) {
|
||||||
if (n.value) {
|
if (n.getValue()) {
|
||||||
sb.append("\n#define ");
|
sb.append("\n#define ");
|
||||||
sb.append(valuePrefix);
|
sb.append(valuePrefix);
|
||||||
sb.append(e.getKey());
|
sb.append(e.getKey());
|
||||||
|
@ -110,7 +100,7 @@ public class ConfigBranch extends Entry<Object> {
|
||||||
sb.append(valuePrefix);
|
sb.append(valuePrefix);
|
||||||
sb.append(e.getKey());
|
sb.append(e.getKey());
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(n.value.toString());
|
sb.append(n.getValue().toString());
|
||||||
for (Map.Entry<String, Integer> e2 : n.values.entrySet()) {
|
for (Map.Entry<String, Integer> e2 : n.values.entrySet()) {
|
||||||
sb.append("\n#define ");
|
sb.append("\n#define ");
|
||||||
sb.append(valuePrefix);
|
sb.append(valuePrefix);
|
||||||
|
@ -129,4 +119,14 @@ public class ConfigBranch extends Entry<Object> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void appendString(StringBuilder sb) {
|
||||||
|
for (Map.Entry<String, Entry<?>> e : getValue().entrySet()) {
|
||||||
|
sb.append("\n");
|
||||||
|
sb.append(e.getKey());
|
||||||
|
sb.append(": ");
|
||||||
|
sb.append(e.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,57 @@ import java.util.Map;
|
||||||
|
|
||||||
public class ConfigEnumEntry extends Entry<Integer> {
|
public class ConfigEnumEntry extends Entry<Integer> {
|
||||||
public Map<String, Integer> values = new HashMap<>();
|
public Map<String, Integer> values = new HashMap<>();
|
||||||
|
private String fieldName;
|
||||||
|
|
||||||
public ConfigEnumEntry() {
|
public ConfigEnumEntry() {
|
||||||
value = 0;
|
setValue(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getValue() {
|
||||||
|
Integer v = super.getValue();
|
||||||
|
if (v == null)
|
||||||
|
throw new NullPointerException();
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sync(Entry<Integer> source, SyncMode mode) {
|
||||||
|
super.sync(source, mode);
|
||||||
|
ConfigEnumEntry n = (ConfigEnumEntry) source;
|
||||||
|
if (mode.modifyDefault) {
|
||||||
|
if (n.values != null && !n.values.isEmpty())
|
||||||
|
values = n.values;
|
||||||
|
}
|
||||||
|
if (mode.modifyValue) {
|
||||||
|
if (n.fieldName != null)
|
||||||
|
setValue(values.get(n.fieldName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean typeMatches(Entry<?> val) {
|
||||||
|
return val instanceof ConfigEnumEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFieldName(String s) {
|
||||||
|
if (s != null)
|
||||||
|
fieldName = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValueName() {
|
||||||
|
for (Map.Entry<String, Integer> en : values.entrySet()) {
|
||||||
|
if (en.getValue().equals(getValue()))
|
||||||
|
return en.getKey();
|
||||||
|
}
|
||||||
|
return getDefaultName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDefaultName() {
|
||||||
|
for (Map.Entry<String, Integer> en : values.entrySet()) {
|
||||||
|
if (en.getValue().equals(getDefault()))
|
||||||
|
return en.getKey();
|
||||||
|
}
|
||||||
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,35 @@ public class ConfigNumericEntry extends Entry<Double> {
|
||||||
public Double max;
|
public Double max;
|
||||||
|
|
||||||
public ConfigNumericEntry() {
|
public ConfigNumericEntry() {
|
||||||
value = 0d;
|
setValue(0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sync(Entry<Double> source, SyncMode mode) {
|
||||||
|
super.sync(source, mode);
|
||||||
|
ConfigNumericEntry n = (ConfigNumericEntry) source;
|
||||||
|
if (mode.modifyDefault) {
|
||||||
|
if (n.min != null)
|
||||||
|
min = n.min;
|
||||||
|
if (n.max != null)
|
||||||
|
max = n.max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean typeMatches(Entry<?> val) {
|
||||||
|
return val instanceof ConfigNumericEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void appendString(StringBuilder sb) {
|
||||||
|
sb.append(getValue())
|
||||||
|
.append(" (")
|
||||||
|
.append(getDefault())
|
||||||
|
.append(", ")
|
||||||
|
.append(min)
|
||||||
|
.append("-")
|
||||||
|
.append(max)
|
||||||
|
.append(")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,44 @@
|
||||||
package io.gitlab.jfronny.respackopts.data;
|
package io.gitlab.jfronny.respackopts.data;
|
||||||
|
|
||||||
public abstract class Entry<T> {
|
public abstract class Entry<T> {
|
||||||
public String name;
|
private T defaultValue;
|
||||||
public T defaultValue;
|
private T value;
|
||||||
public T value;
|
|
||||||
|
public T getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(T value) {
|
||||||
|
if (value != null)
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getDefault() {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefault(T value) {
|
||||||
|
if (value != null)
|
||||||
|
defaultValue = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sync(Entry<T> source, SyncMode mode) {
|
||||||
|
if (mode.modifyDefault)
|
||||||
|
setDefault(source.getDefault());
|
||||||
|
if (mode.modifyValue)
|
||||||
|
setValue(source.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean typeMatches(Entry<?> val);
|
||||||
|
|
||||||
|
public void appendString(StringBuilder sb) {
|
||||||
|
sb.append(value + " (" + defaultValue + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder log = new StringBuilder();
|
||||||
|
appendString(log);
|
||||||
|
return log.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package io.gitlab.jfronny.respackopts.data;
|
||||||
|
|
||||||
|
public class SyncMode {
|
||||||
|
public static final SyncMode RESPACK_LOAD = new SyncMode(false, true, true);
|
||||||
|
public static final SyncMode CONF_LOAD = new SyncMode(true, false, false);
|
||||||
|
|
||||||
|
public final boolean modifyValue;
|
||||||
|
public final boolean modifyDefault;
|
||||||
|
public final boolean isAdd;
|
||||||
|
|
||||||
|
private SyncMode(boolean modifyValue, boolean modifyDefault, boolean isAdd) {
|
||||||
|
this.modifyValue = modifyValue;
|
||||||
|
this.modifyDefault = modifyDefault;
|
||||||
|
this.isAdd = isAdd;
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,6 @@ public class BooleanEntrySerializer implements JsonSerializer<ConfigBooleanEntry
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(ConfigBooleanEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(ConfigBooleanEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
return new JsonPrimitive(src.value);
|
return new JsonPrimitive(src.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ public class ConfigBranchSerializer implements JsonSerializer<ConfigBranch>, Jso
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(ConfigBranch src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(ConfigBranch src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
JsonObject o = new JsonObject();
|
JsonObject o = new JsonObject();
|
||||||
for (Map.Entry<String, Entry<?>> entry : src.getEntries()) {
|
for (Map.Entry<String, Entry<?>> entry : src.getValue().entrySet()) {
|
||||||
o.add(entry.getKey(), context.serialize(entry.getValue()));
|
o.add(entry.getKey(), context.serialize(entry.getValue()));
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
|
@ -34,11 +34,10 @@ public class ConfigBranchSerializer implements JsonSerializer<ConfigBranch>, Jso
|
||||||
JsonPrimitive p = j.getAsJsonPrimitive();
|
JsonPrimitive p = j.getAsJsonPrimitive();
|
||||||
if (p.isBoolean())
|
if (p.isBoolean())
|
||||||
cbNew.add(s, new ConfigBooleanEntry(p.getAsBoolean()));
|
cbNew.add(s, new ConfigBooleanEntry(p.getAsBoolean()));
|
||||||
else if (p.isNumber()) {
|
else if (p.isNumber())
|
||||||
//TODO identify saved enum values - use string?
|
|
||||||
cbNew.add(s, context.deserialize(j, ConfigNumericEntry.class));
|
cbNew.add(s, context.deserialize(j, ConfigNumericEntry.class));
|
||||||
} else if (p.isString())
|
else if (p.isString())
|
||||||
throw new JsonSyntaxException("String primitives are not currently supported");
|
cbNew.add(s, context.deserialize(j, ConfigEnumEntry.class));
|
||||||
}
|
}
|
||||||
else if (j.isJsonArray())
|
else if (j.isJsonArray())
|
||||||
cbNew.add(s, context.deserialize(j, ConfigEnumEntry.class));
|
cbNew.add(s, context.deserialize(j, ConfigEnumEntry.class));
|
||||||
|
|
|
@ -9,7 +9,7 @@ import java.util.HashMap;
|
||||||
public class EnumEntrySerializer implements JsonSerializer<ConfigEnumEntry>, JsonDeserializer<ConfigEnumEntry> {
|
public class EnumEntrySerializer implements JsonSerializer<ConfigEnumEntry>, JsonDeserializer<ConfigEnumEntry> {
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(ConfigEnumEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(ConfigEnumEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
return new JsonPrimitive(src.value == null ? 0 : src.value);
|
return new JsonPrimitive(src.getValueName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -18,7 +18,12 @@ public class EnumEntrySerializer implements JsonSerializer<ConfigEnumEntry>, Jso
|
||||||
if (json.isJsonPrimitive()) {
|
if (json.isJsonPrimitive()) {
|
||||||
JsonPrimitive jp = json.getAsJsonPrimitive();
|
JsonPrimitive jp = json.getAsJsonPrimitive();
|
||||||
if (jp.isNumber()) {
|
if (jp.isNumber()) {
|
||||||
result.value = jp.getAsInt();
|
result.setValue(jp.getAsInt());
|
||||||
|
result.setDefault(jp.getAsInt());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else if (jp.isString()) {
|
||||||
|
result.setFieldName(jp.getAsString());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -28,7 +28,8 @@ public class LegacyConfigDeserializer {
|
||||||
}
|
}
|
||||||
for (Map.Entry<String, Double> e : lc.doubles.entrySet()) {
|
for (Map.Entry<String, Double> e : lc.doubles.entrySet()) {
|
||||||
ConfigNumericEntry ne = new ConfigNumericEntry();
|
ConfigNumericEntry ne = new ConfigNumericEntry();
|
||||||
ne.value = e.getValue();
|
ne.setValue(e.getValue());
|
||||||
|
ne.setDefault(e.getValue());
|
||||||
int i = e.getKey().lastIndexOf('.');
|
int i = e.getKey().lastIndexOf('.');
|
||||||
if (i == -1)
|
if (i == -1)
|
||||||
cb.add(e.getKey(), ne);
|
cb.add(e.getKey(), ne);
|
||||||
|
|
|
@ -8,13 +8,14 @@ import java.lang.reflect.Type;
|
||||||
public class NumericEntrySerializer implements JsonSerializer<ConfigNumericEntry>, JsonDeserializer<ConfigNumericEntry> {
|
public class NumericEntrySerializer implements JsonSerializer<ConfigNumericEntry>, JsonDeserializer<ConfigNumericEntry> {
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(ConfigNumericEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(ConfigNumericEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
return new JsonPrimitive(src.value);
|
return new JsonPrimitive(src.getValue());
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public ConfigNumericEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
public ConfigNumericEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||||
ConfigNumericEntry result = new ConfigNumericEntry();
|
ConfigNumericEntry result = new ConfigNumericEntry();
|
||||||
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
|
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
|
||||||
result.value = json.getAsDouble();
|
result.setValue(json.getAsDouble());
|
||||||
|
result.setDefault(json.getAsDouble());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
else if (isSlider(json)) {
|
else if (isSlider(json)) {
|
||||||
|
@ -32,7 +33,8 @@ public class NumericEntrySerializer implements JsonSerializer<ConfigNumericEntry
|
||||||
throw new JsonSyntaxException("Default value out of range in slider definition");
|
throw new JsonSyntaxException("Default value out of range in slider definition");
|
||||||
}
|
}
|
||||||
if (isWhole(defV) && isWhole(minV) && isWhole(maxV)) {
|
if (isWhole(defV) && isWhole(minV) && isWhole(maxV)) {
|
||||||
result.value = defV;
|
result.setValue(defV);
|
||||||
|
result.setDefault(defV);
|
||||||
result.min = minV;
|
result.min = minV;
|
||||||
result.max = maxV;
|
result.max = maxV;
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -2,7 +2,7 @@ package io.gitlab.jfronny.respackopts.mixin;
|
||||||
|
|
||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
import io.gitlab.jfronny.respackopts.Respackopts;
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
||||||
import io.gitlab.jfronny.respackopts.data.ConfigBranch;
|
import io.gitlab.jfronny.respackopts.data.SyncMode;
|
||||||
import io.gitlab.jfronny.respackopts.data.in.Respackmeta;
|
import io.gitlab.jfronny.respackopts.data.in.Respackmeta;
|
||||||
import net.minecraft.resource.ResourcePackManager;
|
import net.minecraft.resource.ResourcePackManager;
|
||||||
import net.minecraft.resource.ResourcePackProfile;
|
import net.minecraft.resource.ResourcePackProfile;
|
||||||
|
@ -16,7 +16,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Mixin(ResourcePackManager.class)
|
@Mixin(ResourcePackManager.class)
|
||||||
|
@ -36,7 +35,7 @@ public class ResourcePackManagerMixin {
|
||||||
if (!Respackopts.CONFIG_BRANCH.containsKey(conf.id))
|
if (!Respackopts.CONFIG_BRANCH.containsKey(conf.id))
|
||||||
Respackopts.CONFIG_BRANCH.put(conf.id, conf.conf);
|
Respackopts.CONFIG_BRANCH.put(conf.id, conf.conf);
|
||||||
else
|
else
|
||||||
Respackopts.CONFIG_BRANCH.get(conf.id).loadValues(conf.conf, true);
|
Respackopts.CONFIG_BRANCH.get(conf.id).sync(conf.conf, SyncMode.RESPACK_LOAD);
|
||||||
Respackopts.resPackMetas.put(v.getDisplayName().asString(), conf);
|
Respackopts.resPackMetas.put(v.getDisplayName().asString(), conf);
|
||||||
Respackopts.load(conf.id);
|
Respackopts.load(conf.id);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user