Fix configs correcting numeric values when the primitive type differs

Fix configs with range not clamping the corrected value, and instead
just replacing with the default

Closes #5855
This commit is contained in:
tterrag 2019-06-24 17:41:51 -04:00 committed by tterrag1098
parent 790da699dd
commit e81c330694
1 changed files with 23 additions and 1 deletions

View File

@ -559,15 +559,37 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
public Class<? extends V> getClazz() { return clazz; }
public V getMin() { return min; }
public V getMax() { return max; }
private boolean isNumber(Object other)
{
return Number.class.isAssignableFrom(clazz) && other instanceof Number;
}
@Override
public boolean test(Object t)
{
if (isNumber(t))
{
Number n = (Number) t;
return ((Number)min).doubleValue() <= n.doubleValue() && n.doubleValue() <= ((Number)max).doubleValue();
}
if (!clazz.isInstance(t)) return false;
V c = clazz.cast(t);
return c.compareTo(min) >= 0 && c.compareTo(max) <= 0;
}
public Object correct(Object value, Object def)
{
if (isNumber(value))
{
Number n = (Number) value;
return n.doubleValue() < ((Number)min).doubleValue() ? min : n.doubleValue() > ((Number)max).doubleValue() ? max : value;
}
if (!clazz.isInstance(value)) return def;
V c = clazz.cast(value);
return c.compareTo(min) < 0 ? min : c.compareTo(max) > 0 ? max : value;
}
@Override
public String toString()
{
@ -614,7 +636,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<Config>
public boolean needsWorldRestart() { return this.worldRestart; }
public Class<?> getClazz(){ return this.clazz; }
public boolean test(Object value) { return validator.test(value); }
public Object correct(Object value) { return getDefault(); }
public Object correct(Object value) { return range == null ? getDefault() : range.correct(value, getDefault()); }
public Object getDefault()
{