diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua index c0d19f6..bff661d 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua @@ -1,5 +1,11 @@ -local modInfo = Mod.getInfo('smc__core__craft_system') -local require = modInfo.require +local v = vector.new +local assert, ipairs, tostring = assert, ipairs, tostring +local coreChatSendPlayer, coreItemDrop = core.chat_send_player, core.item_drop + +local require = Mod.getInfo('smc__core__craft_system').require + +---@type Events.ImmersiveCraftEvents +local ImmersiveCraftEvents = Smc.requireEvents('ImmersiveCraftEvents') ---@type CraftSystem.BaseCraft local BaseCraft = require('CraftSystem.BaseCraft') @@ -48,24 +54,21 @@ function ImmersiveCraft:new(name, def) instance.stagesByName = stagesByName -- Subscribe to an event that will determine that the player has completed the crafting stage. - Events:subscribe('craft_system:next_immersive_stage:'..name, - ---@param eventName string - ---@param stage CraftSystem.ImmersiveCraftStage - ---@param pos MapVector - ---@param Player Player - function(eventName, stage, pos, Player) + ImmersiveCraftEvents:subscribeOnNextStage(name, function(stage, pos, player) local nextStageName = stage.nextStage if nextStageName == nil then - instance:endCraft(pos, Player) + instance:endCraft(pos, player) else + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) local nextStage = instance.stagesByName[nextStageName] - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + local meta = workStation:getMeta(pos) meta:set_string('stage_name', nextStage.name) - nextStage:startHandler(pos, Player) + nextStage:startHandler(pos, player) end end) @@ -80,16 +83,19 @@ end ---@param amount integer ---@return boolean function ImmersiveCraft:startCraft(pos, player, amount) - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + + local meta = workStation:getMeta(pos) local craftName = meta:get_string('craft_name') if craftName ~= '' then - core.chat_send_player(player:get_player_name(), 'The workbench is occupied by another craft.') + coreChatSendPlayer(player:get_player_name(), 'The workbench is occupied by another craft.') return false end if not ImmersiveCraftCounter:addCraft(pos, player:get_player_name()) then - core.chat_send_player(player:get_player_name(), + coreChatSendPlayer(player:get_player_name(), 'The limit of simultaneously active crafts has been reached: '.. 'complete or cancel active crafts to start a new one') @@ -112,17 +118,20 @@ end ---@param pos MapVector ---@param player Player function ImmersiveCraft:endCraft(pos, player) - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + + local meta = workStation:getMeta(pos) local playerName = meta:get_string('player_name') assert(playerName ~= '') local amount = meta:get_float('craft_amount') assert(amount ~= 0) -- Drop the result of crafting. - local dropPos = vector.new(pos) + vector.new(0, 0.9,0) + local dropPos = v(pos) + v(0, 0.9,0) for i=1, amount do - core.item_drop(ItemStack(self.output), nil, dropPos) + coreItemDrop(ItemStack(self.output), nil, dropPos) end ImmersiveCraftCounter:removeCraft(pos, playerName) @@ -138,8 +147,11 @@ end ---Stop the crafting process and free up the workbench. ---@param pos MapVector function ImmersiveCraft:abortCraft(pos) - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + + local meta = workStation:getMeta(pos) local playerName = meta:get_string('player_name') assert(playerName ~= '') local stageName = meta:get_string('stage_name') diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftNode/tappingNode.lua b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftNode/tappingNode.lua index 7923391..7384b2f 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftNode/tappingNode.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftNode/tappingNode.lua @@ -1,5 +1,7 @@ -local modInfo = Mod.getInfo('smc__core__craft_system') -local require = modInfo.require +local require = Mod.getInfo('smc__core__craft_system').require + +---@type Events.ImmersiveCraftEvents +local ImmersiveCraftEvents = Smc.requireEvents('ImmersiveCraftEvents') ---@type CraftSystem.ImmersiveCraftNode local ImmersiveCraftNode = require('CraftSystem.ImmersiveCraftNode') @@ -59,8 +61,7 @@ local tappingNode = ImmersiveCraftNode:new({ ---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode on_punch = function(luaEntity, puncher, time_from_last_punch, tool_capabilities, dir, damage) -- We simply trigger the event that the player hits the crafting node, bound to the work station with coordinates. - Events:call(('craft_system:punch_tapping_node:%s:%s'):format( - luaEntity._craft_name, luaEntity._stage_name), luaEntity._work_station_pos, puncher, luaEntity) + ImmersiveCraftEvents:callOnPunchTappingNode(luaEntity._craft_name, luaEntity._stage_name, luaEntity._work_station_pos, puncher, luaEntity) return false end, diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftStage/TappingStage.lua b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftStage/TappingStage.lua index a7a09c1..3e25e99 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftStage/TappingStage.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftStage/TappingStage.lua @@ -1,7 +1,13 @@ +local v = vector.new +local assert = assert local extend = Class.extend +local coreItemDrop = core.item_drop local require = Mod.getInfo('smc__core__craft_system').require +---@type Events.ImmersiveCraftEvents +local ImmersiveCraftEvents = Smc.requireEvents('ImmersiveCraftEvents') + ---@type CraftSystem.ImmersiveCraftStage local ImmersiveCraftStage = require('CraftSystem.ImmersiveCraftStage') @@ -43,7 +49,7 @@ function TappingStage:new(def) -- Registering the crafting stage start handler. instance.startHandler = function(tappingStage , pos, player) - local entityPos = vector.new(pos) + vector.new(0,0.875,0) + local entityPos = v(pos) + v(0,0.875,0) ---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams local params = { @@ -65,16 +71,22 @@ function TappingStage:new(def) entityGUID = obj:get_guid() } - --@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + + local meta = workStation:getMeta(pos) meta:set_string('stage_data', core.serialize(stageData)) end -- Registering the crafting stage process handler. -- Called when a player clicks on a crafting node (entity). instance.processHandler = function(tappingStage, pos, player) - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + local meta = workStation:getMeta(pos) + ---@type CraftSystem.ImmersiveCraftStage.TappingStageData? local stageData = core.deserialize(meta:get_string('stage_data')) assert(stageData ~= nil) @@ -92,29 +104,35 @@ function TappingStage:new(def) -- Registering the crafting stage end handler. instance.endHandler = function(tappingStage, pos, player) - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + local meta = workStation:getMeta(pos) + ---@type CraftSystem.ImmersiveCraftStage.TappingStageData? local stageData = core.deserialize(meta:get_string('stage_data')) assert(stageData ~= nil) - local entity = Smc.getObjectByGUID(stageData.entityGUID) assert(entity ~= nil, 'The entity for immersive crafting is nil.') entity:remove() - Events:call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, player) + ImmersiveCraftEvents:callOnNextStage(tappingStage.craftName, tappingStage, pos, player) end -- Registering the aborting the crafting process handler. ---@param tappingStage CraftSystem.ImmersiveCraftStage.TappingStage instance.abortHandler = function(tappingStage, pos) - ---@type ImmersiveWorkStation.MetaData - local meta = core.get_meta(pos) + ---@type ImmersiveWorkStation? + local workStation = Smc.getNodeSuperclassByPos(pos) + assert(workStation ~= nil) + local meta = workStation:getMeta(pos) + ---@type CraftSystem.ImmersiveCraftStage.TappingStageData? local stageData = core.deserialize(meta:get_string('stage_data')) assert(stageData ~= nil) + local amount = meta:get_float('craft_amount') assert(amount ~= 0) @@ -128,9 +146,9 @@ function TappingStage:new(def) entity:remove() -- We drop the item as it was before processing. - local dropPos = vector.new(pos)+vector.new(0, 0.9,0) + local dropPos = v(pos) + v(0, 0.9,0) for i=1, amount do - core.item_drop(ItemStack(tappingStage.targetItem), nil, dropPos) + coreItemDrop(ItemStack(tappingStage.targetItem), nil, dropPos) end end @@ -147,10 +165,7 @@ function TappingStage:new(def) end -- Subscribe to an event that means the player hit the entity. - Events:subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name), - ---@param craftNode LuaEntity - ---@param player Player - function(eventName, pos, player, craftNode) + ImmersiveCraftEvents:subscribeOnPunchTappingNode(instance.craftName, instance.name, function(pos, player, craftNode) local toolDef = player:get_wielded_item():get_definition() -- If a crafting stage requires a specific tool group and he doesn't have it then we don't do anything. @@ -162,7 +177,7 @@ function TappingStage:new(def) -- Creating particles for i=0, 10 do core.add_particle({ - pos = vector.new(pos)+vector.new(0,0.5,0), + pos = v(pos) + v(0,0.5,0), velocity = {x=math.random(-1,1), y=4, z=math.random(-1,1)}, acceleration = {x=0, y=-9.8, z=0}, expirationtime = 2, @@ -172,9 +187,9 @@ function TappingStage:new(def) end -- Making the entity shake (silly method :P). - craftNode.object:set_rotation(vector.new(0.01,0.1,0.01)) - core.after(0.1, function() craftNode.object:set_rotation(vector.new(-0.01,-0.1,-0.01))end) - core.after(0.2, function() craftNode.object:set_rotation(vector.new(0,0,0))end) + craftNode.object:set_rotation(v(0.01,0.1,0.01)) + core.after(0.1, function() craftNode.object:set_rotation(v(-0.01,-0.1,-0.01))end) + core.after(0.2, function() craftNode.object:set_rotation(v(0,0,0))end) -- DRAFT END instance:processHandler(pos, player)