From 35de78bdec1ca4094a71c0a7da57b0c00f4f0d31 Mon Sep 17 00:00:00 2001 From: Vanic Date: Thu, 14 May 2026 22:18:54 +0300 Subject: [PATCH] `PlayerInventory`: add `updateNodeForm` method and support stuff --- .../src/PlayerInventoryUI.lua | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index c65fc0e..31eb941 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -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 +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)