PlayerInventory: add updateNodeForm method and support stuff

This commit is contained in:
Van
2026-05-14 22:18:54 +03:00
parent 0ee21106d5
commit 35de78bdec

View File

@@ -1,6 +1,7 @@
local MAX_NODE_TAB_DISTANCE = 20
local stringMatch = string.match
local tableInsert, tableRemoveValue = table.insert, table.remove_value
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
local CraftSystem = Smc.requireCraftSystem()
@@ -256,13 +257,60 @@ end
---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any)
local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false)
-- We are overriding the `BaseUI.showForm`.
---@type table<string, string[]>
PlayerInventoryUI.ActiveNodeForms = {}
---@param nodePos MapPosition
function PlayerInventoryUI:addActiveNodeForm(nodePos, playerName)
local stringPos = core.pos_to_string(nodePos)
local playerNames = self.ActiveNodeForms[stringPos]
if playerNames == nil then
self.ActiveNodeForms[stringPos] = { playerNames }
else
tableInsert(playerNames, playerName)
end
end
---@param nodePos MapPosition
function PlayerInventoryUI:updateNodeForm(nodePos)
local stringPos = core.pos_to_string(nodePos)
local playerNames = self.ActiveNodeForms[stringPos]
if playerNames == nil then
return
end
for _, playerName in ipairs(playerNames) do
local player = core.get_player_by_name(playerName)
if player ~= nil then
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
end
end
end
---@param nodePos MapPosition
function PlayerInventoryUI:removeActiveNodeForm(nodePos, playerName)
local stringPos = core.pos_to_string(nodePos)
local playerNames = self.ActiveNodeForms[stringPos]
if playerNames == nil then
return
end
tableRemoveValue(playerNames, playerName)
end
-- We are overriding the `BaseUI.showForm` & `BaseUI.closeForm`.
local showForm = PlayerInventoryUI.showForm
local closeForm = PlayerInventoryUI.closeForm
function PlayerInventoryUI:showForm(playerName)
local player = core.get_player_by_name(playerName)
assert(player ~= nil)
local nodePos = self:getContex(playerName, 'node_tab_pos')
if nodePos ~= nil then
self:addActiveNodeForm(nodePos, playerName)
end
-- We overwrite the player's inventory definition so that the updated inventory
-- is shown to the player when they press the "open inventory" key.
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
@@ -271,10 +319,23 @@ function PlayerInventoryUI:showForm(playerName)
return showForm(self, playerName)
end
function PlayerInventoryUI:closeForm(playerName)
local nodePos = self:getContex(playerName, 'node_tab_pos')
if nodePos ~= nil then
self:removeActiveNodeForm(nodePos, playerName)
end
closeForm(PlayerInventoryUI, playerName)
end
---Clears all data on the node's inventory tab.
---@param player Player
local function closeNodeTab(player)
local playerName = player:get_player_name()
local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos')
if nodePos ~= nil then
PlayerInventoryUI:addActiveNodeForm(nodePos, playerName)
end
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', nil)
PlayerInventoryUI:setContex(playerName, 'node_tab', nil)