mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-11 20:47:32 +00:00
fix config list/string handling
This commit is contained in:
6 files changed
+89
-60
No files matched your search
+29
-15
@@ -29,36 +29,50 @@ import com.falsepattern.lib.internal.impl.config.ConfigFieldParameters;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.val;
|
||||
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public abstract class AListConfigField<T> extends AConfigField<T> {
|
||||
public abstract class AListConfigField<T, E extends Annotation> extends AConfigField<T> {
|
||||
protected final boolean fixedLength;
|
||||
protected final int maxLength;
|
||||
protected final T defaultValue;
|
||||
|
||||
protected AListConfigField(ConfigFieldParameters params, Property.Type type)
|
||||
|
||||
protected AListConfigField(ConfigFieldParameters params,
|
||||
Property.Type type,
|
||||
Class<E> defaultAnnotation,
|
||||
Function<E, T> defaultValueGetter,
|
||||
BiConsumer<Property, T> defaultValueSetter
|
||||
)
|
||||
throws ConfigException {
|
||||
super(params, type, true);
|
||||
fixedLength = field.isAnnotationPresent(Config.ListFixedLength.class);
|
||||
maxLength = Optional.ofNullable(field.getAnnotation(Config.ListMaxLength.class))
|
||||
.map(Config.ListMaxLength::value)
|
||||
.orElse(256);
|
||||
if (maxLength < 0) {
|
||||
throw new ConfigException("Negative length list configurations are not supported!\n"
|
||||
+ "Field name: "
|
||||
+ field.getName()
|
||||
+ ", class: "
|
||||
+ field.getDeclaringClass().getName());
|
||||
}
|
||||
property.setIsListLengthFixed(fixedLength);
|
||||
defaultValue = Optional.ofNullable(field.getAnnotation(defaultAnnotation))
|
||||
.map(defaultValueGetter)
|
||||
.orElseThrow(() -> noDefault(field, defaultAnnotation.getSimpleName()));
|
||||
defaultValueSetter.accept(property, defaultValue);
|
||||
if (fixedLength) {
|
||||
maxLength = Array.getLength(defaultValue);
|
||||
} else {
|
||||
maxLength = Optional.ofNullable(field.getAnnotation(Config.ListMaxLength.class))
|
||||
.map(Config.ListMaxLength::value)
|
||||
.orElse(-1);
|
||||
}
|
||||
property.setMaxListLength(maxLength);
|
||||
property.comment += "\n[fixed length: " + (fixedLength ? "yes" : "no") + ", max length: " + maxLength + "]";
|
||||
if (fixedLength) {
|
||||
property.comment += "\n[fixed length: " + maxLength + "]";
|
||||
} else if (maxLength >= 0) {
|
||||
property.comment += "\n[max length: " + maxLength + "]";
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract int length(T arr);
|
||||
|
||||
+7
-7
@@ -37,15 +37,15 @@ import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BooleanListConfigField extends AListConfigField<boolean[]> {
|
||||
private final boolean[] defaultValue;
|
||||
public class BooleanListConfigField extends AListConfigField<boolean[], Config.DefaultBooleanList> {
|
||||
|
||||
public BooleanListConfigField(ConfigFieldParameters params) throws ConfigException {
|
||||
super(params, Property.Type.BOOLEAN);
|
||||
defaultValue = Optional.ofNullable(field.getAnnotation(Config.DefaultBooleanList.class))
|
||||
.map(Config.DefaultBooleanList::value)
|
||||
.orElseThrow(() -> noDefault(field, "DefaultBooleanList"));
|
||||
property.setDefaultValues(defaultValue);
|
||||
super(params,
|
||||
Property.Type.BOOLEAN,
|
||||
Config.DefaultBooleanList.class,
|
||||
Config.DefaultBooleanList::value,
|
||||
Property::setDefaultValues
|
||||
);
|
||||
if (!property.isBooleanList()) {
|
||||
setToDefault();
|
||||
}
|
||||
|
||||
+7
-7
@@ -38,20 +38,20 @@ import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public class DoubleListConfigField extends AListConfigField<double[]> {
|
||||
public class DoubleListConfigField extends AListConfigField<double[], Config.DefaultDoubleList> {
|
||||
private final double min;
|
||||
private final double max;
|
||||
private final double[] defaultValue;
|
||||
|
||||
public DoubleListConfigField(ConfigFieldParameters params) throws ConfigException {
|
||||
super(params, Property.Type.DOUBLE);
|
||||
super(params,
|
||||
Property.Type.DOUBLE,
|
||||
Config.DefaultDoubleList.class,
|
||||
Config.DefaultDoubleList::value,
|
||||
Property::setDefaultValues
|
||||
);
|
||||
val range = Optional.ofNullable(field.getAnnotation(Config.RangeDouble.class));
|
||||
min = range.map(Config.RangeDouble::min).orElse(-Double.MAX_VALUE);
|
||||
max = range.map(Config.RangeDouble::max).orElse(Double.MAX_VALUE);
|
||||
defaultValue = Optional.ofNullable(field.getAnnotation(Config.DefaultDoubleList.class))
|
||||
.map(Config.DefaultDoubleList::value)
|
||||
.orElseThrow(() -> noDefault(field, "DefaultDoubleList"));
|
||||
property.setDefaultValues(defaultValue);
|
||||
property.setMinValue(min);
|
||||
property.setMaxValue(max);
|
||||
try {
|
||||
|
||||
+7
-7
@@ -38,20 +38,20 @@ import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public class IntListConfigField extends AListConfigField<int[]> {
|
||||
public class IntListConfigField extends AListConfigField<int[], Config.DefaultIntList> {
|
||||
private final int min;
|
||||
private final int max;
|
||||
private final int[] defaultValue;
|
||||
|
||||
public IntListConfigField(ConfigFieldParameters params) throws ConfigException {
|
||||
super(params, Property.Type.INTEGER);
|
||||
super(params,
|
||||
Property.Type.INTEGER,
|
||||
Config.DefaultIntList.class,
|
||||
Config.DefaultIntList::value,
|
||||
Property::setDefaultValues
|
||||
);
|
||||
val range = Optional.ofNullable(field.getAnnotation(Config.RangeInt.class));
|
||||
min = range.map(Config.RangeInt::min).orElse(Integer.MIN_VALUE);
|
||||
max = range.map(Config.RangeInt::max).orElse(Integer.MAX_VALUE);
|
||||
defaultValue = Optional.ofNullable(field.getAnnotation(Config.DefaultIntList.class))
|
||||
.map(Config.DefaultIntList::value)
|
||||
.orElseThrow(() -> noDefault(field, "DefaultIntList"));
|
||||
property.setDefaultValues(defaultValue);
|
||||
property.setMinValue(min);
|
||||
property.setMaxValue(max);
|
||||
if (!property.isIntList()) {
|
||||
|
||||
+30
-9
@@ -53,14 +53,35 @@ public class StringConfigField extends AConfigField<String> {
|
||||
.orElseThrow(() -> noDefault(field, "DefaultString"));
|
||||
maxLength = Optional.ofNullable(field.getAnnotation(Config.StringMaxLength.class))
|
||||
.map(Config.StringMaxLength::value)
|
||||
.orElse(256);
|
||||
.orElse(-1);
|
||||
property.setDefaultValue(defaultValue);
|
||||
property.comment += "\n[max length: "
|
||||
+ maxLength
|
||||
+ (pattern != null ? ", pattern: \"" + pattern.pattern() + "\"" : "")
|
||||
+ ", default: \""
|
||||
+ defaultValue
|
||||
+ "\"]";
|
||||
property.comment += generateStringComment(maxLength, pattern, defaultValue);
|
||||
}
|
||||
|
||||
public static String generateStringComment(int maxStringLength, Pattern pattern, String defaultValue) {
|
||||
val cmt = new StringBuilder();
|
||||
boolean comma = false;
|
||||
if (maxStringLength >= 0) {
|
||||
cmt.append("\n[max string length: ").append(maxStringLength);
|
||||
comma = true;
|
||||
}
|
||||
if (pattern != null) {
|
||||
if (comma) {
|
||||
cmt.append(", ");
|
||||
} else {
|
||||
cmt.append("\n[");
|
||||
}
|
||||
cmt.append("pattern: \"").append(pattern.pattern()).append("\"");
|
||||
comma = true;
|
||||
}
|
||||
if (comma) {
|
||||
cmt.append(", ");
|
||||
} else {
|
||||
cmt.append("\n[");
|
||||
}
|
||||
cmt.append("default: ").append(defaultValue);
|
||||
cmt.append("]");
|
||||
return cmt.toString();
|
||||
}
|
||||
|
||||
public static boolean validateString(String value, int maxLength, Pattern pattern, Field field, int listIndex) {
|
||||
@@ -68,7 +89,7 @@ public class StringConfigField extends AConfigField<String> {
|
||||
ConfigValidationFailureEvent.fieldIsNull(field, listIndex);
|
||||
return false;
|
||||
}
|
||||
if (value.length() > maxLength) {
|
||||
if (maxLength >= 0 && value.length() > maxLength) {
|
||||
ConfigValidationFailureEvent.postStringSizeOutOfBounds(field, listIndex, value, maxLength);
|
||||
return false;
|
||||
}
|
||||
@@ -87,7 +108,7 @@ public class StringConfigField extends AConfigField<String> {
|
||||
public static String receiveString(DataInput input, int maxLength, String valueName, String className)
|
||||
throws IOException {
|
||||
val length = input.readInt();
|
||||
if (length > maxLength || length < 0) {
|
||||
if ((maxLength >= 0 && length > maxLength) || length < 0) {
|
||||
throw new IOException("Error while retrieving value for "
|
||||
+ valueName
|
||||
+ " in class "
|
||||
|
||||
+9
-15
@@ -38,33 +38,27 @@ import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class StringListConfigField extends AListConfigField<String[]> {
|
||||
private final String[] defaultValue;
|
||||
public class StringListConfigField extends AListConfigField<String[], Config.DefaultStringList> {
|
||||
private final Pattern pattern;
|
||||
private final int maxStringLength;
|
||||
|
||||
public StringListConfigField(ConfigFieldParameters params) throws ConfigException {
|
||||
super(params, Property.Type.STRING);
|
||||
super(params,
|
||||
Property.Type.STRING,
|
||||
Config.DefaultStringList.class,
|
||||
Config.DefaultStringList::value,
|
||||
Property::setDefaultValues
|
||||
);
|
||||
pattern = Optional.ofNullable(field.getAnnotation(Config.Pattern.class))
|
||||
.map((ptr) -> Pattern.compile(ptr.value()))
|
||||
.orElse(null);
|
||||
defaultValue = Optional.ofNullable(field.getAnnotation(Config.DefaultStringList.class))
|
||||
.map(Config.DefaultStringList::value)
|
||||
.orElseThrow(() -> noDefault(field, "DefaultStringList"));
|
||||
maxStringLength = Optional.ofNullable(field.getAnnotation(Config.StringMaxLength.class))
|
||||
.map(Config.StringMaxLength::value)
|
||||
.orElse(256);
|
||||
property.setDefaultValues(defaultValue);
|
||||
.orElse(-1);
|
||||
if (!property.isList()) {
|
||||
setToDefault();
|
||||
}
|
||||
property.comment +=
|
||||
"\n[max string length: "
|
||||
+ maxLength
|
||||
+ (pattern != null ? ", pattern: \"" + pattern.pattern() + "\"" : "")
|
||||
+ ", default: \""
|
||||
+ stringify(defaultValue)
|
||||
+ "\"]";
|
||||
property.comment += StringConfigField.generateStringComment(maxStringLength, pattern, stringify(defaultValue));
|
||||
}
|
||||
|
||||
private static String stringify(String[] arr) {
|
||||
|
||||
Reference in new issue
Block a user