CraftSystem: reworking the code, adding something useful
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
local tableJoin = table.join
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
local require = modInfo.require
|
local require = modInfo.require
|
||||||
|
|
||||||
@@ -25,8 +27,9 @@ local CraftSystem = {
|
|||||||
|
|
||||||
--- Craft Types ---
|
--- Craft Types ---
|
||||||
|
|
||||||
|
---@alias CraftSystem.BaseCraftType 'primitive'|'immersive'|'processing'
|
||||||
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
|
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
|
||||||
---@alias CraftSystem.ImmersiveCraftType 'workbench'
|
---@alias CraftSystem.ImmersiveCraftType 'workbench' -- DEBUG
|
||||||
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
|
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
|
||||||
---@alias CraftSystem.CraftType CraftSystem.PrimitiveCraftType|CraftSystem.ImmersiveCraftType|CraftSystem.ProcessingCraftType
|
---@alias CraftSystem.CraftType CraftSystem.PrimitiveCraftType|CraftSystem.ImmersiveCraftType|CraftSystem.ProcessingCraftType
|
||||||
|
|
||||||
@@ -35,7 +38,7 @@ local CraftSystem = {
|
|||||||
local function registerPrimitiveCraftType(type, def)
|
local function registerPrimitiveCraftType(type, def)
|
||||||
assert(CraftSystem.PrimitiveCraftHandlers[type] == nil, ('A primitive craft type `%s` already exists.'):format(type))
|
assert(CraftSystem.PrimitiveCraftHandlers[type] == nil, ('A primitive craft type `%s` already exists.'):format(type))
|
||||||
|
|
||||||
local handler = PrimitiveCraftHandler:new(type, def)
|
local handler = PrimitiveCraftHandler:new('primitive', type, def)
|
||||||
CraftSystem.PrimitiveCraftHandlers[type] = handler
|
CraftSystem.PrimitiveCraftHandlers[type] = handler
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -44,7 +47,7 @@ end
|
|||||||
local function registerImmersiveCraftType(type, def)
|
local function registerImmersiveCraftType(type, def)
|
||||||
assert(CraftSystem.ImmersiveCraftHandlers[type] == nil, ('A immersive craft type `%s` already exists.'):format(type))
|
assert(CraftSystem.ImmersiveCraftHandlers[type] == nil, ('A immersive craft type `%s` already exists.'):format(type))
|
||||||
|
|
||||||
local handler = ImmersiveCraftHandler:new(type, def)
|
local handler = ImmersiveCraftHandler:new('immersive', type, def)
|
||||||
CraftSystem.ImmersiveCraftHandlers[type] = handler
|
CraftSystem.ImmersiveCraftHandlers[type] = handler
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -53,7 +56,7 @@ end
|
|||||||
local function registerProcessingCraftType(type, def)
|
local function registerProcessingCraftType(type, def)
|
||||||
assert(CraftSystem.ProcessingCraftHandlers[type] == nil, ('A processing craft type `%s` already exists.'):format(type))
|
assert(CraftSystem.ProcessingCraftHandlers[type] == nil, ('A processing craft type `%s` already exists.'):format(type))
|
||||||
|
|
||||||
local handler = ProcessingCraftHandler:new(type, def)
|
local handler = ProcessingCraftHandler:new('processing', type, def)
|
||||||
CraftSystem.ProcessingCraftHandlers[type] = handler
|
CraftSystem.ProcessingCraftHandlers[type] = handler
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -120,4 +123,112 @@ function CraftSystem:registerProcessingCraft(type, craftName, def)
|
|||||||
handler:registerCraft(craftName, def)
|
handler:registerCraft(craftName, def)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Methods for performing crafting ---
|
||||||
|
|
||||||
|
|
||||||
|
---@param craft CraftSystem.BaseCraft
|
||||||
|
---@param recipeIndex integer
|
||||||
|
---@param count integer
|
||||||
|
---@return ItemStack[] materialStacks A table containing the items required for crafting.
|
||||||
|
---@return table<string, integer> materialGroups A table containing the item groups required for crafting.
|
||||||
|
local function getMaterials(craft, recipeIndex, count)
|
||||||
|
local materialStacks = {}
|
||||||
|
local materialGroups = {}
|
||||||
|
|
||||||
|
for _, itemName in ipairs(craft:getRecipe(recipeIndex)) do
|
||||||
|
local group = itemName:match('group:(%S+)')
|
||||||
|
local materialCount = itemName:match('group:%S+ (%S+)') or 1
|
||||||
|
|
||||||
|
if group == nil then
|
||||||
|
local item = ItemStack(itemName)
|
||||||
|
item:set_count(item:get_count() * count)
|
||||||
|
|
||||||
|
table.insert(materialStacks, item)
|
||||||
|
else
|
||||||
|
materialGroups[group] = (tonumber(materialCount) or 1) * count
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return materialStacks, materialGroups
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check if the player has the required items.
|
||||||
|
---@param materialStacks ItemStack[]
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@return boolean
|
||||||
|
local function checkMaterialStacks(materialStacks, inventory)
|
||||||
|
for _, item in ipairs(materialStacks) do
|
||||||
|
if not inventory:contains_item('main', item) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check whether the player has the required number of items with the required group for crafting.
|
||||||
|
---@param materialGroups table<string, integer>
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@return boolean
|
||||||
|
---@return ItemStack[] materialStacks A list of item stacks that match the required material groups.
|
||||||
|
local function checkMaterialGroups(materialGroups, inventory)
|
||||||
|
local materialStacks = {}
|
||||||
|
|
||||||
|
local mainList = inventory:get_list('main')
|
||||||
|
for group, count in pairs(materialGroups) do
|
||||||
|
for _, item in ipairs(mainList) do
|
||||||
|
if core.get_item_group(item:get_name(), group) > 0 then
|
||||||
|
local itemCount = item:get_count()
|
||||||
|
if count > itemCount then
|
||||||
|
count = count - itemCount
|
||||||
|
table.insert(materialStacks, item)
|
||||||
|
else
|
||||||
|
item:set_count(count)
|
||||||
|
table.insert(materialStacks, item)
|
||||||
|
count = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if count > 0 then
|
||||||
|
return false, {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true, materialStacks
|
||||||
|
end
|
||||||
|
|
||||||
|
---Checks whether the player can perform crafting (whether he has the required materials in his inventory).
|
||||||
|
---@param player Player
|
||||||
|
---@param craft CraftSystem.BaseCraft
|
||||||
|
---@param recipeIndex integer
|
||||||
|
---@param count integer
|
||||||
|
---@return boolean check true if craft can be produced
|
||||||
|
---@return ItemStack[] materialStacks list of itemStacks for crafting
|
||||||
|
function CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, count, flags)
|
||||||
|
local inventory = player:get_inventory()
|
||||||
|
|
||||||
|
local OutputStack = ItemStack(craft.output)
|
||||||
|
OutputStack:set_count(OutputStack:get_count() * count)
|
||||||
|
|
||||||
|
local materialStacks, materialGroups = getMaterials(craft, recipeIndex, count)
|
||||||
|
|
||||||
|
local isHaveStacks = checkMaterialStacks(materialStacks, inventory)
|
||||||
|
if not isHaveStacks then
|
||||||
|
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
|
||||||
|
return false, {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local isHaveGroups, materialGroupStacks = checkMaterialGroups(materialGroups, inventory)
|
||||||
|
if not isHaveGroups then
|
||||||
|
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
|
||||||
|
return false, {}
|
||||||
|
end
|
||||||
|
|
||||||
|
materialStacks = tableJoin(materialStacks, materialGroupStacks)
|
||||||
|
|
||||||
|
|
||||||
|
return true, materialStacks
|
||||||
|
end
|
||||||
|
|
||||||
return CraftSystem
|
return CraftSystem
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
|
---A text indicator of the presence or absence
|
||||||
|
---of a material in the player's inventory.
|
||||||
|
local CHECK = {YES = '[x]', NO = '[ ]', EMPTY = '-'}
|
||||||
|
|
||||||
---@class CraftSystem.BaseCraft : Class
|
---@class CraftSystem.BaseCraft : Class
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field recipe ItemStackString[]
|
---@field recipe ItemStackString[]
|
||||||
|
---@field altRecipes CraftSystem.CraftRecipe[]
|
||||||
---@field output ItemStackString
|
---@field output ItemStackString
|
||||||
---@field group CraftSystem.CraftGroup
|
---@field group CraftSystem.CraftGroup
|
||||||
local BaseCraft = Class:extend({})
|
local BaseCraft = Class:extend({})
|
||||||
@@ -8,6 +13,7 @@ local BaseCraft = Class:extend({})
|
|||||||
---Definition table for the `CraftSystem.BaseCraft`.
|
---Definition table for the `CraftSystem.BaseCraft`.
|
||||||
---@class CraftSystem.BaseCraftDef
|
---@class CraftSystem.BaseCraftDef
|
||||||
---@field recipe ItemStackString[]
|
---@field recipe ItemStackString[]
|
||||||
|
---@field altRecipes CraftSystem.CraftRecipe[]?
|
||||||
---@field output ItemStackString
|
---@field output ItemStackString
|
||||||
---@field group CraftSystem.CraftGroup?
|
---@field group CraftSystem.CraftGroup?
|
||||||
|
|
||||||
@@ -19,10 +25,132 @@ function BaseCraft:new(name, def)
|
|||||||
local instance = self:getInstance({})
|
local instance = self:getInstance({})
|
||||||
instance.name = name
|
instance.name = name
|
||||||
instance.recipe = def.recipe
|
instance.recipe = def.recipe
|
||||||
|
instance.altRecipes = def.altRecipes or {}
|
||||||
instance.output = def.output
|
instance.output = def.output
|
||||||
instance.group = def.group
|
instance.group = def.group
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param material ItemStack
|
||||||
|
---@param count number
|
||||||
|
---@param list ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
local function getCheckMaterialItem(material, count, list)
|
||||||
|
if list == nil then
|
||||||
|
return CHECK.EMPTY
|
||||||
|
end
|
||||||
|
|
||||||
|
local materialName = material:get_name()
|
||||||
|
|
||||||
|
for _, item in ipairs(list) do
|
||||||
|
if materialName == item:get_name() then
|
||||||
|
local itemCount = item:get_count()
|
||||||
|
if count > itemCount then
|
||||||
|
count = count - itemCount
|
||||||
|
else
|
||||||
|
return CHECK.YES
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return CHECK.NO
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param material string
|
||||||
|
---@param count number
|
||||||
|
---@param list ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
local function getCheckMaterialGroup(material, count, list)
|
||||||
|
if list == nil then
|
||||||
|
return CHECK.EMPTY
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, item in ipairs(list) do
|
||||||
|
if core.get_item_group(item:get_name(), material) > 0 then
|
||||||
|
local itemCount = item:get_count()
|
||||||
|
if count > itemCount then
|
||||||
|
count = count - itemCount
|
||||||
|
else
|
||||||
|
return CHECK.YES
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return CHECK.NO
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns the crafting recipe corresponding to the index.
|
||||||
|
---@param index integer Recipe number. If `0` is specified,
|
||||||
|
--- the primary craft (`craft.recipe`) will be used;
|
||||||
|
--- if `1` or greater, the alternative craft
|
||||||
|
--- corresponding to the index will be used (`craft.altRecipes`).
|
||||||
|
---@return CraftSystem.CraftRecipe
|
||||||
|
function BaseCraft:getRecipe(index)
|
||||||
|
local recipe
|
||||||
|
if index == 0 or self.altRecipes == nil then
|
||||||
|
recipe = self.recipe
|
||||||
|
else
|
||||||
|
if index > #self.altRecipes then
|
||||||
|
recipe = self.altRecipes[#self.altRecipes]
|
||||||
|
else
|
||||||
|
recipe = self.altRecipes[index]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(recipe ~= nil)
|
||||||
|
|
||||||
|
return recipe
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param recipeIndex integer?
|
||||||
|
---@param mainList ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
function BaseCraft:recipeToString(recipeIndex, mainList)
|
||||||
|
local recipe
|
||||||
|
if recipeIndex == nil or recipeIndex == 0 or self.altRecipes == nil then
|
||||||
|
recipe = self.recipe
|
||||||
|
else
|
||||||
|
if recipeIndex > #self.altRecipes then
|
||||||
|
recipe = self.altRecipes[#self.altRecipes]
|
||||||
|
else
|
||||||
|
recipe = self.altRecipes[recipeIndex]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(recipe ~= nil)
|
||||||
|
|
||||||
|
local recipeDesc = ''
|
||||||
|
for _, itemName in ipairs(recipe) do
|
||||||
|
local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1
|
||||||
|
|
||||||
|
if group == nil then
|
||||||
|
local recipeItem = ItemStack(itemName)
|
||||||
|
recipeDesc = recipeDesc..('%s %s x%s\n'):format(
|
||||||
|
getCheckMaterialItem(recipeItem, recipeItem:get_count(), mainList),
|
||||||
|
core.strip_colors(recipeItem:get_definition()._title),
|
||||||
|
recipeItem:get_count())
|
||||||
|
else
|
||||||
|
recipeDesc = recipeDesc..('%s any item from the group %s x%s\n'):format(
|
||||||
|
getCheckMaterialGroup(group, count, mainList),
|
||||||
|
group,
|
||||||
|
count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return recipeDesc
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param recipeIndex integer?
|
||||||
|
---@param mainList ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
function BaseCraft:toString(recipeIndex, mainList)
|
||||||
|
local outputItem = ItemStack(self.output)
|
||||||
|
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||||
|
|
||||||
|
local craftDesc = ('%s\n\nNeeded:\n%s'):format(
|
||||||
|
outputItemSettings._title,
|
||||||
|
self:recipeToString(recipeIndex, mainList))
|
||||||
|
|
||||||
|
return craftDesc
|
||||||
|
end
|
||||||
|
|
||||||
return BaseCraft
|
return BaseCraft
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ local PLAYERS_KNOW_ALL_RECIPES = true
|
|||||||
|
|
||||||
---Abstract class for handling crafting.
|
---Abstract class for handling crafting.
|
||||||
---@class CraftSystem.BaseCraftHandler : Class
|
---@class CraftSystem.BaseCraftHandler : Class
|
||||||
|
---@field baseType CraftSystem.BaseCraftType
|
||||||
---@field type CraftSystem.CraftType
|
---@field type CraftSystem.CraftType
|
||||||
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
||||||
---
|
---
|
||||||
@@ -26,12 +27,14 @@ local BaseCraftHandler = Class:extend({
|
|||||||
---@class CraftSystem.BaseCraftHandlerDef
|
---@class CraftSystem.BaseCraftHandlerDef
|
||||||
---@field defaultCraftDef CraftSystem.BaseCraftDef?
|
---@field defaultCraftDef CraftSystem.BaseCraftDef?
|
||||||
|
|
||||||
|
---@param baseType CraftSystem.BaseCraftType
|
||||||
---@param type CraftSystem.CraftType
|
---@param type CraftSystem.CraftType
|
||||||
---@param def CraftSystem.BaseCraftHandlerDef
|
---@param def CraftSystem.BaseCraftHandlerDef
|
||||||
---@return CraftSystem.BaseCraftHandler
|
---@return CraftSystem.BaseCraftHandler
|
||||||
function BaseCraftHandler:new(type, def)
|
function BaseCraftHandler:new(baseType, type, def)
|
||||||
---@type CraftSystem.BaseCraftHandler
|
---@type CraftSystem.BaseCraftHandler
|
||||||
local instance = self:getInstance({})
|
local instance = self:getInstance({})
|
||||||
|
instance.baseType = baseType
|
||||||
instance.type = type
|
instance.type = type
|
||||||
instance.defaultCraftDef = def.defaultCraftDef or {}
|
instance.defaultCraftDef = def.defaultCraftDef or {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
local getInstance, extend = Class.getInstance, Class.extend
|
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
local require = modInfo.require
|
local require = modInfo.require
|
||||||
|
|
||||||
@@ -18,7 +16,6 @@ local stageTypes = {
|
|||||||
---Immersive crafting aimed at a more logical process of crafting items.
|
---Immersive crafting aimed at a more logical process of crafting items.
|
||||||
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
|
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
|
||||||
---@field startStageName string
|
---@field startStageName string
|
||||||
---@field triggerItem ItemStackString
|
|
||||||
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
|
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
|
||||||
local ImmersiveCraft = BaseCraft:extend({})
|
local ImmersiveCraft = BaseCraft:extend({})
|
||||||
|
|
||||||
@@ -27,33 +24,27 @@ local ImmersiveCraft = BaseCraft:extend({})
|
|||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef
|
---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef
|
||||||
---@field startStageName string
|
---@field startStageName string
|
||||||
---@field triggerItem ItemStackString
|
|
||||||
---@field stages CraftSystem.ImmersiveCraftStageDef[]
|
---@field stages CraftSystem.ImmersiveCraftStageDef[]
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param def CraftSystem.ImmersiveCraftDef
|
---@param def CraftSystem.ImmersiveCraftDef
|
||||||
---@return CraftSystem.ImmersiveCraft
|
---@return CraftSystem.ImmersiveCraft
|
||||||
function ImmersiveCraft:new(name, def)
|
function ImmersiveCraft:new(name, def)
|
||||||
-- We create a table of crafting stages that the player needs to complete.
|
|
||||||
---@type table<string, CraftSystem.ImmersiveCraftStage>
|
---@type table<string, CraftSystem.ImmersiveCraftStage>
|
||||||
local stagesByName = {}
|
local stagesByName = {}
|
||||||
|
|
||||||
for _, stageDef in ipairs(def.stages) do
|
for _, stageDef in ipairs(def.stages) do
|
||||||
-- We get the stage class based on its type.
|
|
||||||
local StageClass = stageTypes[stageDef.type]
|
local StageClass = stageTypes[stageDef.type]
|
||||||
|
|
||||||
-- Create a stage and add the craft name to the instance.
|
|
||||||
stageDef.craftName = name
|
stageDef.craftName = name
|
||||||
local stage = StageClass:new(stageDef)
|
local stage = StageClass:new(stageDef)
|
||||||
|
|
||||||
-- We add an instance of the stage to the table by name.
|
|
||||||
stagesByName[stageDef.name] = stage
|
stagesByName[stageDef.name] = stage
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type CraftSystem.ImmersiveCraft
|
---@type CraftSystem.ImmersiveCraft
|
||||||
local instance = BaseCraft:new(name, def)
|
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||||
instance.startStageName = def.startStageName
|
instance.startStageName = def.startStageName
|
||||||
instance.triggerItem = def.triggerItem
|
|
||||||
instance.stagesByName = stagesByName
|
instance.stagesByName = stagesByName
|
||||||
|
|
||||||
-- Subscribe to an event that will determine that the player has completed the crafting stage.
|
-- Subscribe to an event that will determine that the player has completed the crafting stage.
|
||||||
@@ -63,27 +54,17 @@ function ImmersiveCraft:new(name, def)
|
|||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
---@param Player Player
|
||||||
function(eventName, stage, pos, Player)
|
function(eventName, stage, pos, Player)
|
||||||
-- From the current stage (which has been completed) we get the name of the next one.
|
|
||||||
local nextStageName = stage.nextStage
|
local nextStageName = stage.nextStage
|
||||||
|
|
||||||
-- If there is no next stage...
|
|
||||||
if nextStageName == nil then
|
if nextStageName == nil then
|
||||||
-- ...we are finishing the craft.
|
|
||||||
instance:endCraft(pos, Player)
|
instance:endCraft(pos, Player)
|
||||||
else
|
else
|
||||||
-- ...else we start the next stage.
|
|
||||||
local nextStage = instance.stagesByName[nextStageName]
|
local nextStage = instance.stagesByName[nextStageName]
|
||||||
|
|
||||||
-- We get a table of temporary data.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
local meta = core.get_meta(pos)
|
||||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
meta:set_string('stage_name', nextStage.name)
|
||||||
|
|
||||||
-- We are updating the crafting stage in it.
|
|
||||||
craftData.stageName = nextStageName
|
|
||||||
-- We save it to the workstation metadata.
|
|
||||||
Smc.setCraftDataByPos(pos, craftData)
|
|
||||||
|
|
||||||
-- We are starting the next stage of crafting.
|
|
||||||
nextStage:startHandler(pos, Player)
|
nextStage:startHandler(pos, Player)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@@ -99,10 +80,15 @@ end
|
|||||||
---@param amount integer
|
---@param amount integer
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function ImmersiveCraft:startCraft(pos, player, amount)
|
function ImmersiveCraft:startCraft(pos, player, amount)
|
||||||
-- Adding craft to the counter.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
-- If crafting has not been added...
|
local meta = core.get_meta(pos)
|
||||||
|
local craftName = meta:get_string('craft_name')
|
||||||
|
if craftName ~= '' then
|
||||||
|
core.chat_send_player(player:get_player_name(), 'The workbench is occupied by another craft.')
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
if not ImmersiveCraftCounter:addCraft(pos, player:get_player_name()) then
|
if not ImmersiveCraftCounter:addCraft(pos, player:get_player_name()) then
|
||||||
-- ...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: '..
|
||||||
'complete or cancel active crafts to start a new one')
|
'complete or cancel active crafts to start a new one')
|
||||||
@@ -110,42 +96,13 @@ function ImmersiveCraft:startCraft(pos, player, amount)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- We get the material as an ItemStack.
|
|
||||||
---@type ItemStack
|
|
||||||
local stack = ItemStack(self.triggerItem)
|
|
||||||
-- We edit the amount of materials in accordance with the number of crafts being performed.
|
|
||||||
stack:set_count(stack:get_count() * amount)
|
|
||||||
|
|
||||||
-- We get the player's inventory, from where we must take the materials for crafting.
|
|
||||||
local playerInventory = player:get_inventory()
|
|
||||||
|
|
||||||
-- If the player does not have the necessary materials for crafting...
|
|
||||||
if not playerInventory:contains_item('main', stack) then
|
|
||||||
-- ...we inform him about this.
|
|
||||||
core.chat_send_player(player:get_player_name(),
|
|
||||||
'Not enough materials for crafting')
|
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- If we have the materials, we take them and start initializing the craft.
|
|
||||||
playerInventory:remove_item('main', stack)
|
|
||||||
|
|
||||||
-- We get the first stage of crafting.
|
|
||||||
local stage = self.stagesByName[self.startStageName]
|
local stage = self.stagesByName[self.startStageName]
|
||||||
|
|
||||||
-- Create a temporary data table for crafting.
|
meta:set_string('player_name', player:get_player_name())
|
||||||
---@type ImmersiveCraftData
|
meta:set_string('craft_name', self.name)
|
||||||
local craftData = {
|
meta:set_string('stage_name', stage.name)
|
||||||
playerName = player:get_player_name(), -- The name of the player who started the craft.
|
meta:set_float('craft_amount', amount)
|
||||||
craftName = self.name,
|
|
||||||
stageName = stage.name,
|
|
||||||
amount = amount,
|
|
||||||
}
|
|
||||||
-- We save it to the workstation metadata.
|
|
||||||
Smc.setCraftDataByPos(pos, craftData)
|
|
||||||
|
|
||||||
-- We are starting the first stage of crafting.
|
|
||||||
stage:startHandler(pos, player)
|
stage:startHandler(pos, player)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -155,77 +112,84 @@ end
|
|||||||
---@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.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
---@type ImmersiveCraftData?
|
local meta = core.get_meta(pos)
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
local playerName = meta:get_string('player_name')
|
||||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
assert(playerName ~= '')
|
||||||
|
local amount = meta:get_float('craft_amount')
|
||||||
|
assert(amount ~= 0)
|
||||||
|
|
||||||
-- We drop the result of crafting.
|
-- Drop the result of crafting.
|
||||||
local dropPos = vector.new(pos) + vector.new(0, 0.9,0)
|
local dropPos = vector.new(pos) + vector.new(0, 0.9,0)
|
||||||
for i=1, craftData.amount do
|
for i=1, amount do
|
||||||
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, playerName)
|
||||||
ImmersiveCraftCounter:removeCraft(pos, craftData.playerName)
|
|
||||||
|
|
||||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
-- Clear craft data
|
||||||
Smc.setCraftDataByPos(pos)
|
meta:set_string('player_name', '')
|
||||||
|
meta:set_string('craft_name', '')
|
||||||
|
meta:set_string('stage_name', '')
|
||||||
|
meta:set_float('craft_amount', 0)
|
||||||
|
meta:set_string('stage_data', '')
|
||||||
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
|
||||||
function ImmersiveCraft:abortCraft(pos)
|
function ImmersiveCraft:abortCraft(pos)
|
||||||
-- We get current data on crafting.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
---@type ImmersiveCraftData?
|
local meta = core.get_meta(pos)
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
local playerName = meta:get_string('player_name')
|
||||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
assert(playerName ~= '')
|
||||||
|
local stageName = meta:get_string('stage_name')
|
||||||
|
assert(stageName ~= '')
|
||||||
|
|
||||||
-- We get the current stage and start the abort process.
|
local stage = self.stagesByName[stageName]
|
||||||
local stage = self.stagesByName[craftData.stageName]
|
|
||||||
stage:abortHandler(pos)
|
stage:abortHandler(pos)
|
||||||
|
|
||||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
-- Clear craft data
|
||||||
Smc.setCraftDataByPos(pos)
|
meta:set_string('player_name', '')
|
||||||
|
meta:set_string('craft_name', '')
|
||||||
|
meta:set_string('stage_name', '')
|
||||||
|
meta:set_float('craft_amount', 0)
|
||||||
|
meta:set_string('stage_data', '')
|
||||||
|
|
||||||
-- Remove craft from the counter.
|
ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||||
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.
|
||||||
---@return string
|
---@return string
|
||||||
function ImmersiveCraft:toString()
|
function ImmersiveCraft:recipeToString(recipeIndex, mainList)
|
||||||
-- We get the material as an ItemStack.
|
local recipeDesc = ''
|
||||||
---@type ItemStack
|
|
||||||
local stack = ItemStack(self.triggerItem)
|
|
||||||
-- We obtain a table of item parameters.
|
|
||||||
---@type ItemDefinition
|
|
||||||
local itemDef = stack:get_definition()
|
|
||||||
|
|
||||||
-- Let's start writing a description of the craft.
|
|
||||||
local craftDesc = ('Material: %s x%s\n\n'):format(itemDef._title, stack:get_count())..
|
|
||||||
'Crafting process:\n'
|
|
||||||
|
|
||||||
-- Let's start with the first crafting stage.
|
|
||||||
---@type string?
|
---@type string?
|
||||||
local stageName = self.startStageName
|
local stageName = self.startStageName
|
||||||
local stages = self.stagesByName
|
local stages = self.stagesByName
|
||||||
|
|
||||||
-- While we have a stage name...
|
|
||||||
local count = 1
|
local count = 1
|
||||||
while stageName ~= nil do
|
while stageName ~= nil do
|
||||||
-- We get a crafting stage...
|
|
||||||
local stage = stages[stageName]
|
local stage = stages[stageName]
|
||||||
|
|
||||||
-- ...and we add its description to the list of actions for crafting.
|
recipeDesc = recipeDesc..tostring(count)..'. '..stage:toString()..'\n'
|
||||||
craftDesc = craftDesc..tostring(count)..'. '..stage:toString()..'\n'
|
|
||||||
|
|
||||||
-- We also get the name of the next stage.
|
|
||||||
stageName = stage.nextStage
|
stageName = stage.nextStage
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the cycle is interrupted, it means we have processed all the stages.
|
return recipeDesc
|
||||||
-- Return the craft description.
|
end
|
||||||
|
|
||||||
|
---@param recipeIndex integer?
|
||||||
|
---@param mainList ItemStack[]?
|
||||||
|
---@return string
|
||||||
|
function ImmersiveCraft:toString(recipeIndex, mainList)
|
||||||
|
local outputItem = ItemStack(self.output)
|
||||||
|
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||||
|
|
||||||
|
local craftDesc = ('%s\n\nRecipe:\n%s\nActions:\n%s'):format(
|
||||||
|
outputItemSettings._title,
|
||||||
|
BaseCraft.recipeToString(self, recipeIndex, mainList),
|
||||||
|
self:recipeToString(recipeIndex, mainList))
|
||||||
|
|
||||||
return craftDesc
|
return craftDesc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -13,15 +13,11 @@ local ImmersiveCraft = require('CraftSystem.ImmersiveCraft')
|
|||||||
---@class CraftSystem.ImmersiveCraftHandler : CraftSystem.BaseCraftHandler
|
---@class CraftSystem.ImmersiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||||
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
||||||
---
|
---
|
||||||
---@field craftsByTriggerItem table<string, CraftSystem.ImmersiveCraft[]>
|
---@field new fun(self:CraftSystem.ImmersiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ImmersiveCraftType, def:CraftSystem.ImmersiveCraftHandlerDef):CraftSystem.ImmersiveCraftHandler
|
||||||
---
|
|
||||||
---@field new fun(self:CraftSystem.ImmersiveCraftHandler, type:CraftSystem.ImmersiveCraftType, def:CraftSystem.ImmersiveCraftHandlerDef):CraftSystem.ImmersiveCraftHandler
|
|
||||||
---@field getCraftByName fun(self, craftName:string):CraftSystem.ImmersiveCraft
|
---@field getCraftByName fun(self, craftName:string):CraftSystem.ImmersiveCraft
|
||||||
---@field getCraftsByGroup fun(self, craftGroup):CraftSystem.ImmersiveCraft[]
|
---@field getCraftsByGroup fun(self, craftGroup):CraftSystem.ImmersiveCraft[]
|
||||||
---@field getLearnedCrafts fun(self, playerName):table<string, CraftSystem.ImmersiveCraft>
|
---@field getLearnedCrafts fun(self, playerName):table<string, CraftSystem.ImmersiveCraft>
|
||||||
local ImmersiveCraftHandler = BaseCraftHandler:extend({
|
local ImmersiveCraftHandler = BaseCraftHandler:extend({})
|
||||||
craftsByTriggerItem = {},
|
|
||||||
})
|
|
||||||
|
|
||||||
---@class CraftSystem.ImmersiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
---@class CraftSystem.ImmersiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
||||||
---@field defaultCraftDef CraftSystem.ImmersiveCraftDef?
|
---@field defaultCraftDef CraftSystem.ImmersiveCraftDef?
|
||||||
@@ -36,46 +32,22 @@ function ImmersiveCraftHandler:registerCraft(craftName, def)
|
|||||||
|
|
||||||
local def = table.merge(def, self.defaultCraftDef)
|
local def = table.merge(def, self.defaultCraftDef)
|
||||||
|
|
||||||
-- The craft group to which the craft belongs.
|
|
||||||
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||||
def.group = group
|
def.group = group
|
||||||
|
|
||||||
-- We obtain a crafting instance according to the given parameters.
|
local craft = (ImmersiveCraft:new(craftName, def))
|
||||||
local craft = ImmersiveCraft:new(craftName, def)
|
|
||||||
|
|
||||||
-- If no craft with this group has been registered before...
|
|
||||||
if self.craftsByGroup[group] == nil then
|
if self.craftsByGroup[group] == nil then
|
||||||
-- ...then we create a list for this group.
|
|
||||||
self.craftsByGroup[group] = {}
|
self.craftsByGroup[group] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The material from which crafting begins (also known as the starting item).
|
|
||||||
local triggerItem = def.triggerItem
|
|
||||||
|
|
||||||
-- If no crafting for such a starting item has been registered before...
|
|
||||||
if self.craftsByTriggerItem[triggerItem] == nil then
|
|
||||||
-- ...then we create a list for this item.
|
|
||||||
self.craftsByTriggerItem[triggerItem] = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Adding craft to the lists:
|
|
||||||
self.craftsByName[craftName] = craft
|
self.craftsByName[craftName] = craft
|
||||||
tableInsert(self.craftsByGroup[group], craft)
|
tableInsert(self.craftsByGroup[group], craft)
|
||||||
tableInsert(self.craftsByTriggerItem[triggerItem], craft)
|
|
||||||
tableInsert(self.craftsList, craft)
|
tableInsert(self.craftsList, craft)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns a list of crafts that use the given item as a starting material.
|
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||||
---
|
-- Provides a convenient list of crafting items in the player's inventory.
|
||||||
---Will return `nil` if the item is not used in any craft as a starting item.
|
|
||||||
---@param triggerItem string
|
|
||||||
---@return CraftSystem.ImmersiveCraft[]?
|
|
||||||
function ImmersiveCraftHandler:getCraftsByTriggerItem(triggerItem)
|
|
||||||
local crafts = self.craftsByTriggerItem[triggerItem]
|
|
||||||
|
|
||||||
return crafts
|
|
||||||
end
|
|
||||||
|
|
||||||
core.register_on_mods_loaded(function()
|
core.register_on_mods_loaded(function()
|
||||||
for groupName, crafts in pairs(ImmersiveCraftHandler.craftsByGroup) do
|
for groupName, crafts in pairs(ImmersiveCraftHandler.craftsByGroup) do
|
||||||
tableSort(crafts, function(a, b)
|
tableSort(crafts, function(a, b)
|
||||||
|
|||||||
@@ -40,11 +40,9 @@ local tappingNode = ImmersiveCraftNode:new({
|
|||||||
callbacks = {
|
callbacks = {
|
||||||
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||||
on_activate = function(luaEntity, staticdata, dtime)
|
on_activate = function(luaEntity, staticdata, dtime)
|
||||||
-- We obtain a table of initial parameters for the craft node entity.
|
|
||||||
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
|
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
|
||||||
local params = core.deserialize(staticdata)
|
local params = core.deserialize(staticdata)
|
||||||
|
|
||||||
-- We enter parameters into the entity's custom fields.
|
|
||||||
if params ~= nil then
|
if params ~= nil then
|
||||||
luaEntity._item_name = params.targetItem
|
luaEntity._item_name = params.targetItem
|
||||||
luaEntity._craft_name = params.craftName
|
luaEntity._craft_name = params.craftName
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
local extend = Class.extend
|
local extend = Class.extend
|
||||||
|
|
||||||
local getStageDataByPos, setStageDataByPos = Smc.getStageDataByPos, Smc.setStageDataByPos
|
|
||||||
|
|
||||||
local require = Mod.getInfo('smc__core__craft_system').require
|
local require = Mod.getInfo('smc__core__craft_system').require
|
||||||
|
|
||||||
---@type CraftSystem.ImmersiveCraftStage
|
---@type CraftSystem.ImmersiveCraftStage
|
||||||
@@ -29,7 +27,7 @@ local TappingStage = extend(ImmersiveCraftStage, {})
|
|||||||
---Data table for transferring various time parameters between calls.
|
---Data table for transferring various time parameters between calls.
|
||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class ImmersiveStageData.TappingStageData : ImmersiveStageData
|
---@class CraftSystem.ImmersiveCraftStage.TappingStageData
|
||||||
---How many clicks were made by the player on the entity.
|
---How many clicks were made by the player on the entity.
|
||||||
---@field clickCount integer
|
---@field clickCount integer
|
||||||
---Unique identifier of the entity (craft node).
|
---Unique identifier of the entity (craft node).
|
||||||
@@ -45,10 +43,8 @@ function TappingStage:new(def)
|
|||||||
|
|
||||||
-- 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).
|
|
||||||
local entityPos = vector.new(pos) + vector.new(0,0.875,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.
|
|
||||||
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
||||||
local params = {
|
local params = {
|
||||||
targetItem = tappingStage.targetItem,
|
targetItem = tappingStage.targetItem,
|
||||||
@@ -57,89 +53,83 @@ function TappingStage:new(def)
|
|||||||
WorkStationPos = pos,
|
WorkStationPos = pos,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Adding an entity to the world.
|
|
||||||
local obj = core.add_entity(entityPos, 'craft_system:tapping_node', core.serialize(params))
|
local obj = core.add_entity(entityPos, 'craft_system:tapping_node', core.serialize(params))
|
||||||
|
|
||||||
-- If the entity was not returned, something went wrong.
|
|
||||||
if obj == nil then
|
if obj == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create a temporary table of crafting stage data.
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData
|
||||||
---@type ImmersiveStageData.TappingStageData
|
|
||||||
local stageData = {
|
local stageData = {
|
||||||
clickCount = 0,
|
clickCount = 0,
|
||||||
entityGUID = obj:get_guid()
|
entityGUID = obj:get_guid()
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Saving temporary data in work station metadata.
|
--@type ImmersiveWorkStation.MetaData
|
||||||
setStageDataByPos(pos, stageData)
|
local meta = core.get_meta(pos)
|
||||||
|
meta:set_string('stage_data', core.serialize(stageData))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 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.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
---@type ImmersiveStageData.TappingStageData?
|
local meta = core.get_meta(pos)
|
||||||
local stageData = getStageDataByPos(pos)
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||||
|
assert(stageData ~= nil)
|
||||||
|
|
||||||
-- Increasing the click counter
|
|
||||||
local clickCount = stageData.clickCount
|
local clickCount = stageData.clickCount
|
||||||
clickCount = clickCount + 1
|
clickCount = clickCount + 1
|
||||||
|
|
||||||
-- 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.
|
|
||||||
tappingStage:endHandler(pos, player)
|
tappingStage:endHandler(pos, player)
|
||||||
else
|
else
|
||||||
-- ...else we save the number of clicks in the temporary data table.
|
|
||||||
stageData.clickCount = clickCount
|
stageData.clickCount = clickCount
|
||||||
Smc.setStageDataByPos(pos, stageData)
|
meta:set_string('stage_data', core.serialize(stageData))
|
||||||
end
|
end
|
||||||
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.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
---@type ImmersiveStageData.TappingStageData?
|
local meta = core.get_meta(pos)
|
||||||
local stageData = getStageDataByPos(pos)
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||||
|
assert(stageData ~= nil)
|
||||||
|
|
||||||
|
|
||||||
-- We get the entity object.
|
|
||||||
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||||
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||||
|
|
||||||
-- Removing an entity from the world.
|
|
||||||
entity:remove()
|
entity:remove()
|
||||||
|
|
||||||
-- 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)
|
instance.abortHandler = function(tappingStage, pos)
|
||||||
-- We receive current data on crafting.
|
---@type ImmersiveWorkStation.MetaData
|
||||||
---@type ImmersiveCraftData?
|
local meta = core.get_meta(pos)
|
||||||
local craftData = Smc.getCraftDataByPos(pos)
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||||
|
assert(stageData ~= nil)
|
||||||
|
local amount = meta:get_float('craft_amount')
|
||||||
|
assert(amount ~= 0)
|
||||||
|
|
||||||
-- Obtaining stage time data from work station metadata.
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||||
---@type ImmersiveStageData.TappingStageData?
|
local stageData = stageData
|
||||||
local stageData = craftData.stageData
|
|
||||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||||
|
|
||||||
-- We get the entity object.
|
|
||||||
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||||
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||||
|
|
||||||
-- Removing an entity from the world.
|
|
||||||
entity:remove()
|
entity:remove()
|
||||||
|
|
||||||
-- We drop the item as it was before processing.
|
-- We drop the item as it was before processing.
|
||||||
local dropPos = vector.new(pos)+vector.new(0, 0.9,0)
|
local dropPos = vector.new(pos)+vector.new(0, 0.9,0)
|
||||||
for i=1, craftData.amount do
|
for i=1, amount do
|
||||||
core.item_drop(ItemStack(tappingStage.targetItem), nil, dropPos)
|
core.item_drop(ItemStack(tappingStage.targetItem), nil, dropPos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -161,12 +151,10 @@ function TappingStage:new(def)
|
|||||||
---@param craftNode LuaEntity
|
---@param craftNode LuaEntity
|
||||||
---@param player Player
|
---@param player Player
|
||||||
function(eventName, pos, player, craftNode)
|
function(eventName, pos, player, craftNode)
|
||||||
-- We get the item that the player clicked on the crafting node.
|
|
||||||
local toolDef = player:get_wielded_item():get_definition()
|
local toolDef = player:get_wielded_item():get_definition()
|
||||||
|
|
||||||
-- If a crafting stage requires a specific tool group and he doesn't have it...
|
-- If a crafting stage requires a specific tool group and he doesn't have it then we don't do anything.
|
||||||
if instance.toolGroup and (not toolDef.groups or not toolDef.groups[instance.toolGroup]) then
|
if instance.toolGroup and (not toolDef.groups or not toolDef.groups[instance.toolGroup]) then
|
||||||
-- ...then we don't do anything.
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -189,7 +177,6 @@ function TappingStage:new(def)
|
|||||||
core.after(0.2, function() craftNode.object:set_rotation(vector.new(0,0,0))end)
|
core.after(0.2, function() craftNode.object:set_rotation(vector.new(0,0,0))end)
|
||||||
-- DRAFT END
|
-- DRAFT END
|
||||||
|
|
||||||
-- Calling the crafting stage process handler.
|
|
||||||
instance:processHandler(pos, player)
|
instance:processHandler(pos, player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ local require = modInfo.require
|
|||||||
local BaseCraft = require('CraftSystem.BaseCraft')
|
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||||
|
|
||||||
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
|
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
|
||||||
---@field altRecipes CraftSystem.CraftRecipe[]
|
|
||||||
---@field returnItems ItemStackString[]?
|
---@field returnItems ItemStackString[]?
|
||||||
local PrimitiveCraft = BaseCraft:extend({})
|
local PrimitiveCraft = BaseCraft:extend({})
|
||||||
|
|
||||||
@@ -15,7 +14,6 @@ local PrimitiveCraft = BaseCraft:extend({})
|
|||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
|
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
|
||||||
---@field altRecipes CraftSystem.CraftRecipe[]?
|
|
||||||
---@field returnItems ItemStackString[]?
|
---@field returnItems ItemStackString[]?
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
@@ -23,8 +21,7 @@ local PrimitiveCraft = BaseCraft:extend({})
|
|||||||
---@return CraftSystem.PrimitiveCraft
|
---@return CraftSystem.PrimitiveCraft
|
||||||
function PrimitiveCraft:new(name, def)
|
function PrimitiveCraft:new(name, def)
|
||||||
---@type CraftSystem.PrimitiveCraft
|
---@type CraftSystem.PrimitiveCraft
|
||||||
local instance = BaseCraft:new(name, def)
|
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||||
instance.altRecipes = def.altRecipes or {}
|
|
||||||
instance.returnItems = def.returnItems
|
instance.returnItems = def.returnItems
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ local PrimitiveCraft = require('CraftSystem.PrimitiveCraft')
|
|||||||
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.PrimitiveCraft[]>
|
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.PrimitiveCraft[]>
|
||||||
---@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
---@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||||
---
|
---
|
||||||
---@field new fun(self:CraftSystem.PrimitiveCraftHandler, type:CraftSystem.PrimitiveCraftType, def:CraftSystem.PrimitiveCraftHandlerDef):CraftSystem.PrimitiveCraftHandler
|
---@field new fun(self:CraftSystem.PrimitiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.PrimitiveCraftType, def:CraftSystem.PrimitiveCraftHandlerDef):CraftSystem.PrimitiveCraftHandler
|
||||||
---@field getCraftByName fun(self, craftName:string):CraftSystem.PrimitiveCraft
|
---@field getCraftByName fun(self, craftName:string):CraftSystem.PrimitiveCraft
|
||||||
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.PrimitiveCraft[]
|
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.PrimitiveCraft[]
|
||||||
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||||
@@ -62,6 +62,8 @@ function PrimitiveCraftHandler:registerAlternativeRecipes(craftName, recipes)
|
|||||||
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
|
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||||
|
-- Provides a convenient list of crafting items in the player's inventory.
|
||||||
core.register_on_mods_loaded(function()
|
core.register_on_mods_loaded(function()
|
||||||
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
|
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
|
||||||
tableSort(crafts, function(a, b)
|
tableSort(crafts, function(a, b)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ local ProcessingCraft = BaseCraft:extend({})
|
|||||||
---@return CraftSystem.ProcessingCraft
|
---@return CraftSystem.ProcessingCraft
|
||||||
function ProcessingCraft:new(name, def)
|
function ProcessingCraft:new(name, def)
|
||||||
---@type CraftSystem.ProcessingCraft
|
---@type CraftSystem.ProcessingCraft
|
||||||
local instance = BaseCraft:new(name, def)
|
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||||
instance.time = def.time
|
instance.time = def.time
|
||||||
instance.startHandler = def.startHandler or function() end
|
instance.startHandler = def.startHandler or function() end
|
||||||
instance.processHandler = def.processHandler or function() end
|
instance.processHandler = def.processHandler or function() end
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ local ProcessingCraft = require('CraftSystem.ProcessingCraft')
|
|||||||
---
|
---
|
||||||
---@field craftsByHash table<string, CraftSystem.ProcessingCraft>
|
---@field craftsByHash table<string, CraftSystem.ProcessingCraft>
|
||||||
---
|
---
|
||||||
---@field new fun(self:CraftSystem.ProcessingCraftHandler, type:CraftSystem.ProcessingCraftType, def:CraftSystem.ProcessingCraftHandlerDef):CraftSystem.ProcessingCraftHandler
|
---@field new fun(self:CraftSystem.ProcessingCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ProcessingCraftType, def:CraftSystem.ProcessingCraftHandlerDef):CraftSystem.ProcessingCraftHandler
|
||||||
---@field getCraftByName fun(self, craftName:string):CraftSystem.ProcessingCraft
|
---@field getCraftByName fun(self, craftName:string):CraftSystem.ProcessingCraft
|
||||||
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.ProcessingCraft[]
|
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.ProcessingCraft[]
|
||||||
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[]
|
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[]
|
||||||
|
|||||||
Reference in New Issue
Block a user