HelperLib: add getObjectByGUID & immersive craft data methods

This commit is contained in:
Van
2026-02-13 21:03:01 +03:00
parent e9818fba24
commit 15bb906df7
3 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
local EMPTY_FLAG = 'EMPTY_FLAG'
---@class ImmersiveStageData
---@class ImmersiveCraftData
---@field craftName string,
---@field stageName string,
---@field stageData ImmersiveStageData?
---Returns craft data for immersive crafting, which is stored in the desktop metadata.
---@param meta NodeMetaRef
---@return ImmersiveCraftData?
function Smc.getCraftDataByMeta(meta)
local dataString = meta:get_string('craft_data')
if dataString == EMPTY_FLAG then
return
end
return core.deserialize(dataString)
end
---Sets craft data for immersive crafting that is stored in the work station metadata.
---
---If `data` is `nil`, the data is cleared.
---@param meta NodeMetaRef
---@param craftData ImmersiveCraftData?
function Smc.setCraftDataByMeta(meta, craftData)
if craftData == nil then
meta:set_string('craft_data', EMPTY_FLAG)
end
meta:set_string('craft_data', core.serialize(craftData))
end
---Returns craft data for immersive crafting, which is stored in the work station metadata.
---@param pos MapPosition
---@return ImmersiveCraftData?
function Smc.getCraftDataByPos(pos)
local meta = core.get_meta(pos)
return Smc.getCraftDataByMeta(meta)
end
---Sets craft data for immersive crafting that is stored in the work station metadata.
---
---If `data` is `nil`, the data is cleared.
---@param pos MapPosition
---@param craftData ImmersiveCraftData?
function Smc.setCraftDataByPos(pos, craftData)
local meta = core.get_meta(pos)
Smc.setCraftDataByMeta(meta, craftData)
end
---Returns stage data for immersive crafting, which is stored in the work station metadata.
---
---**Note**: If the workbench does not have crafting data, an error will be thrown.
---@param pos MapPosition
---@return ImmersiveStageData?
function Smc.getStageDataByPos(pos)
local craftData = Smc.getCraftDataByPos(pos)
assert(craftData ~= nil, 'Immersive crafting data is empty.')
return craftData.stageData
end
---Sets stage data for immersive crafting that is stored in the work station metadata.
---@param pos MapPosition
---@param stageData ImmersiveStageData
function Smc.setStageDataByPos(pos, stageData)
local craftData = Smc.getCraftDataByPos(pos)
assert(craftData ~= nil, 'Immersive crafting data is empty.')
craftData.stageData = stageData
Smc.setCraftDataByPos(pos, craftData)
end
---Checks if the workstation is busy or free for new crafting.
---
---The verification is based on `ImmersiveCraftData`.
---@param pos MapPosition
---@return boolean
function Smc.workStationIsFree(pos)
local data = Smc.getCraftDataByPos(pos)
if data == nil then
return true
else
return false
end
end