From 58a23a901ac334697398db7bd514ec96a05bae20 Mon Sep 17 00:00:00 2001 From: badgamernl Date: Wed, 2 Sep 2020 16:03:31 +0200 Subject: [PATCH] Fix amount textbox validation & event --- locale/en/gui.cfg | 3 ++- modules/gui/autofill.lua | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index ed99cd97..aac03bcf 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -89,8 +89,9 @@ toggle-section-collapse-tooltip=Collapse Section toggle-entity-tooltip=Toggle the autofill of __1__ entity (overwrites autofill settings of the entity) toggle-tooltip=Toggle the autofill of __1__ into the __2__ slot of entities amount-tooltip=Amount of items it will try and put into the __1__ slot of __2__ -confirmed=Set autofill amount to __1__ __2__ for __3__ +invalid=Set autofill amount to the maximum __1__ __2__ for __3__ filled=Autofilled the __1__ with __2__ __3__ + [warp-list] main-caption=Warp List main-tooltip=Warp List; Must be within __1__ tiles to use diff --git a/modules/gui/autofill.lua b/modules/gui/autofill.lua index 7e6dcf03..cfbda6fe 100644 --- a/modules/gui/autofill.lua +++ b/modules/gui/autofill.lua @@ -157,7 +157,10 @@ Gui.element(function(event_trigger, parent, item) type = 'textfield', text = item.amount, tooltip = {'autofill.amount-tooltip', item.category, rich_img('item', item.entity) }, - clear_and_focus_on_right_click = true + clear_and_focus_on_right_click = true, + numeric = true, + allow_decimal = false, + allow_negative = false } end) :style{ @@ -165,7 +168,10 @@ end) height = 31, padding = -2 } -:on_confirmed(function(player, element, _) +:on_text_changed(function(player, element, _) + local value = tonumber(element.text) + if not value then value = 0 end + local clamped = math.clamp(value, 0, 1000) local item_name = string.sub(element.parent.name, 1 + string.len('toggle-setting-'), -1) local entity_name = element.parent.parent.parent.parent.name if not autofill_player_settings[player.name] then return end @@ -173,8 +179,12 @@ end) if not setting then return end local item = setting.items[item_name] if not item then return end - item.amount = tonumber(element.text) - player.print({'autofill.confirmed', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) + item.amount = clamped + if clamped ~= value then + element.text = clamped + player.print({'autofill.invalid', item.amount, rich_img('item', item.name), rich_img('entity', entity_name) }) + return + end end) --- Autofill setting, contains a button and a textbox