HelperLib: add getObjectByGUID & immersive craft data methods
This commit is contained in:
@@ -5,3 +5,5 @@ local require = modInfo.require
|
|||||||
|
|
||||||
require('Lib.globalStep')
|
require('Lib.globalStep')
|
||||||
require('Lib.color')
|
require('Lib.color')
|
||||||
|
require('Lib.immersiveCraftData')
|
||||||
|
require('Lib.getObjectByGUID')
|
||||||
16
mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua
Normal file
16
mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
---Returns an active object by unique identifier (GUID).
|
||||||
|
---* For players, this is a player name.
|
||||||
|
---* For Lua entities, this is a uniquely generated string, guaranteed not to collide with player names.
|
||||||
|
---
|
||||||
|
---**Note**: If an object with such an identifier does not exist or is inactive, the value `nil` will be returned.
|
||||||
|
---@param guid string only use printable ASCII characters. Example: `@bGh3p2AbRE29Mb4biqX6OA`.
|
||||||
|
---@return ObjectRef|Player|Entity|nil
|
||||||
|
function Smc.getObjectByGUID(guid)
|
||||||
|
local obj = core.objects_by_guid[guid]
|
||||||
|
|
||||||
|
if obj == nil then
|
||||||
|
return
|
||||||
|
else
|
||||||
|
return obj
|
||||||
|
end
|
||||||
|
end
|
||||||
92
mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua
Normal file
92
mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua
Normal 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
|
||||||
Reference in New Issue
Block a user