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) function BaseCraftHandler:getCraftByName(craftName)
local craft = self.craftsByName[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)) craftName))
return craft return craft
@@ -36,7 +36,7 @@ end
function BaseCraftHandler:getCraftsByGroup(craftGroup) function BaseCraftHandler:getCraftsByGroup(craftGroup)
local crafts = self.craftsByGroup[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)) craftGroup))
return crafts return crafts

View File

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

View File

@@ -42,6 +42,17 @@ function PrimitiveCraftHandler:registerCraft(craftName, def)
tableInsert(self.craftsList, craft) tableInsert(self.craftsList, craft)
end 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() 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)