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
This commit is contained in:
Bmandk authored and GitHub committed 2024-08-07 15:17:02 -05:00
1 parent affe3f997f
commit b708eb3a24
1 file changed
+16 -11
@@ -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 class GuiCraft extends Gui {
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 @@ public class GuiCraft extends Gui {
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);