CraftSystem: add getCraftByHash method to ProcessingCraftHandler, minor fixes
This commit is contained in:
@@ -9,7 +9,7 @@ local ImmersiveCraftHandler= require('CraftSystem.ImmersiveCraftHandler')
|
|||||||
local ProcessingCraftHandler= require('CraftSystem.ProcessingCraftHandler')
|
local ProcessingCraftHandler= require('CraftSystem.ProcessingCraftHandler')
|
||||||
|
|
||||||
---@alias CraftSystem.CraftGroup string
|
---@alias CraftSystem.CraftGroup string
|
||||||
---@alias CraftSystem.CraftRecipe string[]
|
---@alias CraftSystem.CraftRecipe ItemStackString[]
|
||||||
|
|
||||||
---@class CraftSystem
|
---@class CraftSystem
|
||||||
---@field PrimitiveCraftHandlers table<string, CraftSystem.PrimitiveCraftHandler>
|
---@field PrimitiveCraftHandlers table<string, CraftSystem.PrimitiveCraftHandler>
|
||||||
@@ -27,7 +27,7 @@ local CraftSystem = {
|
|||||||
|
|
||||||
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
|
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
|
||||||
---@alias CraftSystem.ImmersiveCraftType 'workbench'
|
---@alias CraftSystem.ImmersiveCraftType 'workbench'
|
||||||
---@alias CraftSystem.ProcessingCraftType ''
|
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
|
||||||
|
|
||||||
---@param type CraftSystem.PrimitiveCraftType
|
---@param type CraftSystem.PrimitiveCraftType
|
||||||
local function registerPrimitiveCraftType(type)
|
local function registerPrimitiveCraftType(type)
|
||||||
@@ -83,6 +83,7 @@ end
|
|||||||
--Registration of craft types:
|
--Registration of craft types:
|
||||||
registerPrimitiveCraftType('inside_player_inventory')
|
registerPrimitiveCraftType('inside_player_inventory')
|
||||||
registerImmersiveCraftType('workbench')
|
registerImmersiveCraftType('workbench')
|
||||||
|
registerProcessingCraftType('test') -- DEBUG
|
||||||
|
|
||||||
|
|
||||||
--- Registration methods ---
|
--- Registration methods ---
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ local require = modInfo.require
|
|||||||
local BaseCraft = require('CraftSystem.BaseCraft')
|
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||||
|
|
||||||
---@class CraftSystem.ProcessingCraft : CraftSystem.BaseCraft
|
---@class CraftSystem.ProcessingCraft : CraftSystem.BaseCraft
|
||||||
|
---@field recipe CraftSystem.CraftRecipe
|
||||||
---@field time number
|
---@field time number
|
||||||
---@field startHandler fun()
|
---@field startHandler fun()
|
||||||
---@field processHandler fun()
|
---@field processHandler fun()
|
||||||
@@ -17,6 +18,7 @@ local ProcessingCraft = extend(BaseCraft, {})
|
|||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class CraftSystem.ProcessingCraftDef : CraftSystem.BaseCraftDef
|
---@class CraftSystem.ProcessingCraftDef : CraftSystem.BaseCraftDef
|
||||||
|
---@field recipe CraftSystem.CraftRecipe
|
||||||
---@field time number
|
---@field time number
|
||||||
---@field startHandler fun()?
|
---@field startHandler fun()?
|
||||||
---@field processHandler fun()?
|
---@field processHandler fun()?
|
||||||
@@ -29,6 +31,7 @@ function ProcessingCraft:new(name, def)
|
|||||||
---@type CraftSystem.ProcessingCraft
|
---@type CraftSystem.ProcessingCraft
|
||||||
local instance = getInstance(self, {})
|
local instance = getInstance(self, {})
|
||||||
instance.name = name
|
instance.name = name
|
||||||
|
instance.recipe = def.recipe
|
||||||
instance.group = def.group
|
instance.group = def.group
|
||||||
instance.output = def.output
|
instance.output = def.output
|
||||||
instance.time = def.time
|
instance.time = def.time
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
local HASH_SEPARATOR = ','
|
||||||
|
|
||||||
local extend = Class.extend
|
local extend = Class.extend
|
||||||
|
|
||||||
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
|
local tableInsert, tableCopy, tableSort, tableConcat, tableIsEmpty = table.insert, table.copy, table.sort, table.concat, table.is_empty
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||||
local require = modInfo.require
|
local require = modInfo.require
|
||||||
@@ -14,12 +16,21 @@ local ProcessingCraft = require('CraftSystem.ProcessingCraft')
|
|||||||
---A primitive crafting processor that doesn't require much interaction and provides
|
---A primitive crafting processor that doesn't require much interaction and provides
|
||||||
---simple crafting based on a recipe-result scheme.
|
---simple crafting based on a recipe-result scheme.
|
||||||
---@class CraftSystem.ProcessingCraftHandler : CraftSystem.BaseCraftHandler
|
---@class CraftSystem.ProcessingCraftHandler : CraftSystem.BaseCraftHandler
|
||||||
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.ProcessingCraft[]>
|
---@field craftsByHash table<string, CraftSystem.ProcessingCraft>
|
||||||
---@field groupedCrafts (CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[]
|
|
||||||
---@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)[]
|
||||||
local ProcessingCraftHandler = extend(BaseCraftHandler, {})
|
local ProcessingCraftHandler = extend(BaseCraftHandler, {
|
||||||
|
craftsByHash = {}
|
||||||
|
})
|
||||||
|
|
||||||
|
---@param recipe CraftSystem.CraftRecipe
|
||||||
|
local function getCraftHash(recipe)
|
||||||
|
recipe = tableCopy(recipe)
|
||||||
|
|
||||||
|
tableSort(recipe)
|
||||||
|
return tableConcat(recipe, HASH_SEPARATOR)
|
||||||
|
end
|
||||||
|
|
||||||
---Register a primitive craft that does not require special interaction.
|
---Register a primitive craft that does not require special interaction.
|
||||||
---@param craftName string
|
---@param craftName string
|
||||||
@@ -37,9 +48,33 @@ function ProcessingCraftHandler:registerCraft(craftName, def)
|
|||||||
self.craftsByGroup[group] = {}
|
self.craftsByGroup[group] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local craftHash = getCraftHash(craft.recipe)
|
||||||
|
assert(self.craftsByHash[craftHash] == nil, 'A craft with this recipe already exists.')
|
||||||
|
|
||||||
self.craftsByName[craftName] = craft
|
self.craftsByName[craftName] = craft
|
||||||
tableInsert(self.craftsByGroup[group], craft)
|
tableInsert(self.craftsByGroup[group], craft)
|
||||||
tableInsert(self.craftsList, craft)
|
tableInsert(self.craftsList, craft)
|
||||||
|
self.craftsByHash[craftHash] = craft
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param input ItemStack[]
|
||||||
|
---@return CraftSystem.ProcessingCraft?
|
||||||
|
function ProcessingCraftHandler:getCraftByRecipe(input)
|
||||||
|
local recipe = {}
|
||||||
|
for _, item in ipairs(input) do
|
||||||
|
if not item:is_empty() then
|
||||||
|
tableInsert(recipe, item:get_name())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if tableIsEmpty(recipe) then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local craftHash = getCraftHash(recipe)
|
||||||
|
local craft = self.craftsByHash[craftHash]
|
||||||
|
|
||||||
|
return craft
|
||||||
end
|
end
|
||||||
|
|
||||||
return ProcessingCraftHandler
|
return ProcessingCraftHandler
|
||||||
|
|||||||
Reference in New Issue
Block a user