Add craft abort support & fixes visual tappingCraftNode
This commit is contained in:
@@ -106,7 +106,7 @@ end
|
|||||||
function ImmersiveCraft:startCraft(pos, player, amount)
|
function ImmersiveCraft:startCraft(pos, player, amount)
|
||||||
-- Adding craft to the counter.
|
-- Adding craft to the counter.
|
||||||
-- If crafting has not been added...
|
-- If crafting has not been added...
|
||||||
if not ImmersiveCraftCounter:addCraft(pos, player) then
|
if not ImmersiveCraftCounter:addCraft(pos, player:get_player_name()) then
|
||||||
-- ...then we don't let the player start a new craft.
|
-- ...then we don't let the player start a new craft.
|
||||||
core.chat_send_player(player:get_player_name(),
|
core.chat_send_player(player:get_player_name(),
|
||||||
'The limit of simultaneously active crafts has been reached:'..
|
'The limit of simultaneously active crafts has been reached:'..
|
||||||
@@ -142,9 +142,10 @@ function ImmersiveCraft:startCraft(pos, player, amount)
|
|||||||
-- Create a temporary data table for crafting.
|
-- Create a temporary data table for crafting.
|
||||||
---@type ImmersiveCraftData
|
---@type ImmersiveCraftData
|
||||||
local craftData = {
|
local craftData = {
|
||||||
craftName = self.name,
|
playerName = player:get_player_name(), -- The name of the player who started the craft.
|
||||||
stageName = stage.name,
|
craftName = self.name,
|
||||||
amount = amount,
|
stageName = stage.name,
|
||||||
|
amount = amount,
|
||||||
}
|
}
|
||||||
-- We save it to the workstation metadata.
|
-- We save it to the workstation metadata.
|
||||||
Smc.setCraftDataByPos(pos, craftData)
|
Smc.setCraftDataByPos(pos, craftData)
|
||||||
@@ -157,8 +158,8 @@ end
|
|||||||
|
|
||||||
---Complete the immersive crafting process.
|
---Complete the immersive crafting process.
|
||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
---@param player Player
|
||||||
function ImmersiveCraft:endCraft(pos, Player)
|
function ImmersiveCraft:endCraft(pos, player)
|
||||||
-- We get current data on crafting.
|
-- We get current data on crafting.
|
||||||
---@type ImmersiveCraftData?
|
---@type ImmersiveCraftData?
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
@@ -170,17 +171,16 @@ function ImmersiveCraft:endCraft(pos, Player)
|
|||||||
core.item_drop(ItemStack(self.output), nil, dropPos)
|
core.item_drop(ItemStack(self.output), nil, dropPos)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Remove craft from the counter.
|
||||||
|
ImmersiveCraftCounter:removeCraft(pos, craftData.playerName)
|
||||||
|
|
||||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||||
Smc.setCraftDataByPos(pos)
|
Smc.setCraftDataByPos(pos)
|
||||||
|
|
||||||
-- Remove craft from the counter.
|
|
||||||
ImmersiveCraftCounter:removeCraft(pos, Player)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Stop the crafting process and free up the workbench.
|
---Stop the crafting process and free up the workbench.
|
||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
function ImmersiveCraft:abortCraft(pos)
|
||||||
function ImmersiveCraft:abortCraft(pos, Player)
|
|
||||||
-- We get current data on crafting.
|
-- We get current data on crafting.
|
||||||
---@type ImmersiveCraftData?
|
---@type ImmersiveCraftData?
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
@@ -188,13 +188,13 @@ function ImmersiveCraft:abortCraft(pos, Player)
|
|||||||
|
|
||||||
-- We get the current stage and start the abort process.
|
-- We get the current stage and start the abort process.
|
||||||
local stage = self.stagesByName[craftData.stageName]
|
local stage = self.stagesByName[craftData.stageName]
|
||||||
stage:abortHandler(pos, Player)
|
stage:abortHandler(pos)
|
||||||
|
|
||||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||||
Smc.setCraftDataByPos(pos)
|
Smc.setCraftDataByPos(pos)
|
||||||
|
|
||||||
-- Remove craft from the counter.
|
-- Remove craft from the counter.
|
||||||
ImmersiveCraftCounter:removeCraft(pos, Player)
|
ImmersiveCraftCounter:removeCraft(pos, craftData.playerName)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns a human-readable description of the crafting process.
|
---Returns a human-readable description of the crafting process.
|
||||||
|
|||||||
@@ -19,11 +19,10 @@ local ImmersiveCraftCounter = {
|
|||||||
---Add craft to the counter.
|
---Add craft to the counter.
|
||||||
---
|
---
|
||||||
---Returns `false` if the counter is already full.
|
---Returns `false` if the counter is already full.
|
||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
---@param playerName string
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function ImmersiveCraftCounter:addCraft(pos, Player)
|
function ImmersiveCraftCounter:addCraft(pos, playerName)
|
||||||
local playerName = Player:get_player_name()
|
|
||||||
if self.craftsCountForPlayers[playerName] == nil then
|
if self.craftsCountForPlayers[playerName] == nil then
|
||||||
self.craftsCountForPlayers[playerName] = {}
|
self.craftsCountForPlayers[playerName] = {}
|
||||||
end
|
end
|
||||||
@@ -44,10 +43,10 @@ function ImmersiveCraftCounter:addCraft(pos, Player)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Remove craft from the counter.
|
---Remove craft from the counter.
|
||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
---@param playerName string
|
||||||
function ImmersiveCraftCounter:removeCraft(pos, Player)
|
function ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||||
local playerCrafts = self.craftsCountForPlayers[Player:get_player_name()]
|
local playerCrafts = self.craftsCountForPlayers[playerName]
|
||||||
if playerCrafts == nil then
|
if playerCrafts == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,10 +22,14 @@ local tappingNode = ImmersiveCraftNode:new({
|
|||||||
_name = 'craft_system:tapping_node',
|
_name = 'craft_system:tapping_node',
|
||||||
_add_mod_name = false,
|
_add_mod_name = false,
|
||||||
|
|
||||||
|
---@type ObjectProperties
|
||||||
initial_properties = {
|
initial_properties = {
|
||||||
visual = 'wielditem',
|
visual = 'wielditem',
|
||||||
visual_size = {x=0.4, y=0.4, z=0.4},
|
visual_size = {x=0.5, y=0.5, z=0.5},
|
||||||
--static_save = false,
|
collisionbox = {
|
||||||
|
-0.375, -0.375, -0.375,
|
||||||
|
0.375, 0.375, 0.375
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
_item_name = '',
|
_item_name = '',
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ local getInstance = Class.getInstance
|
|||||||
---@field name string
|
---@field name string
|
||||||
---@field type CraftSystem.ImmersiveCraftStage.Type
|
---@field type CraftSystem.ImmersiveCraftStage.Type
|
||||||
---@field craftName string
|
---@field craftName string
|
||||||
---@field startHandler fun(self, pos:Position, Player:Player)
|
---@field startHandler fun(self, pos:Position, player:Player)
|
||||||
---@field processHandler fun(self, pos:Position, Player:Player)
|
---@field processHandler fun(self, pos:Position, player:Player)
|
||||||
---@field endHandler fun(self, pos:Position, Player:Player)
|
---@field endHandler fun(self, pos:Position, player:Player)
|
||||||
---@field abortHandler fun(self, pos:Position, Player:Player)
|
---@field abortHandler fun(self, pos:Position)
|
||||||
---@field toString fun(self):string
|
---@field toString fun(self):string
|
||||||
---@field nextStage string?
|
---@field nextStage string?
|
||||||
---@field returnItems ItemStackString[]?
|
---@field returnItems ItemStackString[]?
|
||||||
|
|||||||
@@ -41,9 +41,9 @@ function TappingStage:new(def)
|
|||||||
instance.clicksNeed = def.clicksNeed
|
instance.clicksNeed = def.clicksNeed
|
||||||
|
|
||||||
-- Registering the crafting stage start handler.
|
-- Registering the crafting stage start handler.
|
||||||
instance.startHandler = function(tappingStage , pos, Player)
|
instance.startHandler = function(tappingStage , pos, player)
|
||||||
-- We determine the coordinate where we will create the crafting node (entity).
|
-- We determine the coordinate where we will create the crafting node (entity).
|
||||||
local entityPos = vector.new(pos) + vector.new(0,0.8,0)
|
local entityPos = vector.new(pos) + vector.new(0,0.875,0)
|
||||||
|
|
||||||
-- We create a table of initial parameters that we will pass to the entity.
|
-- We create a table of initial parameters that we will pass to the entity.
|
||||||
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
||||||
@@ -75,7 +75,7 @@ function TappingStage:new(def)
|
|||||||
|
|
||||||
-- Registering the crafting stage process handler.
|
-- Registering the crafting stage process handler.
|
||||||
-- Called when a player clicks on a crafting node (entity).
|
-- Called when a player clicks on a crafting node (entity).
|
||||||
instance.processHandler = function(tappingStage, pos, Player)
|
instance.processHandler = function(tappingStage, pos, player)
|
||||||
-- Obtaining stage time data from work station metadata.
|
-- Obtaining stage time data from work station metadata.
|
||||||
---@type ImmersiveStageData.TappingStageData?
|
---@type ImmersiveStageData.TappingStageData?
|
||||||
local stageData = getStageDataByPos(pos)
|
local stageData = getStageDataByPos(pos)
|
||||||
@@ -88,7 +88,7 @@ function TappingStage:new(def)
|
|||||||
-- If the number of clicks is greater than required to complete the step...
|
-- If the number of clicks is greater than required to complete the step...
|
||||||
if clickCount >= tappingStage.clicksNeed then
|
if clickCount >= tappingStage.clicksNeed then
|
||||||
-- ...we are completing the stage.
|
-- ...we are completing the stage.
|
||||||
tappingStage:endHandler(pos, Player)
|
tappingStage:endHandler(pos, player)
|
||||||
else
|
else
|
||||||
-- ...else we save the number of clicks in the temporary data table.
|
-- ...else we save the number of clicks in the temporary data table.
|
||||||
stageData.clickCount = clickCount
|
stageData.clickCount = clickCount
|
||||||
@@ -97,7 +97,7 @@ function TappingStage:new(def)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Registering the crafting stage end handler.
|
-- Registering the crafting stage end handler.
|
||||||
instance.endHandler = function(tappingStage, pos, Player)
|
instance.endHandler = function(tappingStage, pos, player)
|
||||||
-- Obtaining stage time data from work station metadata.
|
-- Obtaining stage time data from work station metadata.
|
||||||
---@type ImmersiveStageData.TappingStageData?
|
---@type ImmersiveStageData.TappingStageData?
|
||||||
local stageData = getStageDataByPos(pos)
|
local stageData = getStageDataByPos(pos)
|
||||||
@@ -111,12 +111,12 @@ function TappingStage:new(def)
|
|||||||
entity:remove()
|
entity:remove()
|
||||||
|
|
||||||
-- We trigger an event that the crafting stage has been completed.
|
-- We trigger an event that the crafting stage has been completed.
|
||||||
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, Player)
|
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, player)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Registering the aborting the crafting process handler.
|
-- Registering the aborting the crafting process handler.
|
||||||
---@param tappingStage CraftSystem.ImmersiveCraftStage.TappingStage
|
---@param tappingStage CraftSystem.ImmersiveCraftStage.TappingStage
|
||||||
instance.abortHandler = function(tappingStage, pos, Player)
|
instance.abortHandler = function(tappingStage, pos)
|
||||||
-- We receive current data on crafting.
|
-- We receive current data on crafting.
|
||||||
---@type ImmersiveCraftData?
|
---@type ImmersiveCraftData?
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
@@ -151,7 +151,7 @@ function TappingStage:new(def)
|
|||||||
-- Subscribe to an event that means the player hit the entity.
|
-- 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),
|
Events.subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
|
||||||
---@param craftNode LuaEntity
|
---@param craftNode LuaEntity
|
||||||
function(eventName, pos, Player, craftNode)
|
function(eventName, pos, player, craftNode)
|
||||||
-- DRAFT START:
|
-- DRAFT START:
|
||||||
-- Creating particles
|
-- Creating particles
|
||||||
for i=0, 10 do
|
for i=0, 10 do
|
||||||
@@ -172,7 +172,7 @@ function TappingStage:new(def)
|
|||||||
-- DRAFT END
|
-- DRAFT END
|
||||||
|
|
||||||
-- Calling the crafting stage process handler.
|
-- Calling the crafting stage process handler.
|
||||||
instance:processHandler(pos, Player)
|
instance:processHandler(pos, player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|||||||
@@ -79,6 +79,18 @@ function WorkStation:new(name, def)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
||||||
|
if Smc.workStationIsFree(pos) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
|
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||||
|
|
||||||
|
local craft = ImmersiveCraftHandler:getCraftByName(craftData.craftName)
|
||||||
|
craft:abortCraft(pos)
|
||||||
|
end
|
||||||
|
|
||||||
callbacks.on_dig = function(pos, node, digger)
|
callbacks.on_dig = function(pos, node, digger)
|
||||||
if Smc.workStationIsFree(pos) then
|
if Smc.workStationIsFree(pos) then
|
||||||
return core.node_dig(pos, node, digger)
|
return core.node_dig(pos, node, digger)
|
||||||
@@ -190,6 +202,22 @@ ImmersiveCraftHandler:registerCraft('bricks', {
|
|||||||
targetItem = 'rocks:sylite',
|
targetItem = 'rocks:sylite',
|
||||||
type = 'tapping',
|
type = 'tapping',
|
||||||
clicksNeed = 3,
|
clicksNeed = 3,
|
||||||
|
nextStage = 'tapping_2',
|
||||||
|
},
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping_2',
|
||||||
|
targetItem = 'rocks:sylite_block',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 5,
|
||||||
|
nextStage = 'tapping_3',
|
||||||
|
},
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping_3',
|
||||||
|
targetItem = 'rocks:sylite_bricks',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 1,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ local EMPTY_FLAG = 'EMPTY_FLAG'
|
|||||||
---@class ImmersiveStageData
|
---@class ImmersiveStageData
|
||||||
|
|
||||||
---@class ImmersiveCraftData
|
---@class ImmersiveCraftData
|
||||||
---@field craftName string
|
---@field playerName string The name of the player who started the craft.
|
||||||
---@field stageName string
|
---@field craftName string
|
||||||
---@field amount integer
|
---@field stageName string
|
||||||
---@field stageData ImmersiveStageData?
|
---@field amount integer
|
||||||
|
---@field stageData ImmersiveStageData?
|
||||||
|
|
||||||
---Returns craft data for immersive crafting, which is stored in the desktop metadata.
|
---Returns craft data for immersive crafting, which is stored in the desktop metadata.
|
||||||
---@param meta NodeMetaRef
|
---@param meta NodeMetaRef
|
||||||
|
|||||||
Reference in New Issue
Block a user