Update Night-Config to 3.5.0, improve defineEnum methods

Closes #5537
This commit is contained in:
tterrag 2019-03-22 23:38:43 -04:00
parent d7ba9a50df
commit 10a06157aa
2 changed files with 91 additions and 26 deletions

View file

@ -294,8 +294,8 @@ project(':forge') {
installer 'net.minecraftforge:forgespi:0.11.+' installer 'net.minecraftforge:forgespi:0.11.+'
installer 'net.minecraftforge:coremods:0.4.+' installer 'net.minecraftforge:coremods:0.4.+'
installer 'net.minecraftforge:unsafe:0.2.+' installer 'net.minecraftforge:unsafe:0.2.+'
installer 'com.electronwill.night-config:core:3.4.2' installer 'com.electronwill.night-config:core:3.5.0'
installer 'com.electronwill.night-config:toml:3.4.2' installer 'com.electronwill.night-config:toml:3.5.0'
installer 'org.jline:jline:3.9.0' installer 'org.jline:jline:3.9.0'
installer 'org.apache.maven:maven-artifact:3.6.0' installer 'org.apache.maven:maven-artifact:3.6.0'
installer 'net.jodah:typetools:0.6.0' installer 'net.jodah:typetools:0.6.0'

View file

@ -24,7 +24,6 @@ import static com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction.REMO
import static com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction.REPLACE; import static com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction.REPLACE;
import static net.minecraftforge.fml.Logging.CORE; import static net.minecraftforge.fml.Logging.CORE;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -33,10 +32,8 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.ListIterator;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -46,20 +43,17 @@ import org.apache.logging.log4j.LogManager;
import com.electronwill.nightconfig.core.CommentedConfig; import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config; import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.InMemoryFormat; import com.electronwill.nightconfig.core.EnumGetMethod;
import com.electronwill.nightconfig.core.utils.UnmodifiableConfigWrapper;
import com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction; import com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction;
import com.electronwill.nightconfig.core.ConfigSpec.CorrectionListener; import com.electronwill.nightconfig.core.ConfigSpec.CorrectionListener;
import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.electronwill.nightconfig.core.InMemoryFormat;
import com.electronwill.nightconfig.core.file.FileConfig; import com.electronwill.nightconfig.core.file.FileConfig;
import com.electronwill.nightconfig.core.io.WritingMode; import com.electronwill.nightconfig.core.utils.UnmodifiableConfigWrapper;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.netty.util.BooleanSupplier;
/* /*
* 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 acept comments, language keys,
* and other things Forge configs would find useful. * and other things Forge configs would find useful.
@ -296,37 +290,84 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
} }
//Enum //Enum
public <V extends Enum<V>> ConfigValue<V> defineEnum(String path, V defaultValue) { public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue) {
return defineEnum(split(path), defaultValue); return defineEnum(split(path), defaultValue);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(List<String> path, V defaultValue) { public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter) {
return defineEnum(split(path), defaultValue, converter);
}
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue) {
return defineEnum(path, defaultValue, defaultValue.getDeclaringClass().getEnumConstants()); return defineEnum(path, defaultValue, defaultValue.getDeclaringClass().getEnumConstants());
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(String path, V defaultValue, @SuppressWarnings("unchecked") V... acceptableValues) { public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter) {
return defineEnum(path, defaultValue, converter, defaultValue.getDeclaringClass().getEnumConstants());
}
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, @SuppressWarnings("unchecked") V... acceptableValues) {
return defineEnum(split(path), defaultValue, acceptableValues); return defineEnum(split(path), defaultValue, acceptableValues);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(List<String> path, V defaultValue, @SuppressWarnings("unchecked") V... acceptableValues) { public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter, @SuppressWarnings("unchecked") V... acceptableValues) {
return defineEnum(path, defaultValue, Arrays.asList(acceptableValues)); return defineEnum(split(path), defaultValue, converter, acceptableValues);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(String path, V defaultValue, Collection<V> acceptableValues) { public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, @SuppressWarnings("unchecked") V... acceptableValues) {
return defineEnum(path, defaultValue, (Collection<V>) Arrays.asList(acceptableValues));
}
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter, @SuppressWarnings("unchecked") V... acceptableValues) {
return defineEnum(path, defaultValue, converter, Arrays.asList(acceptableValues));
}
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, Collection<V> acceptableValues) {
return defineEnum(split(path), defaultValue, acceptableValues); return defineEnum(split(path), defaultValue, acceptableValues);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(List<String> path, V defaultValue, Collection<V> acceptableValues) { public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter, Collection<V> acceptableValues) {
return defineEnum(path, defaultValue, acceptableValues::contains); return defineEnum(split(path), defaultValue, converter, acceptableValues);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(String path, V defaultValue, Predicate<Object> validator) { public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, Collection<V> acceptableValues) {
return defineEnum(path, defaultValue, EnumGetMethod.NAME_IGNORECASE, acceptableValues);
}
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter, Collection<V> acceptableValues) {
return defineEnum(path, defaultValue, converter, obj -> {
if (obj instanceof Enum) {
return acceptableValues.contains(obj);
}
if (obj == null) {
return false;
}
try {
return acceptableValues.contains(converter.get(obj, defaultValue.getClass()));
} catch (IllegalArgumentException | ClassCastException e) {
return false;
}
});
}
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, Predicate<Object> validator) {
return defineEnum(split(path), defaultValue, validator); return defineEnum(split(path), defaultValue, validator);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(List<String> path, V defaultValue, Predicate<Object> validator) { public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter, Predicate<Object> validator) {
return defineEnum(split(path), defaultValue, converter, validator);
}
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, Predicate<Object> validator) {
return defineEnum(path, () -> defaultValue, validator, defaultValue.getDeclaringClass()); return defineEnum(path, () -> defaultValue, validator, defaultValue.getDeclaringClass());
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(String path, Supplier<V> defaultSupplier, Predicate<Object> validator, Class<V> clazz) { public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter, Predicate<Object> validator) {
return defineEnum(path, () -> defaultValue, converter, validator, defaultValue.getDeclaringClass());
}
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, Supplier<V> defaultSupplier, Predicate<Object> validator, Class<V> clazz) {
return defineEnum(split(path), defaultSupplier, validator, clazz); return defineEnum(split(path), defaultSupplier, validator, clazz);
} }
public <V extends Enum<V>> ConfigValue<V> defineEnum(List<String> path, Supplier<V> defaultSupplier, Predicate<Object> validator, Class<V> clazz) { public <V extends Enum<V>> EnumValue<V> defineEnum(String path, Supplier<V> defaultSupplier, EnumGetMethod converter, Predicate<Object> validator, Class<V> clazz) {
return define(path, defaultSupplier, validator, clazz); return defineEnum(split(path), defaultSupplier, converter, validator, clazz);
}
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, Supplier<V> defaultSupplier, Predicate<Object> validator, Class<V> clazz) {
return defineEnum(path, defaultSupplier, EnumGetMethod.NAME_IGNORECASE, validator, clazz);
}
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, Supplier<V> defaultSupplier, EnumGetMethod converter, Predicate<Object> validator, Class<V> clazz) {
context.setClazz(clazz);
return new EnumValue<V>(this, define(path, new ValueSpec(defaultSupplier, validator, context) {
@Override
public Object correct(Object value) {
return ((Enum<?>)super.correct(value)).name();
}
}, defaultSupplier).getPath(), defaultSupplier, converter, clazz);
} }
//boolean //boolean
public BooleanValue define(String path, boolean defaultValue) { public BooleanValue define(String path, boolean defaultValue) {
@ -593,7 +634,12 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
{ {
Preconditions.checkNotNull(spec, "Cannot get config value before spec is built"); Preconditions.checkNotNull(spec, "Cannot get config value before spec is built");
Preconditions.checkNotNull(spec.childConfig, "Cannot get config value without assigned Config object present"); Preconditions.checkNotNull(spec.childConfig, "Cannot get config value without assigned Config object present");
return spec.childConfig.getOrElse(path, defaultSupplier); return getRaw(spec.childConfig, path, defaultSupplier);
}
protected T getRaw(Config config, List<String> path, Supplier<T> defaultSupplier)
{
return config.getOrElse(path, defaultSupplier);
} }
public Builder next() public Builder next()
@ -633,6 +679,25 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
super(parent, path, defaultSupplier); super(parent, path, defaultSupplier);
} }
} }
public static class EnumValue<T extends Enum<T>> extends ConfigValue<T>
{
private final EnumGetMethod converter;
private final Class<T> clazz;
EnumValue(Builder parent, List<String> path, Supplier<T> defaultSupplier, EnumGetMethod converter, Class<T> clazz)
{
super(parent, path, defaultSupplier);
this.converter = converter;
this.clazz = clazz;
}
@Override
protected T getRaw(Config config, List<String> path, Supplier<T> defaultSupplier)
{
return config.getEnumOrElse(path, clazz, converter, defaultSupplier);
}
}
private static final Joiner LINE_JOINER = Joiner.on("\n"); private static final Joiner LINE_JOINER = Joiner.on("\n");
private static final Joiner DOT_JOINER = Joiner.on("."); private static final Joiner DOT_JOINER = Joiner.on(".");