Skip to content

Commit

Permalink
Fixed so amount field is saved when going in and out of window (#16)
Browse files Browse the repository at this point in the history
* Fixed so amount field is saved when going in and out of window

* Use input validation so non-numeric characters can't be typed
  • Loading branch information
Bmandk authored Aug 7, 2024
1 parent affe3f9 commit b708eb3
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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) -> {
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b708eb3

Please sign in to comment.