56 lines
1.4 KiB
Lua
56 lines
1.4 KiB
Lua
---@class BaseUI
|
|
---Context for transferring data between displaying
|
|
---the form spec to the player and handling callbacks.
|
|
---@field contex table<string, table>
|
|
local BaseUI = {
|
|
contex = {}
|
|
}
|
|
|
|
---Set a value for a specific key.
|
|
---Recommended for use where `core.show_formspec` is used.
|
|
---
|
|
---In the future, the key value can be obtained in the formspec callback.
|
|
---@param player string|Player Player as an object (entity) or his name
|
|
---@param key string
|
|
---@param value any
|
|
function BaseUI:setContex(player, key, value)
|
|
if type(player) == 'table' then
|
|
player = player:get_player_name()
|
|
end
|
|
|
|
local contex = self.contex[player]
|
|
|
|
if contex == nil then
|
|
self.contex[player] = {[key]=value}
|
|
else
|
|
contex[key] = value
|
|
end
|
|
end
|
|
|
|
---Get the value of a specific key.
|
|
---Recommended for use in a formspec callback.
|
|
---@param player string|Player Player as an object (entity) or his name
|
|
---@param key string
|
|
---@return any?
|
|
function BaseUI:getContex(player, key)
|
|
if type(player) == 'table' then
|
|
player = player:get_player_name()
|
|
end
|
|
|
|
local contex = self.contex[player] or {}
|
|
return contex[key]
|
|
end
|
|
|
|
---Clear saved context data for a player.
|
|
---Recommended for use when closing formspec.
|
|
---@param player string|Player Player as an object (entity) or his name
|
|
function BaseUI:clearContex(player)
|
|
if type(player) == 'table' then
|
|
player = player:get_player_name()
|
|
end
|
|
|
|
self.contex[player] = {}
|
|
end
|
|
|
|
return BaseUI
|