CraftSystem: add getCraftByHash method to ProcessingCraftHandler, minor fixes

This commit is contained in:
Van
2026-05-06 19:44:18 +03:00
parent 089105d3e9
commit 5265024231
3 changed files with 45 additions and 6 deletions

View File

@@ -9,7 +9,7 @@ local ImmersiveCraftHandler= require('CraftSystem.ImmersiveCraftHandler')
local ProcessingCraftHandler= require('CraftSystem.ProcessingCraftHandler')
---@alias CraftSystem.CraftGroup string
---@alias CraftSystem.CraftRecipe string[]
---@alias CraftSystem.CraftRecipe ItemStackString[]
---@class CraftSystem
---@field PrimitiveCraftHandlers table<string, CraftSystem.PrimitiveCraftHandler>
@@ -27,7 +27,7 @@ local CraftSystem = {
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
---@alias CraftSystem.ImmersiveCraftType 'workbench'
---@alias CraftSystem.ProcessingCraftType ''
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
---@param type CraftSystem.PrimitiveCraftType
local function registerPrimitiveCraftType(type)
@@ -83,6 +83,7 @@ end
--Registration of craft types:
registerPrimitiveCraftType('inside_player_inventory')
registerImmersiveCraftType('workbench')
registerProcessingCraftType('test') -- DEBUG
--- Registration methods ---

View File

@@ -7,6 +7,7 @@ local require = modInfo.require
local BaseCraft = require('CraftSystem.BaseCraft')
---@class CraftSystem.ProcessingCraft : CraftSystem.BaseCraft
---@field recipe CraftSystem.CraftRecipe
---@field time number
---@field startHandler fun()
---@field processHandler fun()
@@ -17,6 +18,7 @@ local ProcessingCraft = extend(BaseCraft, {})
---
---**Only for EmmyLua.**
---@class CraftSystem.ProcessingCraftDef : CraftSystem.BaseCraftDef
---@field recipe CraftSystem.CraftRecipe
---@field time number
---@field startHandler fun()?
---@field processHandler fun()?
@@ -29,6 +31,7 @@ function ProcessingCraft:new(name, def)
---@type CraftSystem.ProcessingCraft
local instance = getInstance(self, {})
instance.name = name
instance.recipe = def.recipe
instance.group = def.group
instance.output = def.output
instance.time = def.time

View File

@@ -1,6 +1,8 @@
local HASH_SEPARATOR = ','
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 require = modInfo.require
@@ -14,12 +16,21 @@ local ProcessingCraft = require('CraftSystem.ProcessingCraft')
---A primitive crafting processor that doesn't require much interaction and provides
---simple crafting based on a recipe-result scheme.
---@class CraftSystem.ProcessingCraftHandler : CraftSystem.BaseCraftHandler
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.ProcessingCraft[]>
---@field groupedCrafts (CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[]
---@field craftsByHash table<string, CraftSystem.ProcessingCraft>
---@field getCraftByName fun(self, craftName:string):CraftSystem.ProcessingCraft
---@field getCraftsByGroup fun(self, craftGroup:string):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.
---@param craftName string
@@ -37,9 +48,33 @@ function ProcessingCraftHandler:registerCraft(craftName, def)
self.craftsByGroup[group] = {}
end
local craftHash = getCraftHash(craft.recipe)
assert(self.craftsByHash[craftHash] == nil, 'A craft with this recipe already exists.')
self.craftsByName[craftName] = craft
tableInsert(self.craftsByGroup[group], 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
return ProcessingCraftHandler