From b708eb3a24a5af17557b6ce8c247aa52626bea0f Mon Sep 17 00:00:00 2001 From: Bmandk Date: Wed, 7 Aug 2024 22:17:02 +0200 Subject: [PATCH] Fixed so amount field is saved when going in and out of window (#16) * Fixed so amount field is saved when going in and out of window * Use input validation so non-numeric characters can't be typed --- .../jecalculation/gui/guis/GuiCraft.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java index 58c0388b..9e5d1401 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java @@ -58,8 +58,9 @@ public class GuiCraft extends Gui { Calculator calculator = null; RecordCraft record; RecordGroupCraft groupCraft; + long currentAmount = 1; WLabel label = new WLabel(31, 7, 20, 20, true).setLsnrUpdate((i, v) -> { - v.setAmount(getCurrentAmount()); + v.setAmount(currentAmount); addLabel(v); refreshCrafts(); }); @@ -94,7 +95,19 @@ public class GuiCraft extends Gui { WButton invE = new WButtonIcon(149, 82, 20, 20, Resource.BTN_INV_E, "craft.inventory_enabled"); WButton invD = new WButtonIcon(149, 82, 20, 20, Resource.BTN_INV_D, "craft.inventory_disabled"); WTextField amount = new WTextField(60, 7, 65).setListener(i -> { - groupCraft.setAmount(0, getCurrentAmount()); + String text = i.getText(); + if (text.isEmpty()) return; + text = text.replaceAll("[^0-9]", ""); + try { + currentAmount = Long.parseLong(text); + if (currentAmount < 1) currentAmount = 1; + } catch (NumberFormatException e) { + currentAmount = 1; + } + String s = Long.toString(currentAmount); + i.setText(s); // This is not a recursive call + groupCraft.setAmount(0, currentAmount); + record.amount = s; refreshCalculator(); }); WLabelGroup craftingGroup = new WLabelGroup(7, 31, 8, 1, false).setLsnrLeftClick((i, v) -> { @@ -115,6 +128,7 @@ public GuiCraft() { record = Controller.getRCraft(); groupCraft = Controller.getRGroupCraft(); amount.setText(record.amount); + currentAmount = record.amount.isEmpty() ? 1 : Long.parseLong(record.amount); add(new WHelp("craft")); add(new WPanel(0, 0, 176, 186)); add( @@ -269,15 +283,6 @@ private void refreshCrafts() { refreshCalculator(); } - private long getCurrentAmount() { - String s = amount.getText(); - try { - return s.isEmpty() ? 1 : Long.parseLong(amount.getText()); - } catch (NumberFormatException ignored) { - return 1; - } - } - private void addLabel(ILabel l) { if (l == ILabel.EMPTY) return; record.push(l, false);