This commit is contained in:
coolGi
2023-07-16 23:57:27 +09:30
parent 8727cd09af
commit ce3a06e410
3 changed files with 38 additions and 22 deletions
@@ -52,12 +52,7 @@ public class ConfigBase
*/
public static final List<Class<?>> acceptableInputs = new ArrayList<Class<?>>() {{
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
@@ -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;
}
}
@@ -42,7 +42,8 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>> 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<T> extends AbstractConfigType<T, ConfigEntry<T>> 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<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 && 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;