diff --git a/mods/CORE/WorkStation/src/WorkStation.lua b/mods/CORE/WorkStation/src/WorkStation.lua index 408c61d..d7ced36 100644 --- a/mods/CORE/WorkStation/src/WorkStation.lua +++ b/mods/CORE/WorkStation/src/WorkStation.lua @@ -9,15 +9,18 @@ local WorkStationUI = Mod.getInfo('smc__ui__work_station').require('WorkStationU ---@type Node local Node = Mod.getInfo('smc__core__node').require('Node') +-- Default settings that a work station node should have. local WorkStationNode = Node:getExtended({ settings = { name = 'work_station', drawtype = 'normal', paramtype = 'light', - groups = {work_station = 1} + groups = {work_station = 1, dig_immediate = 2,} } }) +---A work station (node) that enables player interaction +---with the immersive crafting mechanics. ---@class WorkStation ---@field name string ---@field group CraftSystem.CraftGroup @@ -36,36 +39,61 @@ local WorkStation = {} function WorkStation:new(name, def) local nodeDef = def.nodeDef if nodeDef.callbacks == nil then + ---@type Node.Callbacks nodeDef.callbacks = {} end local settings = nodeDef.settings local callbacks = nodeDef.callbacks + -- Callback when the work station block is installed in the world + -- Ensures initialization of necessary parameters callbacks.on_construct = function(pos) + -- Getting node metadata local meta = core.get_meta(pos) - --meta:set_string('work_station_name', name) - --meta:set_string('craft_group', def.group) + -- Initializing the table of temporary parameters for crafting. + Smc.setCraftDataByMeta(meta) end + -- Callback for interaction with a work station (start of crafting) callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) - Logger.warningLog('Punch') - local crafts = ImmersiveCraftHandler:getCraftsByTriggerItem(itemstack:get_name()) - - if crafts == nil then + -- If the workstation is occupied by another craft... + if not Smc.workStationIsFree(pos) then + -- ...we inform the player about this... + core.chat_send_player(clicker:get_player_name(), 'The workbench is occupied by another craft.') + -- ...and do nothing. return end - self:showSelectionCraftForm(clicker, crafts) + -- We get a list of crafts that can be produced using the item that the player was holding in his hand. + local crafts = ImmersiveCraftHandler:getCraftsByTriggerItem(itemstack:get_name()) + + -- If the list is empty... + if crafts == nil then + -- ..this means there are no crafts for this item, + -- so we don't do anything. + return + else + -- ...else we show the player the formspec for choosing a craft. + self:showSelectionCraftForm(clicker, crafts, pos) + end end - local instance = getInstance(self, { - name = name, - group = def.group, - nodeDef = nodeDef - }) - print('NODEDEF: ',dump(nodeDef.callbacks)) + callbacks.on_dig = function(pos, node, digger) + if Smc.workStationIsFree(pos) then + return core.node_dig(pos, node, digger) + else + return false + end + end + ---@type WorkStation + local instance = getInstance(self, {}) + instance.name = name + instance.group = def.group + instance.nodeDef = nodeDef + + -- Registering a node for Luanti. WorkStationNode:new(nodeDef) return instance @@ -73,16 +101,48 @@ end ---@param player Player ---@param crafts CraftSystem.ImmersiveCraft[] -function WorkStation:showSelectionCraftForm(player, crafts) +---@param pos Position +function WorkStation:showSelectionCraftForm(player, crafts, pos) local outputs = {} - for i, craft in ipairs(crafts) do - outputs[i] = craft.output + for _, craft in ipairs(crafts) do + outputs[craft.output] = craft.name end - core.show_formspec(player:get_player_name(), 'work_station:selection_craft', WorkStationUI:selectionCraftForm(outputs)) + local playerName = player:get_player_name() + + WorkStationUI:setContex(playerName, 'pos', pos) + WorkStationUI:setContex(playerName, 'outputs', outputs) + + core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, false)) end +WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue) + ---@type ItemStackString? + local craftName = string.match(fieldName, '(%S+)::item_button') + + if craftName == nil then + return + end + + local craft = ImmersiveCraftHandler:getCraftByName(craftName) + local playerName = player:get_player_name() + + WorkStationUI:setContex(playerName, 'craft', craft) + core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, true)) +end) + +WorkStationUI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue) + local playerName = player:get_player_name() + local pos = WorkStationUI:getContex(playerName, 'pos') + local craft = WorkStationUI:getContex(playerName, 'craft') + + craft:startCraft(pos, player) + core.close_formspec(playerName, 'work_station:selection_craft') +end) + +-- --- DEBUG --- + WorkStation:new('test_station', { group = 'test', nodeDef = { @@ -93,20 +153,52 @@ WorkStation:new('test_station', { } }) -ImmersiveCraftHandler:addCraft('bricks', { +ImmersiveCraftHandler:registerCraft('bricks', { triggerItem = 'rocks:sylite', output = 'rocks:sylite_bricks', - startStageName = 'ab', + startStageName = 'tapping', group = 'test', - stages = {}, + stages = { + ---@type CraftSystem.ImmersiveCraftStage.TappingStageDef + { + name = 'tapping', + targetItem = 'rocks:sylite', + type = 'tapping', + clicksNeed = 3, + } + }, }) -ImmersiveCraftHandler:addCraft('block', { +ImmersiveCraftHandler:registerCraft('block', { triggerItem = 'rocks:sylite', output = 'rocks:sylite_block', - startStageName = 'ab', + startStageName = 'tapping', group = 'test', - stages = {}, + stages = { + ---@type CraftSystem.ImmersiveCraftStage.TappingStageDef + { + name = 'tapping', + targetItem = 'rocks:sylite', + type = 'tapping', + clicksNeed = 5, + } + }, +}) + +ImmersiveCraftHandler:registerCraft('marentine_planks', { + triggerItem = 'wood:marentine_log', + output = 'wood:marentine_planks', + startStageName = 'tapping', + group = 'test', + stages = { + ---@type CraftSystem.ImmersiveCraftStage.TappingStageDef + { + name = 'tapping', + targetItem = 'wood:marentine_log', + type = 'tapping', + clicksNeed = 5, + } + }, }) return WorkStation