diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/ConfigBase.java b/core/src/main/java/com/seibel/distanthorizons/core/config/ConfigBase.java index 7b0ddd4e3..9d8e601a9 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/ConfigBase.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/ConfigBase.java @@ -52,12 +52,7 @@ public class ConfigBase */ public static final List> acceptableInputs = new ArrayList>() {{ add(Boolean.class); - add(Byte.class); - add(Integer.class); - add(Double.class); - add(Short.class); - add(Long.class); - add(Float.class); + add(Number.class); // Contains: Byte, Short, Int, Long, Double, Float0 add(String.class); // TODO[CONFIG]: Check the type of these is valid diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/NumberUtil.java b/core/src/main/java/com/seibel/distanthorizons/core/config/NumberUtil.java index 9d63ce909..1731da4d0 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/NumberUtil.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/NumberUtil.java @@ -4,7 +4,7 @@ import java.util.HashMap; import java.util.Map; /** - * Gets the minimum or maximum values of a given type + * Helps with working with numbers that the value of which is unknown * * @author coolGi * @version 2023-7-16 @@ -35,4 +35,34 @@ public class NumberUtil { public static Number getMaximum(Class c) { return maxValues.get(c); } + + /** Does a greater than (>) operator on any number */ + public static boolean greaterThan(Number a, Number b) { + if (a.getClass() != b.getClass()) + return false; + Class typeClass = a.getClass(); + + if (typeClass == Byte.class) return a.byteValue() > b.byteValue(); + if (typeClass == Short.class) return a.shortValue() > b.shortValue(); + if (typeClass == Integer.class) return a.intValue() > b.intValue(); + if (typeClass == Long.class) return a.longValue() > b.longValue(); + if (typeClass == Double.class) return a.doubleValue() > b.doubleValue(); + if (typeClass == Float.class) return a.floatValue() > b.floatValue(); + return false; + } + + /** Does a less than (<) operator on any number */ + public static boolean lessThan(Number a, Number b) { + if (a.getClass() != b.getClass()) + return false; + Class typeClass = a.getClass(); + + if (typeClass == Byte.class) return a.byteValue() < b.byteValue(); + if (typeClass == Short.class) return a.shortValue() < b.shortValue(); + if (typeClass == Integer.class) return a.intValue() < b.intValue(); + if (typeClass == Long.class) return a.longValue() < b.longValue(); + if (typeClass == Double.class) return a.doubleValue() < b.doubleValue(); + if (typeClass == Float.class) return a.floatValue() < b.floatValue(); + return false; + } } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java b/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java index 76112607b..a297bdb3e 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java @@ -42,7 +42,8 @@ public class ConfigEntry extends AbstractConfigType> implem super(appearance, value); this.defaultValue = value; this.comment = comment; - this.setMinMax(min, max); + this.min = min; + this.max = max; this.allowApiOverride = allowApiOverride; this.performance = performance; this.listenerList = listenerList; @@ -104,23 +105,13 @@ public class ConfigEntry extends AbstractConfigType> implem public T getMin() { return this.min; } /** Sets the min value */ @Override - @SuppressWarnings("unchecked") // Suppress due to its always safe - public void setMin(T newMin) { - if (newMin == null) - newMin = (T) NumberUtil.getMinimum(this.value.getClass()); - this.min = newMin; - } + public void setMin(T newMin) { this.min = newMin; } /** Gets the max value */ @Override public T getMax() { return this.max; } /** Sets the max value */ @Override - @SuppressWarnings("unchecked") // Suppress due to its always safe - public void setMax(T newMax) { - if (newMax == null) - newMax = (T) NumberUtil.getMinimum(this.value.getClass()); - this.max = newMax; - } + public void setMax(T newMax) { this.max = newMax; } /** Sets the min and max within a single setter */ @Override public void setMinMax(T newMin, T newMax) { @@ -219,9 +210,9 @@ public class ConfigEntry extends AbstractConfigType> 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 && Float.parseFloat(value.toString()) > Float.parseFloat(max.toString())) + if (max != null && NumberUtil.greaterThan((Number) value, (Number) max)) return 1; - if (min != null && Float.parseFloat(value.toString()) < Float.parseFloat(min.toString())) + if (min != null && NumberUtil.lessThan((Number) value, (Number) min)) return -1; return 0;