From 15bb906df73ec7b66113ca6633f9f870e425af97 Mon Sep 17 00:00:00 2001 From: Vanic Date: Fri, 13 Feb 2026 21:03:01 +0300 Subject: [PATCH] `HelperLib`: add `getObjectByGUID` & immersive craft data methods --- mods/SYSTEM/HelperLib/src/Lib.lua | 2 + .../HelperLib/src/Lib/getObjectByGUID.lua | 16 ++++ .../HelperLib/src/Lib/immersiveCraftData.lua | 92 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua create mode 100644 mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua diff --git a/mods/SYSTEM/HelperLib/src/Lib.lua b/mods/SYSTEM/HelperLib/src/Lib.lua index 526150e..4cdfbb1 100644 --- a/mods/SYSTEM/HelperLib/src/Lib.lua +++ b/mods/SYSTEM/HelperLib/src/Lib.lua @@ -5,3 +5,5 @@ local require = modInfo.require require('Lib.globalStep') require('Lib.color') +require('Lib.immersiveCraftData') +require('Lib.getObjectByGUID') \ No newline at end of file diff --git a/mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua b/mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua new file mode 100644 index 0000000..be359f2 --- /dev/null +++ b/mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua @@ -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 diff --git a/mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua b/mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua new file mode 100644 index 0000000..ca38113 --- /dev/null +++ b/mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua @@ -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