CraftSystem (and other): Changing the approach to the crafting system. Adding crafting types, correcting calls to the crafting system
This commit is contained in:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user