From e32839a9c504663e4c62f80e90f250d7746f4d34 Mon Sep 17 00:00:00 2001 From: Wide-Cat Date: Sun, 4 Aug 2024 18:25:45 +0100 Subject: [PATCH] Fix text box and int/double/blockpos setting issues closes #4807 --- .../gui/widgets/input/WBlockPosEdit.java | 12 +++++-- .../gui/widgets/input/WDoubleEdit.java | 11 +++--- .../gui/widgets/input/WIntEdit.java | 9 +++-- .../gui/widgets/input/WTextBox.java | 36 +++++++++---------- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WBlockPosEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WBlockPosEdit.java index cca613b5fb..44cdb8999e 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WBlockPosEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WBlockPosEdit.java @@ -128,7 +128,9 @@ private void addTextBox() { lastValue = value; if (textBoxX.get().isEmpty()) set(new BlockPos(0, 0, 0)); else { - set(new BlockPos(Integer.parseInt(textBoxX.get()), value.getY(), value.getZ())); + try { + set(new BlockPos(Integer.parseInt(textBoxX.get()), value.getY(), value.getZ())); + } catch (NumberFormatException ignored) {} } newValueCheck(); }; @@ -137,7 +139,9 @@ private void addTextBox() { lastValue = value; if (textBoxY.get().isEmpty()) set(new BlockPos(0, 0, 0)); else { - set(new BlockPos(value.getX(), Integer.parseInt(textBoxY.get()), value.getZ())); + try { + set(new BlockPos(value.getX(), Integer.parseInt(textBoxY.get()), value.getZ())); + } catch (NumberFormatException ignored) {} } newValueCheck(); }; @@ -146,7 +150,9 @@ private void addTextBox() { lastValue = value; if (textBoxZ.get().isEmpty()) set(new BlockPos(0, 0, 0)); else { - set(new BlockPos(value.getX(), value.getY(), Integer.parseInt(textBoxZ.get()))); + try { + set(new BlockPos(value.getX(), value.getY(), Integer.parseInt(textBoxZ.get()))); + } catch (NumberFormatException ignored) {} } newValueCheck(); }; diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java index 885363ceb8..47fed3323a 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java @@ -15,7 +15,7 @@ public class WDoubleEdit extends WHorizontalList { private final double min, max; private final double sliderMin, sliderMax; - public int decimalPlaces = 3; + public int decimalPlaces; public boolean noSlider = false; public boolean small = false; @@ -53,7 +53,11 @@ public void init() { else if (textBox.get().equals("-")) value = -0; else if (textBox.get().equals(".")) value = 0; else if (textBox.get().equals("-.")) value = 0; - else value = Double.parseDouble(textBox.get()); + else { + try { + value = Double.parseDouble(textBox.get()); + } catch (NumberFormatException ignored) {} + } double preValidationValue = value; @@ -114,8 +118,7 @@ private void setButton(double v) { if (this.value == v) return; if (v < min) this.value = min; - else if (v > max) this.value = max; - else this.value = v; + else this.value = Math.min(v, max); if (this.value == v) { textBox.set(valueString()); diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java index b8cc53a88f..c3cc4287a6 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java @@ -48,7 +48,11 @@ public void init() { if (textBox.get().isEmpty()) value = 0; else if (textBox.get().equals("-")) value = -0; - else value = Integer.parseInt(textBox.get()); + else { + try { + value = Integer.parseInt(textBox.get()); + } catch (NumberFormatException ignored) {} + } if (slider != null) slider.set(value); @@ -99,8 +103,7 @@ private void setButton(int v) { if (this.value == v) return; if (v < min) this.value = min; - else if (v > max) this.value = max; - else this.value = v; + else this.value = Math.min(v, max); if (this.value == v) { textBox.set(Integer.toString(this.value)); diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java index b2e1c9efd1..2aa35dc543 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java @@ -277,18 +277,16 @@ public boolean onKeyRepeated(int key, int mods) { int addedChars = 0; StringBuilder sb = new StringBuilder(text.length() + clipboard.length()); - sb.append(text, 0, cursor); + sb.append(text); for (int i = 0; i < clipboard.length(); i++) { char c = clipboard.charAt(i); - if (filter.filter(text, c)) { - sb.append(c); + if (filter.filter(sb.toString(), c)) { + sb.insert(cursor + addedChars, c); addedChars++; } } - sb.append(text, cursor, text.length()); - text = sb.toString(); cursor += addedChars; resetSelection(); @@ -319,24 +317,24 @@ else if (cursor != selectionStart || cursor != selectionEnd) { return true; } else if (key == GLFW_KEY_DELETE) { - if (cursor == selectionStart && cursor == selectionEnd) { - if (cursor < text.length()) { - String preText = text; + if (cursor == selectionStart && cursor == selectionEnd) { + if (cursor < text.length()) { + String preText = text; - int count = mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_ALT : MinecraftClient.IS_SYSTEM_MAC ? GLFW_MOD_SUPER : GLFW_MOD_CONTROL) - ? text.length() - cursor - : (mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_CONTROL : GLFW_MOD_ALT)) - ? countToNextSpace(false) - : 1; + int count = mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_ALT : MinecraftClient.IS_SYSTEM_MAC ? GLFW_MOD_SUPER : GLFW_MOD_CONTROL) + ? text.length() - cursor + : (mods == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_CONTROL : GLFW_MOD_ALT)) + ? countToNextSpace(false) + : 1; - text = text.substring(0, cursor) + text.substring(cursor + count); + text = text.substring(0, cursor) + text.substring(cursor + count); - if (!text.equals(preText)) runAction(); - } - } - else { - clearSelection(); + if (!text.equals(preText)) runAction(); } + } + else { + clearSelection(); + } return true; } else if (key == GLFW_KEY_LEFT) {