aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2024-10-22 18:29:43 +0100
committerisXander <xander@isxander.dev>2024-10-22 18:29:43 +0100
commiteac29444deae2a148262348a4d502c2277e64e7a (patch)
treee7f2cfc177bc574e1f78c4d32aef28c79a15c771 /src
parentb2293d323d7b9f9f12c10898e6fea59bb4f69f5a (diff)
downloadYetAnotherConfigLib-eac29444deae2a148262348a4d502c2277e64e7a.tar.gz
YetAnotherConfigLib-eac29444deae2a148262348a4d502c2277e64e7a.tar.bz2
YetAnotherConfigLib-eac29444deae2a148262348a4d502c2277e64e7a.zip
Fix #214
Diffstat (limited to 'src')
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java b/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java
index 3c06876..8e1a399 100644
--- a/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java
+++ b/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java
@@ -44,7 +44,8 @@ public abstract class NumberFieldController<T extends Number> implements ISlider
@Override
public void setFromString(String value) {
try {
- setPendingValue(Mth.clamp(NUMBER_FORMAT.parse(value).doubleValue(), min(), max()));
+ String transformed = transformInput(value);
+ setPendingValue(Mth.clamp(NUMBER_FORMAT.parse(transformed).doubleValue(), min(), max()));
} catch (ParseException ignore) {
YACLConstants.LOGGER.warn("Failed to parse number: {}", value);
}
@@ -57,7 +58,8 @@ public abstract class NumberFieldController<T extends Number> implements ISlider
@Override
public boolean isInputValid(String input) {
- input = input.replace(DECIMAL_FORMAT_SYMBOLS.getGroupingSeparator() + "", "");
+ input = transformInput(input);
+
ParsePosition parsePosition = new ParsePosition(0);
NUMBER_FORMAT.parse(input, parsePosition);
return parsePosition.getIndex() == input.length();
@@ -77,4 +79,11 @@ public abstract class NumberFieldController<T extends Number> implements ISlider
public double interval() {
return -1;
}
+
+ protected String transformInput(String input) {
+ if (input.isEmpty()) input = "0";
+ if (input.equals("-")) input = "-0";
+
+ return input.replace(DECIMAL_FORMAT_SYMBOLS.getGroupingSeparator() + "", "");
+ }
}