BaseUI: Clearing the context is now an optional parameter & edit /list command. close #33

This commit is contained in:
Van
2026-03-02 19:54:22 +03:00
parent 1a6086190e
commit aee1246aaa
3 changed files with 26 additions and 12 deletions

View File

@@ -10,18 +10,26 @@ local BaseUI = {
contex = {} contex = {}
} }
---@param name string ---@param name string The name of the formspec that will be shown to the player. It is recommended that the name be unique.
---@param getFormSpecString fun(self:BaseUI,playerName:string,...):string ---@param getFormSpecString fun(self:BaseUI,playerName:string,...):string
function BaseUI:new(name, getFormSpecString) ---@param clearContex boolean? Whether to clear the context stored for a player when that player closes the form. Default `true`.
function BaseUI:new(name, getFormSpecString, clearContex)
if clearContex == nil then
clearContex = true
end
---@type BaseUI ---@type BaseUI
local instance = getInstance(self, {}) local instance = getInstance(self, {})
instance.name = name instance.name = name
instance.getFormSpecString = getFormSpecString instance.getFormSpecString = getFormSpecString
-- Ensure that when the form is closed, the context is cleared. if clearContex then
instance:registerOnFormQuit(function(player) core.log(name)
instance:clearContex(player) -- Ensure that when the form is closed, the context is cleared.
end) instance:registerOnFormQuit(function(player)
instance:clearContex(player)
end)
end
return instance return instance
end end

View File

@@ -10,7 +10,7 @@ core.register_chatcommand('list', {
func = function(name, param) func = function(name, param)
local player = core.get_player_by_name(name) local player = core.get_player_by_name(name)
AdminListUI:showForm(name, false) AdminListUI:showForm(name)
return true, '' return true, ''
end end
@@ -27,7 +27,7 @@ AdminListUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, f
local playerName = player:get_player_name() local playerName = player:get_player_name()
AdminListUI:setContex(playerName, 'select_item', stack) AdminListUI:setContex(playerName, 'select_item', stack)
AdminListUI:showForm(playerName, true) AdminListUI:showForm(playerName)
end) end)
AdminListUI:registerOnFormFieldAction('admin_commands:list_add_item', function(player, fieldName, fieldValue) AdminListUI:registerOnFormFieldAction('admin_commands:list_add_item', function(player, fieldName, fieldValue)
@@ -47,8 +47,8 @@ end)
core.register_on_joinplayer(function(player, last_login) core.register_on_joinplayer(function(player, last_login)
player:set_sky({ player:set_sky({
sky_color = { sky_color = {
day_sky = '#FAE6E0', day_sky = '#FCF0ED',
day_horizon = '#E7A985', day_horizon = '#F1D4C3',
}, },
type = 'regular', type = 'regular',
}) })

View File

@@ -2,9 +2,15 @@ local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
---@param self BaseUI ---@param self BaseUI
---@param playerName string ---@param playerName string
---@param isSelect boolean ---@param isSelect boolean?
---@return string ---@return string
local function getFormSpecString(self, playerName, isSelect) local function getFormSpecString(self, playerName, isSelect)
if isSelect == nil and self:getContex(playerName, 'select_item') ~= nil then
isSelect = true
else
isSelect = false
end
local items = {} local items = {}
for name, def in pairs(core.registered_items) do for name, def in pairs(core.registered_items) do
@@ -60,7 +66,7 @@ end
---@class AdminListUI : BaseUI ---@class AdminListUI : BaseUI
---@field getFormSpecString fun(self:BaseUI,playerName:string,isSelect:boolean):string ---@field getFormSpecString fun(self:BaseUI,playerName:string,isSelect:boolean):string
---@field showForm fun(self:BaseUI,playerName:string,isSelect:boolean):string ---@field showForm fun(self:BaseUI,playerName:string,isSelect:boolean):string
local AdminListUI = BaseUI:new('admin_commands:list_form', getFormSpecString) local AdminListUI = BaseUI:new('admin_commands:list_form', getFormSpecString, false)
AdminListUI:registerOnFormFieldAction('admin_commands:list_items', function(player, fieldName, fieldValue) AdminListUI:registerOnFormFieldAction('admin_commands:list_items', function(player, fieldName, fieldValue)
local scrollValue = string.match(fieldValue, 'CHG:(%S+)') local scrollValue = string.match(fieldValue, 'CHG:(%S+)')