From 566508ceced17b639a69153e8eace47d5bc4fff0 Mon Sep 17 00:00:00 2001 From: Des Herriott Date: Thu, 7 Nov 2024 08:58:38 +0000 Subject: [PATCH 1/3] fix: better IntTextBox validation https://github.com/FTBTeam/FTB-Mods-Issues/issues/1382 --- .../ftb/mods/ftblibrary/ui/IntTextBox.java | 24 ++++++++++--------- .../dev/ftb/mods/ftblibrary/ui/TextBox.java | 10 +++++++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/IntTextBox.java b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/IntTextBox.java index cc17a58..a744b46 100644 --- a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/IntTextBox.java +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/IntTextBox.java @@ -1,22 +1,32 @@ package dev.ftb.mods.ftblibrary.ui; -import dev.ftb.mods.ftblibrary.ui.input.KeyModifiers; +import net.minecraft.util.Mth; import java.util.function.Predicate; public class IntTextBox extends TextBox { + // note: empty text and text with only '-' need to be allowed so numbers can be typed (treat as 0) + private static final Predicate IS_NUMBER = s -> s.matches("^-?[0-9]*$"); - private static final Predicate IS_NUMBER = s -> s.matches("^-?[0-9]+$"); private int min = Integer.MIN_VALUE; private int max = Integer.MAX_VALUE; public IntTextBox(Panel panel) { super(panel); setFilter(IS_NUMBER); + setStrictValidity(true); } public int getIntValue() { - return Integer.parseInt(getText()); + String text = getText(); + if (text.isEmpty() || text.equals("-")) { + return Mth.clamp(0, min, max); + } + try { + return Integer.parseInt(text); + } catch (NumberFormatException ignored) { + return Mth.clamp(0, min, max); + } } public void setMin(int min) { @@ -59,12 +69,4 @@ public void ensureValue() { setAmount(max); } } - - @Override - public boolean charTyped(char c, KeyModifiers modifiers) { - if (Character.isDigit(c)) { - return super.charTyped(c, modifiers); - } - return false; - } } \ No newline at end of file diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/TextBox.java b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/TextBox.java index f92cfdf..a97268d 100644 --- a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/TextBox.java +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/TextBox.java @@ -35,6 +35,7 @@ public class TextBox extends Widget implements IFocusableWidget { private Predicate filter; private Component label; private Color4I labelColor = Color4I.WHITE; + private boolean strictValidity = false; public TextBox(Panel panel) { super(panel); @@ -69,6 +70,10 @@ public final void setFocused(boolean focused) { } } + public void setStrictValidity(boolean strictValidity) { + this.strictValidity = strictValidity; + } + public void setFilter(Predicate filter) { this.filter = filter; } @@ -172,12 +177,15 @@ public void insertText(String string) { } String newText = (new StringBuilder(text)).replace(selStart, selEnd, filtered).toString(); + boolean prevValid = validText; validText = isValid(newText); - if (!text.equals(newText)) { + if (!text.equals(newText) && (validText || !strictValidity)) { text = newText; setCursorPosition(selStart + nToInsert); setSelectionPos(cursorPos); onTextChanged(); + } else { + validText = prevValid; } } } From a7346e851ad512533c9344df227a0c4aa686efc5 Mon Sep 17 00:00:00 2001 From: Des Herriott Date: Thu, 7 Nov 2024 08:59:25 +0000 Subject: [PATCH 2/3] chore: changelog updated --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd043a2..6fc5d39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * Overhauled and cleaned up many icon textures +### Fixed +* Fixed a client crash with certain inputs to IntTextBox (as used in FTB Chunks waypoint editing) + ## [2101.1.4] ### Added From 6997800d8164ce263777d6ab090fa45a9ec3c46b Mon Sep 17 00:00:00 2001 From: Des Herriott Date: Thu, 7 Nov 2024 09:00:25 +0000 Subject: [PATCH 3/3] chore: replaced a couple of deprecated icon usages --- .../ftb/mods/ftblibrary/ui/misc/AbstractThreePanelScreen.java | 2 +- .../dev/ftb/mods/ftblibrary/ui/misc/KeyReferenceScreen.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/AbstractThreePanelScreen.java b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/AbstractThreePanelScreen.java index 7c76faa..0770e5a 100644 --- a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/AbstractThreePanelScreen.java +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/AbstractThreePanelScreen.java @@ -145,7 +145,7 @@ public TopPanel() { super(AbstractThreePanelScreen.this); closeButton = new SimpleButton(this, Component.translatable("gui.close"), - Icons.CLOSE, (btn, mb) -> doCancel()); + Icons.CANCEL, (btn, mb) -> doCancel()); } @Override diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/KeyReferenceScreen.java b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/KeyReferenceScreen.java index a4d8f90..0b61772 100644 --- a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/KeyReferenceScreen.java +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/misc/KeyReferenceScreen.java @@ -27,7 +27,7 @@ public KeyReferenceScreen(String... translationKeys) { textPanel = new TextPanel(this); - closeButton = new SimpleTextButton(this, Component.translatable("gui.close"), Icons.CLOSE) { + closeButton = new SimpleTextButton(this, Component.translatable("gui.close"), Icons.CANCEL) { @Override public void onClicked(MouseButton button) { onBack();