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 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; } } } 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();