CraftSystem: add alternative recipes to primitive crafts

This commit is contained in:
Van
2026-04-30 21:34:50 +03:00
parent 964a60ed7c
commit 08dde36326
3 changed files with 23 additions and 10 deletions

View File

@@ -25,7 +25,7 @@ local BaseCraftHandler = {
function BaseCraftHandler:getCraftByName(craftName)
local craft = self.craftsByName[craftName]
assert(craft ~= nil, ('There is no primitive craft named `%s`.'):format(
assert(craft ~= nil, ('A craft `%s` not exists.'):format(
craftName))
return craft
@@ -36,7 +36,7 @@ end
function BaseCraftHandler:getCraftsByGroup(craftGroup)
local crafts = self.craftsByGroup[craftGroup]
assert(crafts ~= nil, ('There are no primitive crafts with `%s` group'):format(
assert(crafts ~= nil, ('A craft with group `%s` not exists.'):format(
craftGroup))
return crafts

View File

@@ -8,7 +8,8 @@ local BaseCraft = require('CraftSystem.BaseCraft')
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
---@field recipe CraftSystem.CraftRecipe
---@field returnItems ItemStackString[]
---@field altRecipes CraftSystem.CraftRecipe[]
---@field returnItems ItemStackString[]?
local PrimitiveCraft = extend(BaseCraft, {})
---Definition table for the `CraftSystem.PrimitiveCraft`.
@@ -16,6 +17,7 @@ local PrimitiveCraft = extend(BaseCraft, {})
---**Only for EmmyLua.**
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
---@field recipe CraftSystem.CraftRecipe
---@field altRecipes CraftSystem.CraftRecipe[]?
---@field returnItems ItemStackString[]?
---@param name string
@@ -23,13 +25,13 @@ local PrimitiveCraft = extend(BaseCraft, {})
---@return CraftSystem.PrimitiveCraft
function PrimitiveCraft:new(name, def)
---@type CraftSystem.PrimitiveCraft
local instance = getInstance(self, {
name = name,
group = def.group,
output = def.output,
recipe = def.recipe,
returnItems = def.returnItems or {}
})
local instance = getInstance(self, {})
instance.name = name
instance.group = def.group
instance.output = def.output
instance.recipe = def.recipe
instance.altRecipes = def.altRecipes or {}
instance.returnItems = def.returnItems
return instance
end

View File

@@ -42,6 +42,17 @@ function PrimitiveCraftHandler:registerCraft(craftName, def)
tableInsert(self.craftsList, craft)
end
---Adds alternative recipes to the craft with the specified name.
---
---If a craft with the specified name does not exist, an error will call.
---@param craftName string
---@param recipes CraftSystem.CraftRecipe[]
function PrimitiveCraftHandler:registerAlternativeRecipes(craftName, recipes)
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
end
core.register_on_mods_loaded(function()
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
tableSort(crafts, function(a, b)