PlayerInventory: add updateNodeForm method and support stuff
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
local MAX_NODE_TAB_DISTANCE = 20
|
local MAX_NODE_TAB_DISTANCE = 20
|
||||||
|
|
||||||
local stringMatch = string.match
|
local stringMatch = string.match
|
||||||
|
local tableInsert, tableRemoveValue = table.insert, table.remove_value
|
||||||
|
|
||||||
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
|
||||||
local CraftSystem = Smc.requireCraftSystem()
|
local CraftSystem = Smc.requireCraftSystem()
|
||||||
@@ -256,13 +257,60 @@ end
|
|||||||
---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any)
|
---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any)
|
||||||
local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false)
|
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 showForm = PlayerInventoryUI.showForm
|
||||||
|
local closeForm = PlayerInventoryUI.closeForm
|
||||||
|
|
||||||
function PlayerInventoryUI:showForm(playerName)
|
function PlayerInventoryUI:showForm(playerName)
|
||||||
local player = core.get_player_by_name(playerName)
|
local player = core.get_player_by_name(playerName)
|
||||||
assert(player ~= nil)
|
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
|
-- We overwrite the player's inventory definition so that the updated inventory
|
||||||
-- is shown to the player when they press the "open inventory" key.
|
-- is shown to the player when they press the "open inventory" key.
|
||||||
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
|
||||||
@@ -271,10 +319,23 @@ function PlayerInventoryUI:showForm(playerName)
|
|||||||
return showForm(self, playerName)
|
return showForm(self, playerName)
|
||||||
end
|
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.
|
---Clears all data on the node's inventory tab.
|
||||||
---@param player Player
|
---@param player Player
|
||||||
local function closeNodeTab(player)
|
local function closeNodeTab(player)
|
||||||
local playerName = player:get_player_name()
|
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_pos', nil)
|
||||||
PlayerInventoryUI:setContex(playerName, 'node_tab', nil)
|
PlayerInventoryUI:setContex(playerName, 'node_tab', nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user