diff --git a/mods/CORE/CraftSystem/src/CraftSystem.lua b/mods/CORE/CraftSystem/src/CraftSystem.lua index 6358de6..528f16b 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem.lua @@ -10,27 +10,108 @@ local ProcessingCraftHandler= require('CraftSystem.ProcessingCraftHandler') ---@alias CraftSystem.CraftGroup string ---@alias CraftSystem.CraftRecipe string[] ----@alias CraftSystem.CraftType 'primitive'|'immersive'|'processing' ---@class CraftSystem -local CraftSystem = {} +---@field PrimitiveCraftHandlers table +---@field ImmersiveCraftHandlers table +---@field ProcessingCraftHandlers table +local CraftSystem = { + PrimitiveCraftHandlers = {}, + ImmersiveCraftHandlers = {}, + ProcessingCraftHandlers = {} +} + + +--- Craft Types --- + +---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory' +---@alias CraftSystem.ImmersiveCraftType 'workbench' +---@alias CraftSystem.ProcessingCraftType '' + +---@param type CraftSystem.PrimitiveCraftType +local function registerPrimitiveCraftType(type) + assert(CraftSystem.PrimitiveCraftHandlers[type] == nil, ('A primitive craft type `%s` already exists.'):format(type)) + + local handler = PrimitiveCraftHandler:new() + CraftSystem.PrimitiveCraftHandlers[type] = handler +end + +---@param type CraftSystem.ImmersiveCraftType +local function registerImmersiveCraftType(type) + assert(CraftSystem.ImmersiveCraftHandlers[type] == nil, ('A immersive craft type `%s` already exists.'):format(type)) + + local handler = ImmersiveCraftHandler:new() + CraftSystem.ImmersiveCraftHandlers[type] = handler +end + +---@param type CraftSystem.ProcessingCraftType +local function registerProcessingCraftType(type) + assert(CraftSystem.ProcessingCraftHandlers[type] == nil, ('A processing craft type `%s` already exists.'):format(type)) + + local handler = ProcessingCraftHandler:new() + CraftSystem.ProcessingCraftHandlers[type] = handler +end + +---@param type CraftSystem.PrimitiveCraftType +---@return CraftSystem.PrimitiveCraftHandler +function CraftSystem:getPrimitiveCraftHandler(type) + local handler = self.PrimitiveCraftHandlers[type] + assert(handler ~= nil) + + return handler +end + +---@param type CraftSystem.ImmersiveCraftType +---@return CraftSystem.ImmersiveCraftHandler +function CraftSystem:getImmersiveCraftHandler(type) + local handler = self.ImmersiveCraftHandlers[type] + assert(handler ~= nil) + + return handler +end + +---@param type CraftSystem.ProcessingCraftType +---@return CraftSystem.ProcessingCraftHandler +function CraftSystem:getProcessingCraftHandler(type) + local handler = self.ProcessingCraftHandlers[type] + assert(handler ~= nil) + + return handler +end + +--Registration of craft types: +registerPrimitiveCraftType('inside_player_inventory') +registerImmersiveCraftType('workbench') + + +--- Registration methods --- + +---@param type CraftSystem.PrimitiveCraftType ---@param craftName string ---@param def CraftSystem.PrimitiveCraftDef -function CraftSystem.registerPrimitiveCraft(craftName, def) - PrimitiveCraftHandler:registerCraft(craftName, def) +function CraftSystem:registerPrimitiveCraft(type, craftName, def) + local handler = self:getPrimitiveCraftHandler(type) + + handler:registerCraft(craftName, def) end +---@param type CraftSystem.ImmersiveCraftType ---@param craftName string ---@param def CraftSystem.ImmersiveCraftDef -function CraftSystem.registerImmersiveCraft(craftName, def) - ImmersiveCraftHandler:registerCraft(craftName, def) +function CraftSystem:registerImmersiveCraft(type, craftName, def) + local handler = self:getImmersiveCraftHandler(type) + + handler:registerCraft(craftName, def) end +---@param type CraftSystem.ProcessingCraftType ---@param craftName string ---@param def CraftSystem.ProcessingCraftDef -function CraftSystem.registerProcessingCraft(craftName, def) - ProcessingCraftHandler:registerCraft(craftName, def) +function CraftSystem:registerProcessingCraft(type, craftName, def) + local handler = self:getProcessingCraftHandler(type) + + handler:registerCraft(craftName, def) end return CraftSystem diff --git a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua index f6133ad..3fca948 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + local tableInsert = table.insert local stringMatch = string.match @@ -20,6 +22,13 @@ local BaseCraftHandler = { learnedGroupedCrafts = {}, } +---@return CraftSystem.BaseCraftHandler +function BaseCraftHandler:new() + local instance = getInstance(self, {}) + + return instance +end + ---@param craftName string ---@return CraftSystem.BaseCraft function BaseCraftHandler:getCraftByName(craftName) diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua similarity index 100% rename from mods/CORE/CraftSystem/src/CraftSystem/ProcessingHandler.lua rename to mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua diff --git a/mods/CORE/WorkStation/src/WorkStation.lua b/mods/CORE/WorkStation/src/WorkStation.lua index 59cfc85..8882377 100644 --- a/mods/CORE/WorkStation/src/WorkStation.lua +++ b/mods/CORE/WorkStation/src/WorkStation.lua @@ -1,10 +1,6 @@ local getInstance = Class.getInstance ----@type CraftSystem.ImmersiveCraftHandler -local ImmersiveCraftHandler = Mod.getInfo('smc__core__craft_system').require('CraftSystem.ImmersiveCraftHandler') - ----@type WorkStationUI -local WorkStationUI = Mod.getInfo('smc__ui__work_station').require('WorkStationUI') +local CraftSystem = Smc.requireCraftSystem() local Node = Smc.requireNode() @@ -21,22 +17,34 @@ local WorkStationNode = Node:getExtended({ ---A work station (node) that enables player interaction ---with the immersive crafting mechanics. ---@class WorkStation ----@field name string ----@field group CraftSystem.CraftGroup ----@field nodeDef Node.NodeDefinition +---@field name string +---@field craftType CraftSystem.ImmersiveCraftType +---@field handler CraftSystem.ImmersiveCraftHandler +---@field UI BaseUI local WorkStation = {} ---Definition table for the `WorkStation`. --- ---**Only for EmmyLua.** ---@class WorkStationDef ----@field group CraftSystem.CraftGroup ----@field nodeDef Node.NodeDefinition +---@field craftType CraftSystem.ImmersiveCraftType +---@field UI BaseUI ----@param name string ----@param def WorkStationDef -function WorkStation:new(name, def) - local nodeDef = def.nodeDef +---@param name string +---@param def WorkStationDef +---@param nodeDef Node.NodeDefinition +function WorkStation:new(name, def, nodeDef) + local handler = CraftSystem:getImmersiveCraftHandler(def.craftType) + local UI = def.UI + + ---@type WorkStation + local instance = getInstance(self, {}) + instance.name = name + instance.handler = handler + instance.craftType = def.craftType + instance.UI = UI + + local nodeDef = nodeDef if nodeDef.callbacks == nil then ---@type Node.Callbacks nodeDef.callbacks = {} @@ -65,7 +73,7 @@ function WorkStation:new(name, def) end -- 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()) + local crafts = handler:getCraftsByTriggerItem(itemstack:get_name()) -- If the list is empty... if crafts == nil then @@ -74,7 +82,7 @@ function WorkStation:new(name, def) return else -- ...else we show the player the formspec for choosing a craft. - self:showSelectionCraftForm(clicker, crafts, pos) + instance:showSelectionCraftForm(clicker, crafts, pos) end end @@ -86,7 +94,7 @@ function WorkStation:new(name, def) local craftData = Smc.getCraftDataByPos(pos) assert(craftData ~= nil, 'Immersive crafting data is empty.') - local craft = ImmersiveCraftHandler:getCraftByName(craftData.craftName) + local craft = handler:getCraftByName(craftData.craftName) craft:abortCraft(pos) end @@ -98,11 +106,50 @@ function WorkStation:new(name, def) end end - ---@type WorkStation - local instance = getInstance(self, {}) - instance.name = name - instance.group = def.group - instance.nodeDef = nodeDef + -- Registering a callback for player choice of craft (this does not start crafting). + UI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue) + -- We get the name of the craft from the name of the button the player pressed. + local craftName = string.match(fieldName, '(%S+)::item_button') + assert(craftName ~= nil, 'Craft name cannot be empty') + + -- We get a craft object. + local craft = handler:getCraftByName(craftName) + -- We get the name of the player who wants to start crafting. + local playerName = player:get_player_name() + + -- We enter into the context the craft that the player has chosen. + UI:setContex(playerName, 'craft', craft) + -- We show the player a new interface with a crafting description tab. + UI:showForm(playerName, true) + end) + + -- We register a callback when the player starts crafting. + UI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue) + -- We get the name of the player who wants to start crafting. + local playerName = player:get_player_name() + + -- We get the workstation position and craft from the context. + local pos = UI:getContex(playerName, 'pos') + ---@type CraftSystem.ImmersiveCraft + local craft = UI:getContex(playerName, 'craft') + + + -- We process the amount of crafting. + local isCraftStart = false + if fieldValue == 'x1' then + -- Single craft + isCraftStart = craft:startCraft(pos, player, 1) + elseif fieldValue == 'x10' then + -- Ten crafts at a time. + isCraftStart = craft:startCraft(pos, player, 10) + end + + -- If the craft was started... + if isCraftStart then + -- ...we are closing the formspec. + UI:closeForm(playerName) + end + end) -- Registering a node for Luanti. WorkStationNode:new(nodeDef) @@ -124,71 +171,35 @@ function WorkStation:showSelectionCraftForm(player, crafts, pos) -- We get the name of the player who wants to start crafting. local playerName = player:get_player_name() + local UI = self.UI -- We set the context for transferring data about the workstation coordinates and the list of crafts. - WorkStationUI:setContex(playerName, 'pos', pos) - WorkStationUI:setContex(playerName, 'outputs', outputs) + UI:setContex(playerName, 'pos', pos) + UI:setContex(playerName, 'outputs', outputs) -- Showing the desktop interface to the player. - WorkStationUI:showForm(playerName,false) + UI:showForm(playerName,false) end --- Registering a callback for player choice of craft (this does not start crafting). -WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue) - -- We get the name of the craft from the name of the button the player pressed. - local craftName = string.match(fieldName, '(%S+)::item_button') - assert(craftName ~= nil, 'Craft name cannot be empty') - -- We get a craft object. - local craft = ImmersiveCraftHandler:getCraftByName(craftName) - -- We get the name of the player who wants to start crafting. - local playerName = player:get_player_name() - - -- We enter into the context the craft that the player has chosen. - WorkStationUI:setContex(playerName, 'craft', craft) - -- We show the player a new interface with a crafting description tab. - WorkStationUI:showForm(playerName, true) -end) - --- We register a callback when the player starts crafting. -WorkStationUI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue) - -- We get the name of the player who wants to start crafting. - local playerName = player:get_player_name() - - -- We get the workstation position and craft from the context. - local pos = WorkStationUI:getContex(playerName, 'pos') - ---@type CraftSystem.ImmersiveCraft - local craft = WorkStationUI:getContex(playerName, 'craft') - - - -- We process the amount of crafting. - local isCraftStart = false - if fieldValue == 'x1' then - -- Single craft - isCraftStart = craft:startCraft(pos, player, 1) - elseif fieldValue == 'x10' then - -- Ten crafts at a time. - isCraftStart = craft:startCraft(pos, player, 10) - end - - -- If the craft was started... - if isCraftStart then - -- ...we are closing the formspec. - WorkStationUI:closeForm(playerName) - end -end) -- --- DEBUG --- +---@type WorkStationUI +local WorkStationUI = Mod.getInfo('smc__ui__work_station').require('WorkStationUI') +local ImmersiveCraftHandler = CraftSystem:getImmersiveCraftHandler('workbench') + WorkStation:new('test_station', { - group = 'test', - nodeDef = { + craftType = 'workbench', + UI = WorkStationUI + }, + { settings = { name = 'test_station', tiles = {'test_station.png'} } } -}) +) ImmersiveCraftHandler:registerCraft('bricks', { triggerItem = 'rocks:sylite', diff --git a/mods/GAME/ITEMS/rocks/init.lua b/mods/GAME/ITEMS/rocks/init.lua index cc65b1d..4c83c71 100644 --- a/mods/GAME/ITEMS/rocks/init.lua +++ b/mods/GAME/ITEMS/rocks/init.lua @@ -1,6 +1,7 @@ local Node, TileSheet = Smc.requireNode(), Smc.requireTileSheet() +local CraftSystem = Smc.requireCraftSystem() -local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() +local InventoryCraftHandler = CraftSystem:getPrimitiveCraftHandler('inside_player_inventory') ---@type TileSheet local rocksTileSheet = TileSheet:new("rocks_sheet.png", 16, 16) @@ -9,13 +10,13 @@ local rocksTileSheet = TileSheet:new("rocks_sheet.png", 16, 16) ---@field rarity Item.Rarity? local function registerSegmentsCrafts(name, craftGroup) - PrimitiveCraftHandler:registerCraft(name..'_slab', { + InventoryCraftHandler:registerCraft(name..'_slab', { recipe = {name}, output = name..'_slab', group = craftGroup }) - PrimitiveCraftHandler:registerCraft(name..'_stair', { + InventoryCraftHandler:registerCraft(name..'_stair', { recipe = {name}, output = name..'_stair', group = craftGroup @@ -88,7 +89,7 @@ local function registerRock(name, y, def, staff) registerSegmentsCrafts(modPrefix..blockName, craftGroup) - PrimitiveCraftHandler:registerCraft(blockName, { + InventoryCraftHandler:registerCraft(blockName, { recipe = {modPrefix..name}, output = modPrefix..blockName, group = craftGroup, @@ -110,7 +111,7 @@ local function registerRock(name, y, def, staff) registerSegmentsCrafts(modPrefix..bricksName, craftGroup) - PrimitiveCraftHandler:registerCraft(bricksName, { + InventoryCraftHandler:registerCraft(bricksName, { recipe = {modPrefix..name}, output = modPrefix..bricksName, group = craftGroup, diff --git a/mods/GAME/ITEMS/storages/init.lua b/mods/GAME/ITEMS/storages/init.lua index 2a36041..b627fa4 100644 --- a/mods/GAME/ITEMS/storages/init.lua +++ b/mods/GAME/ITEMS/storages/init.lua @@ -96,12 +96,3 @@ storageFactory:registerItems({ }, }, }) - -local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() - -PrimitiveCraftHandler:registerCraft('storages:case_closed', { - recipe = { - 'group:log 4' - }, - output = 'storages:case_closed', -}) \ No newline at end of file diff --git a/mods/GAME/ITEMS/wood/init.lua b/mods/GAME/ITEMS/wood/init.lua index 5d8f9f2..c2cd7df 100644 --- a/mods/GAME/ITEMS/wood/init.lua +++ b/mods/GAME/ITEMS/wood/init.lua @@ -1,6 +1,8 @@ local Item, Node, TileSheet = Smc.requireItem(), Smc.requireNode(), Smc.requireTileSheet() -local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() +local CraftSystem = Smc.requireCraftSystem() + +local InventoryCraftHandler = CraftSystem:getPrimitiveCraftHandler('inside_player_inventory') ---@type Node.NodeDefinition local defaultLogDef = { @@ -253,7 +255,7 @@ local function registerWood(name, x, def) } }) - PrimitiveCraftHandler:registerCraft('wood:'..name..'_planks', { + InventoryCraftHandler:registerCraft('wood:'..name..'_planks', { recipe = { 'wood:'..name..'_log' }, @@ -261,7 +263,7 @@ local function registerWood(name, x, def) group = 'wood:planks_blocks', }) - PrimitiveCraftHandler:registerCraft('wood:'..name..'_log_stripped', { + InventoryCraftHandler:registerCraft('wood:'..name..'_log_stripped', { recipe = { 'wood:'..name..'_log' }, @@ -269,7 +271,7 @@ local function registerWood(name, x, def) group = 'wood:logs_stripped', }) - PrimitiveCraftHandler:registerCraft('wood:'..name..'_bar', { + InventoryCraftHandler:registerCraft('wood:'..name..'_bar', { recipe = { 'wood:'..name..'_log' }, @@ -277,7 +279,7 @@ local function registerWood(name, x, def) group = 'wood:bars', }) - PrimitiveCraftHandler:registerCraft('wood:'..name..'_plank', { + InventoryCraftHandler:registerCraft('wood:'..name..'_plank', { recipe = { 'wood:'..name..'_bar' }, @@ -285,7 +287,7 @@ local function registerWood(name, x, def) group = 'wood:planks', }) - PrimitiveCraftHandler:registerCraft('wood:'..name..'_stick', { + InventoryCraftHandler:registerCraft('wood:'..name..'_stick', { recipe = { 'wood:'..name..'_bar' }, diff --git a/mods/SYSTEM/HelperLib/src/Lib/requires.lua b/mods/SYSTEM/HelperLib/src/Lib/requires.lua index 635f374..ab1619a 100644 --- a/mods/SYSTEM/HelperLib/src/Lib/requires.lua +++ b/mods/SYSTEM/HelperLib/src/Lib/requires.lua @@ -71,19 +71,3 @@ function Smc.requireCraftSystem() return class end - ----@return CraftSystem.PrimitiveCraftHandler -function Smc.requireCraftSystemPrimitiveCraftHandler() - ---@type CraftSystem.PrimitiveCraftHandler - local class = Mod.getInfo('smc__core__craft_system').require('CraftSystem.PrimitiveCraftHandler') - - return class -end - ----@return CraftSystem.ImmersiveCraftHandler -function Smc.requireCraftSystemImmersiveCraftHandler() - ---@type CraftSystem.ImmersiveCraftHandler - local class = Mod.getInfo('smc__core__craft_system').require('CraftSystem.ImmersiveCraftHandler') - - return class -end diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index ac909b6..e8df51d 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -1,8 +1,9 @@ local stringMatch = string.match local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec() +local CraftSystem = Smc.requireCraftSystem() -local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() +local InventoryCraftHandler = CraftSystem:getPrimitiveCraftHandler('inside_player_inventory') local require = Mod.getInfo('smc__ui__player_inventory').require @@ -413,7 +414,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play -- We get the recipe selected in the crafting menu. local craftName = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_craft') local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0 - local craft = PrimitiveCraftHandler:getCraftByName(craftName) + local craft = InventoryCraftHandler:getCraftByName(craftName) -- We create an ItemStack of the crafted item by multiplying -- its count by a multiplier - the number of items the player wants to craft at once. diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua index af1d3af..32e00ca 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua @@ -3,9 +3,10 @@ local mathFloor = math.floor ---Number of crafting recipes on one menu page. local PAGE_NUMBER_ITEMS = 20 -local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() - local Formspec = Smc.requireFormspec() +local CraftSystem = Smc.requireCraftSystem() + +local InventoryCraftHandler = CraftSystem:getPrimitiveCraftHandler('inside_player_inventory') ---A text indicator of the presence or absence ---of a material in the player's inventory. @@ -92,13 +93,13 @@ local function getCraftTab(self, playerName, tabType) local crafts if searchFieldValue ~= '' and searchFieldValue ~= nil then ---@type CraftSystem.PrimitiveCraft[] - crafts = PrimitiveCraftHandler:getLearnedCraftsMatch(playerName, searchFieldValue) + crafts = InventoryCraftHandler:getLearnedCraftsMatch(playerName, searchFieldValue) elseif selectedGroup == nil then ---@type (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] - crafts = PrimitiveCraftHandler:getLearnedCrafts(playerName) + crafts = InventoryCraftHandler:getLearnedCrafts(playerName) else ---@type CraftSystem.PrimitiveCraft[] - crafts = PrimitiveCraftHandler:getCraftsByGroup(selectedGroup) + crafts = InventoryCraftHandler:getCraftsByGroup(selectedGroup) end local craftsListForm = Formspec:new() @@ -171,7 +172,7 @@ local function getCraftTab(self, playerName, tabType) :label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax)) if selectedCraft ~= '' then - local craft = PrimitiveCraftHandler:getCraftByName(selectedCraft) + local craft = InventoryCraftHandler:getCraftByName(selectedCraft) local item = ItemStack(craft.output) local itemDef = item:get_definition()