ImmersiveWorkStation: refactoring (assignment to NodeSuperClass)
This commit is contained in:
@@ -1,29 +1,29 @@
|
|||||||
local getInstance = Class.getInstance
|
local assert = assert
|
||||||
|
local coreNodePunch, coreNodeDig, coreChatSendPlayer, coreGetMeta, coreSwapNode = core.node_punch, core.node_dig, core.chat_send_player, core.get_meta, core.swap_node
|
||||||
|
|
||||||
local CraftSystem = Smc.requireCraftSystem()
|
local CraftSystem = Smc.requireCraftSystem()
|
||||||
|
|
||||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||||
|
|
||||||
local Node = Smc.requireNode()
|
local Node = Smc.requireNode()
|
||||||
|
|
||||||
-- Default settings that a work station node should have.
|
|
||||||
local ImmersiveWorkStationNode = Node:getExtended({
|
local ImmersiveWorkStationNode = Node:getExtended({
|
||||||
settings = {
|
settings = {
|
||||||
name = '',
|
name = '',
|
||||||
drawtype = 'normal',
|
drawtype = 'normal',
|
||||||
paramtype = 'light',
|
paramtype = 'light',
|
||||||
groups = { immersive_station = 1, dig_immediate = 2 }
|
groups = { immersive_work_station = 1, dig_immediate = 2 }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
---A work station (node) that enables player interaction
|
---A work station (node) that enables player interaction
|
||||||
---with the immersive crafting mechanics.
|
---with the immersive crafting mechanics.
|
||||||
---@class ImmersiveWorkStation
|
---@class ImmersiveWorkStation : Class, NodeSuperClass
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field craftType CraftSystem.ImmersiveCraftType
|
---@field craftType CraftSystem.ImmersiveCraftType
|
||||||
---@field handler CraftSystem.ImmersiveCraftHandler
|
---@field handler CraftSystem.ImmersiveCraftHandler
|
||||||
---@field tab PlayerInventoryUI.getTabFunc
|
---@field tab PlayerInventoryUI.getTabFunc
|
||||||
local ImmersiveWorkStation = {}
|
---@field nodeName string?
|
||||||
|
---@field activeNodeName string?
|
||||||
|
local ImmersiveWorkStation = Class:extend({})
|
||||||
|
|
||||||
---Definition table for the `ImmersiveWorkStation`.
|
---Definition table for the `ImmersiveWorkStation`.
|
||||||
---
|
---
|
||||||
@@ -32,67 +32,86 @@ local ImmersiveWorkStation = {}
|
|||||||
---@field craftType CraftSystem.ImmersiveCraftType
|
---@field craftType CraftSystem.ImmersiveCraftType
|
||||||
---@field tab PlayerInventoryUI.getTabFunc
|
---@field tab PlayerInventoryUI.getTabFunc
|
||||||
|
|
||||||
---@param name string
|
---@alias ImmersiveWorkStation.NodesDefinition {settings:Node.Settings, activeSettings:Node.Settings?, callbacks:Node.Callbacks?}
|
||||||
|
|
||||||
---@param def ImmersiveWorkStationDef
|
---@param def ImmersiveWorkStationDef
|
||||||
---@param nodeDef Node.NodeDefinition
|
---@param nodesDef ImmersiveWorkStation.NodesDefinition
|
||||||
function ImmersiveWorkStation:new(name, def, nodeDef)
|
function ImmersiveWorkStation:new(def, nodesDef)
|
||||||
local handler = CraftSystem:getImmersiveCraftHandler(def.craftType)
|
local handler = CraftSystem:getImmersiveCraftHandler(def.craftType)
|
||||||
|
|
||||||
---@type ImmersiveWorkStation
|
---@type ImmersiveWorkStation
|
||||||
local instance = getInstance(self, {})
|
local instance = self:getInstance({})
|
||||||
instance.name = name
|
|
||||||
instance.craftType = def.craftType
|
instance.craftType = def.craftType
|
||||||
instance.handler = handler
|
instance.handler = handler
|
||||||
instance.tab = def.tab
|
instance.tab = def.tab
|
||||||
|
|
||||||
if nodeDef.callbacks == nil then
|
local settings = nodesDef.settings
|
||||||
---@type Node.Callbacks
|
local activeSettings = nodesDef.activeSettings
|
||||||
nodeDef.callbacks = {}
|
local callbacks = nodesDef.callbacks or {}
|
||||||
end
|
|
||||||
local settings = nodeDef.settings
|
|
||||||
local callbacks = nodeDef.callbacks
|
|
||||||
|
|
||||||
-- Callback when the work station block is installed in the world
|
local oldCallback = callbacks.on_construct or function(...) end
|
||||||
-- Ensures initialization of necessary parameters
|
|
||||||
callbacks.on_construct = function(pos)
|
callbacks.on_construct = function(pos)
|
||||||
|
oldCallback(pos)
|
||||||
|
|
||||||
instance:init(pos)
|
instance:init(pos)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
oldCallback = callbacks.on_rightclick or function(...) end
|
||||||
-- Callback for interaction with a work station (start of crafting)
|
-- Callback for interaction with a work station (start of crafting)
|
||||||
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
|
oldCallback(pos, node, clicker, itemstack, pointed_thing)
|
||||||
|
|
||||||
instance:showForm(pos, clicker)
|
instance:showForm(pos, clicker)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
oldCallback = callbacks.on_punch or function(...) end
|
||||||
|
-- Callback for interaction with a work station (abort of crafting)
|
||||||
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
||||||
|
oldCallback(pos, node, puncher, pointed_thing)
|
||||||
|
|
||||||
instance:abortCraft(pos)
|
instance:abortCraft(pos)
|
||||||
|
coreNodePunch(pos, node, puncher, pointed_thing)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
oldCallback = callbacks.on_dig or function(...) end
|
||||||
callbacks.on_dig = function(pos, node, digger)
|
callbacks.on_dig = function(pos, node, digger)
|
||||||
---@type ImmersiveWorkStation.MetaData
|
oldCallback(pos, node, digger)
|
||||||
local meta = core.get_meta(pos)
|
|
||||||
|
|
||||||
local craftName = meta:get_string('craft_name')
|
return instance:onDig(pos, node, digger)
|
||||||
if craftName == '' then
|
|
||||||
return core.node_dig(pos, node, digger)
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Registering a node for Luanti.
|
-- Registering a nodes for Luanti.
|
||||||
ImmersiveWorkStationNode:new(nodeDef)
|
local node = ImmersiveWorkStationNode:new({ settings = settings, callbacks = callbacks })
|
||||||
|
instance.nodeName = node.itemDef.settings.name
|
||||||
|
|
||||||
|
if activeSettings ~= nil then
|
||||||
|
local activeNode = ImmersiveWorkStationNode:new({ settings = activeSettings, callbacks = callbacks })
|
||||||
|
instance.activeNodeName = activeNode.itemDef.settings.name
|
||||||
|
|
||||||
|
Smc.addNodeSuperclass(instance.activeNodeName, instance)
|
||||||
|
end
|
||||||
|
|
||||||
|
Smc.addNodeSuperclass(instance.nodeName, instance)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---A wrapper around `core.get_meta()` for convenience
|
||||||
|
---and returning the correct `MetaDataRef` type.
|
||||||
|
---@static
|
||||||
|
---@param pos MapVector
|
||||||
|
---@return ImmersiveWorkStation.MetaData
|
||||||
|
function ImmersiveWorkStation:getMeta(pos)
|
||||||
|
return coreGetMeta(pos)
|
||||||
|
end
|
||||||
|
|
||||||
function ImmersiveWorkStation:showForm(pos, player)
|
function ImmersiveWorkStation:showForm(pos, player)
|
||||||
local playerName = player:get_player_name()
|
local playerName = player:get_player_name()
|
||||||
---@type ImmersiveWorkStation.MetaData
|
local meta = self:getMeta(pos)
|
||||||
local meta = core.get_meta(pos)
|
|
||||||
|
|
||||||
local craftName = meta:get_string('craft_name')
|
local craftName = meta:get_string('craft_name')
|
||||||
if craftName ~= '' then
|
if craftName ~= '' then
|
||||||
core.chat_send_player(playerName, 'The workbench is occupied by another craft.')
|
coreChatSendPlayer(playerName, 'The workbench is occupied by another craft.')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -106,14 +125,45 @@ end
|
|||||||
|
|
||||||
---@param pos MapVector
|
---@param pos MapVector
|
||||||
function ImmersiveWorkStation:init(pos)
|
function ImmersiveWorkStation:init(pos)
|
||||||
---@type ImmersiveWorkStation.MetaData
|
local meta = self:getMeta(pos)
|
||||||
local meta = core.get_meta(pos)
|
end
|
||||||
|
|
||||||
|
---Replaces the node with active or inactive depending on the status of the craft.
|
||||||
|
---
|
||||||
|
---If an active node is not specified for a station,
|
||||||
|
---then no replacement will occur regardless of the status of the process.
|
||||||
|
---@param isCrafting boolean The stations process status. If `true`, the process is running.
|
||||||
|
---@param pos MapVector
|
||||||
|
function ImmersiveWorkStation:swapNode(isCrafting, pos)
|
||||||
|
assert(self.nodeName ~= nil)
|
||||||
|
|
||||||
|
if self.activeNodeName ~= nil then
|
||||||
|
if isCrafting then
|
||||||
|
coreSwapNode(pos, {name = self.activeNodeName})
|
||||||
|
else
|
||||||
|
coreSwapNode(pos, {name = self.nodeName})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param pos MapVector
|
||||||
|
---@param node NodeTable
|
||||||
|
---@param digger Player
|
||||||
|
---@return boolean
|
||||||
|
function ImmersiveWorkStation:onDig(pos, node, digger)
|
||||||
|
local meta = self:getMeta(pos)
|
||||||
|
|
||||||
|
local craftName = meta:get_string('craft_name')
|
||||||
|
if craftName == '' then
|
||||||
|
return coreNodeDig(pos, node, digger)
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param pos MapVector
|
---@param pos MapVector
|
||||||
function ImmersiveWorkStation:abortCraft(pos)
|
function ImmersiveWorkStation:abortCraft(pos)
|
||||||
---@type ImmersiveWorkStation.MetaData
|
local meta = self:getMeta(pos)
|
||||||
local meta = core.get_meta(pos)
|
|
||||||
|
|
||||||
local craftName = meta:get_string('craft_name')
|
local craftName = meta:get_string('craft_name')
|
||||||
if craftName == '' then
|
if craftName == '' then
|
||||||
@@ -139,7 +189,7 @@ local function tab(self, playerName, tabType)
|
|||||||
return baseGetCraftTab(self, playerName, tabType, 'node_tab', handler)
|
return baseGetCraftTab(self, playerName, tabType, 'node_tab', handler)
|
||||||
end
|
end
|
||||||
|
|
||||||
ImmersiveWorkStation:new('test_station', {
|
ImmersiveWorkStation:new({
|
||||||
craftType = 'workbench',
|
craftType = 'workbench',
|
||||||
tab = tab,
|
tab = tab,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user