From 358211ca662c72cc85f32ae28bd6094b78b62ebd Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 2 Jul 2019 11:54:59 -0700 Subject: [PATCH] Fix type cast issues in ForgeConfig values. Closes #5894 Added config value set and save functions. By default mod configs are auto-saved, so you do not need to call save unless you create your own Config object. --- .../common/ForgeConfigSpec.java | 88 ++++++++++++++----- 1 file changed, 65 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java b/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java index 8642bd2173..071747d057 100644 --- a/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java +++ b/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java @@ -59,21 +59,21 @@ import com.google.common.collect.Lists; import com.google.common.collect.ObjectArrays; /* - * Like {@link com.electronwill.nightconfig.core.ConfigSpec} except in builder format, and extended to acept comments, language keys, + * Like {@link com.electronwill.nightconfig.core.ConfigSpec} except in builder format, and extended to accept comments, language keys, * and other things Forge configs would find useful. */ public class ForgeConfigSpec extends UnmodifiableConfigWrapper { private Map, String> levelComments = new HashMap<>(); - + private Config childConfig; - + private ForgeConfigSpec(Config storage, Map, String> levelComments) { super(storage); this.levelComments = levelComments; } - + public void setConfig(CommentedConfig config) { this.childConfig = config; if (!isCorrect(config)) { @@ -87,6 +87,14 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper } } + public void save() + { + Preconditions.checkNotNull(childConfig, "Cannot save config value without assigned Config object present"); + if (childConfig instanceof FileConfig) { + ((FileConfig)childConfig).save(); + } + } + public boolean isCorrect(CommentedConfig config) { LinkedList parentPath = new LinkedList<>(); return correct(this.config, config, parentPath, Collections.unmodifiableList( parentPath ), null, true) == 0; @@ -328,6 +336,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper public > EnumValue defineEnum(List path, V defaultValue, Collection acceptableValues) { return defineEnum(path, defaultValue, EnumGetMethod.NAME_IGNORECASE, acceptableValues); } + @SuppressWarnings("unchecked") public > EnumValue defineEnum(List path, V defaultValue, EnumGetMethod converter, Collection acceptableValues) { return defineEnum(path, defaultValue, converter, obj -> { if (obj instanceof Enum) { @@ -399,7 +408,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper public DoubleValue defineInRange(List path, Supplier defaultSupplier, double min, double max) { return new DoubleValue(this, defineInRange(path, defaultSupplier, min, max, Double.class).getPath(), defaultSupplier); } - + //Ints public IntValue defineInRange(String path, int defaultValue, int min, int max) { return defineInRange(split(path), defaultValue, min, max); @@ -413,7 +422,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper public IntValue defineInRange(List path, Supplier defaultSupplier, int min, int max) { return new IntValue(this, defineInRange(path, defaultSupplier, min, max, Integer.class).getPath(), defaultSupplier); } - + //Longs public LongValue defineInRange(String path, long defaultValue, long min, long max) { return defineInRange(split(path), defaultValue, min, max); @@ -559,7 +568,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper public Class getClazz() { return clazz; } public V getMin() { return min; } public V getMax() { return max; } - + private boolean isNumber(Object other) { return Number.class.isAssignableFrom(clazz) && other instanceof Number; @@ -577,7 +586,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper V c = clazz.cast(t); return c.compareTo(min) >= 0 && c.compareTo(max) <= 0; } - + public Object correct(Object value, Object def) { if (isNumber(value)) @@ -589,7 +598,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper V c = clazz.cast(value); return c.compareTo(min) < 0 ? min : c.compareTo(max) > 0 ? max : value; } - + @Override public String toString() { @@ -645,15 +654,15 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper return _default; } } - + public static class ConfigValue { private final Builder parent; private final List path; private final Supplier defaultSupplier; - + private ForgeConfigSpec spec; - + ConfigValue(Builder parent, List path, Supplier defaultSupplier) { this.parent = parent; @@ -661,30 +670,44 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper this.defaultSupplier = defaultSupplier; this.parent.values.add(this); } - + public List getPath() { return Lists.newArrayList(path); } - + public T get() { Preconditions.checkNotNull(spec, "Cannot get config value before spec is built"); Preconditions.checkNotNull(spec.childConfig, "Cannot get config value without assigned Config object present"); return getRaw(spec.childConfig, path, defaultSupplier); } - + protected T getRaw(Config config, List path, Supplier defaultSupplier) { return config.getOrElse(path, defaultSupplier); } - + public Builder next() { return parent; } + + public void save() + { + Preconditions.checkNotNull(spec, "Cannot save config value before spec is built"); + Preconditions.checkNotNull(spec.childConfig, "Cannot save config value without assigned Config object present"); + spec.save(); + } + + public void set(T value) + { + Preconditions.checkNotNull(spec, "Cannot set config value before spec is built"); + Preconditions.checkNotNull(spec.childConfig, "Cannot set config value without assigned Config object present"); + spec.childConfig.set(path, value); + } } - + public static class BooleanValue extends ConfigValue { BooleanValue(Builder parent, List path, Supplier defaultSupplier) @@ -692,43 +715,62 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper super(parent, path, defaultSupplier); } } - + public static class IntValue extends ConfigValue { IntValue(Builder parent, List path, Supplier defaultSupplier) { super(parent, path, defaultSupplier); } + + @Override + protected Integer getRaw(Config config, List path, Supplier defaultSupplier) + { + return config.getIntOrElse(path, () -> defaultSupplier.get()); + } } - + public static class LongValue extends ConfigValue { LongValue(Builder parent, List path, Supplier defaultSupplier) { super(parent, path, defaultSupplier); } + + @Override + protected Long getRaw(Config config, List path, Supplier defaultSupplier) + { + return config.getLongOrElse(path, () -> defaultSupplier.get()); + } } - + public static class DoubleValue extends ConfigValue { DoubleValue(Builder parent, List path, Supplier defaultSupplier) { super(parent, path, defaultSupplier); } + + @Override + protected Double getRaw(Config config, List path, Supplier defaultSupplier) + { + Number n = config.get(path); + return n == null ? defaultSupplier.get() : n.doubleValue(); + } } - + public static class EnumValue> extends ConfigValue { private final EnumGetMethod converter; private final Class clazz; - + EnumValue(Builder parent, List path, Supplier defaultSupplier, EnumGetMethod converter, Class clazz) { super(parent, path, defaultSupplier); this.converter = converter; this.clazz = clazz; } - + @Override protected T getRaw(Config config, List path, Supplier defaultSupplier) {