Fixed up short and long not saving properly

This commit is contained in:
coolGi
2023-06-06 18:36:46 +09:30
parent 3930641fb6
commit a67cbf3435
4 changed files with 27 additions and 9 deletions
@@ -841,7 +841,7 @@ public class Config
.build();
public static ConfigEntry<Long> longTest = new ConfigEntry.Builder<Long>()
.set(42069l)
.set(42069L)
.build();
public static ConfigEntry<Float> floatTest = new ConfigEntry.Builder<Float>()
@@ -857,7 +857,7 @@ public class Config
.build();
public static ConfigEntry<Map<String, String>> mapTest = new ConfigEntry.Builder<Map<String, String>>()
.set(new HashMap<>())
.set(new HashMap<String, String>())
.build();
}
@@ -23,7 +23,6 @@ public class ConfigBase
{
/** Our own config instance, don't modify */
public static ConfigBase INSTANCE;
/** Our own config instance, dont modify */
public ConfigFileHandling configFileINSTANCE;
public static final Logger LOGGER = LogManager.getLogger(ConfigBase.class.getSimpleName());
@@ -59,13 +58,15 @@ public class ConfigBase
add(Long.class);
add(Float.class);
add(String.class);
// TODO[CONFIG]: Check the type of these is valid
add(List.class);
add(ArrayList.class);
add(Map.class); // TODO[CONFIG]: This is handled separately to check the first input is String and the second input is valid
add(Map.class);
add(HashMap.class);
}};
/** Disables the minimum and maximum of a variable */
/** Disables the minimum and maximum of any variable */
public boolean disableMinMax = false; // Very fun to use
public final List<AbstractConfigType<?, ?>> entries = new ArrayList<>();
@@ -16,6 +16,8 @@ import java.util.Map;
public class ConfigTypeConverters {
// Once you've made a converter add it to here where the first value is the type you want to convert and the 2nd value is the converter
public static final Map<Class, ConverterBase> convertObjects = new HashMap<Class, ConverterBase>() {{
put(Short.class, new ShortConverter());
put(Long.class, new LongConverter());
put(Float.class, new FloatConverter());
put(Byte.class, new ByteConverter());
@@ -51,8 +53,16 @@ public class ConfigTypeConverters {
// Float and Bytes are a bit wack with the config parser
// Some number types are a bit wack with the config parser
// So we just store them as strings
public static class ShortConverter extends ConverterBase {
@Override public String convertToString(Object item) { return ((Short) item).toString(); }
@Override public Short convertFromString(String s) { return Short.valueOf(s); }
}
public static class LongConverter extends ConverterBase {
@Override public String convertToString(Object item) { return ((Long) item).toString(); }
@Override public Long convertFromString(String s) { return Long.valueOf(s); }
}
public static class FloatConverter extends ConverterBase {
@Override public String convertToString(Object item) { return ((Float) item).toString(); }
@Override public Float convertFromString(String s) { return Float.valueOf(s); }
@@ -146,7 +146,14 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
*/
@Override
public byte isValid() { return isValid(this.value); }
/** Checks if a value is valid */
/**
* Checks if a new value is valid
*
* @return 0 == valid
* <p> 2 == invalid
* <p> 1 == number too high
* <p> -1 == number too low
*/
@Override
public byte isValid(T value) {
if (this.configBase.disableMinMax)
@@ -155,9 +162,9 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
if (value.getClass() != this.value.getClass()) // If the 2 variables aren't the same type then it will be invalid
return 2;
if (Number.class.isAssignableFrom(value.getClass())) { // Only check min max if it is a number
if (this.max != null && Double.parseDouble(value.toString()) > Double.parseDouble(max.toString())) // TODO: Use something larger and more precise like float
if (this.max != null && Float.parseFloat(value.toString()) > Float.parseFloat(max.toString()))
return 1;
if (this.min != null && Double.parseDouble(value.toString()) < Double.parseDouble(min.toString()))
if (this.min != null && Float.parseFloat(value.toString()) < Float.parseFloat(min.toString()))
return -1;
return 0;