WorkStation: tidying up the code, adding comments
This commit is contained in:
@@ -99,46 +99,71 @@ function WorkStation:new(name, def)
|
|||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Show the player the initial workstation interface for choosing a craft.
|
||||||
---@param player Player
|
---@param player Player
|
||||||
---@param crafts CraftSystem.ImmersiveCraft[]
|
---@param crafts CraftSystem.ImmersiveCraft[]
|
||||||
---@param pos Position
|
---@param pos Position
|
||||||
function WorkStation:showSelectionCraftForm(player, crafts, pos)
|
function WorkStation:showSelectionCraftForm(player, crafts, pos)
|
||||||
|
-- Create a table in the format craft result - craft name.
|
||||||
local outputs = {}
|
local outputs = {}
|
||||||
|
|
||||||
for _, craft in ipairs(crafts) do
|
for _, craft in ipairs(crafts) do
|
||||||
outputs[craft.output] = craft.name
|
outputs[craft.output] = craft.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- We get the name of the player who wants to start crafting.
|
||||||
local playerName = player:get_player_name()
|
local playerName = player:get_player_name()
|
||||||
|
|
||||||
|
-- We set the context for transferring data about the workstation coordinates and the list of crafts.
|
||||||
WorkStationUI:setContex(playerName, 'pos', pos)
|
WorkStationUI:setContex(playerName, 'pos', pos)
|
||||||
WorkStationUI:setContex(playerName, 'outputs', outputs)
|
WorkStationUI:setContex(playerName, 'outputs', outputs)
|
||||||
|
|
||||||
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, false))
|
-- Showing the desktop interface to the player.
|
||||||
|
WorkStationUI:showForm(playerName,false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Registering a callback for player choice of craft (this does not start crafting).
|
||||||
WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
|
WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
|
||||||
---@type ItemStackString?
|
-- We get the name of the craft from the name of the button the player pressed.
|
||||||
local craftName = string.match(fieldName, '(%S+)::item_button')
|
local craftName = string.match(fieldName, '(%S+)::item_button')
|
||||||
|
assert(craftName ~= nil, 'Craft name cannot be empty')
|
||||||
|
|
||||||
if craftName == nil then
|
-- We get a craft object.
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local craft = ImmersiveCraftHandler:getCraftByName(craftName)
|
local craft = ImmersiveCraftHandler:getCraftByName(craftName)
|
||||||
|
-- We get the name of the player who wants to start crafting.
|
||||||
local playerName = player:get_player_name()
|
local playerName = player:get_player_name()
|
||||||
|
|
||||||
|
-- We enter into the context the craft that the player has chosen.
|
||||||
WorkStationUI:setContex(playerName, 'craft', craft)
|
WorkStationUI:setContex(playerName, 'craft', craft)
|
||||||
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, true))
|
-- We show the player a new interface with a crafting description tab.
|
||||||
|
WorkStationUI:showForm(playerName, true)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- We register a callback when the player starts crafting.
|
||||||
WorkStationUI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue)
|
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()
|
local playerName = player:get_player_name()
|
||||||
|
|
||||||
|
-- We get the workstation position and craft from the context.
|
||||||
local pos = WorkStationUI:getContex(playerName, 'pos')
|
local pos = WorkStationUI:getContex(playerName, 'pos')
|
||||||
|
---@type CraftSystem.ImmersiveCraft
|
||||||
local craft = WorkStationUI:getContex(playerName, 'craft')
|
local craft = WorkStationUI:getContex(playerName, 'craft')
|
||||||
|
|
||||||
craft:startCraft(pos, player)
|
|
||||||
core.close_formspec(playerName, 'work_station:selection_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)
|
end)
|
||||||
|
|
||||||
-- --- DEBUG ---
|
-- --- DEBUG ---
|
||||||
|
|||||||
Reference in New Issue
Block a user