From e81c330694211ee0cdf736500ab75a7767fd69a3 Mon Sep 17 00:00:00 2001 From: tterrag Date: Mon, 24 Jun 2019 17:41:51 -0400 Subject: [PATCH] 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 --- .../common/ForgeConfigSpec.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java b/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java index 07563b2a4f..8642bd2173 100644 --- a/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java +++ b/src/main/java/net/minecraftforge/common/ForgeConfigSpec.java @@ -559,15 +559,37 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper public Class 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 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() {