CraftSystem: remove alt recipes

This commit is contained in:
Van
2026-07-03 19:53:27 +03:00
parent 6c10201fa9
commit 6f35ca8d6f
7 changed files with 13 additions and 75 deletions

View File

@@ -134,7 +134,7 @@ local function getMaterials(craft, recipeIndex, count)
local materialStacks = {}
local materialGroups = {}
for _, itemName in ipairs(craft:getRecipe(recipeIndex)) do
for _, itemName in ipairs(craft.recipe) do
local group = itemName:match('group:(%S+)')
local materialCount = itemName:match('group:%S+ (%S+)') or 1

View File

@@ -5,7 +5,6 @@ local CHECK = {YES = '[x]', NO = '[ ]', EMPTY = '-'}
---@class CraftSystem.BaseCraft : Class
---@field name string
---@field recipe ItemStackString[]
---@field altRecipes CraftSystem.CraftRecipe[]
---@field output ItemStackString
---@field group CraftSystem.CraftGroup
local BaseCraft = Class:extend({})
@@ -13,7 +12,6 @@ local BaseCraft = Class:extend({})
---Definition table for the `CraftSystem.BaseCraft`.
---@class CraftSystem.BaseCraftDef
---@field recipe ItemStackString[]
---@field altRecipes CraftSystem.CraftRecipe[]?
---@field output ItemStackString
---@field group CraftSystem.CraftGroup?
@@ -25,7 +23,6 @@ function BaseCraft:new(name, def)
local instance = self:getInstance({})
instance.name = name
instance.recipe = def.recipe
instance.altRecipes = def.altRecipes or {}
instance.output = def.output
instance.group = def.group
@@ -80,46 +77,11 @@ local function getCheckMaterialGroup(material, count, list)
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)
---@param mainList ItemStack[]?
---@return string
function BaseCraft:recipeToString(mainList)
local recipeDesc = ''
for _, itemName in ipairs(recipe) do
for _, itemName in ipairs(self.recipe) do
local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1
if group == nil then
@@ -139,16 +101,15 @@ function BaseCraft:recipeToString(recipeIndex, mainList)
return recipeDesc
end
---@param recipeIndex integer?
---@param mainList ItemStack[]?
---@return string
function BaseCraft:toString(recipeIndex, mainList)
---@param mainList ItemStack[]?
---@return string
function BaseCraft:toString(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))
self:recipeToString(mainList))
return craftDesc
end

View File

@@ -198,7 +198,7 @@ function ImmersiveCraft:toString(recipeIndex, mainList)
local craftDesc = ('%s\n\nRecipe:\n%s\nActions:\n%s'):format(
outputItemSettings._title,
BaseCraft.recipeToString(self, recipeIndex, mainList),
BaseCraft.recipeToString(self, mainList),
self:recipeToString(recipeIndex, mainList))
return craftDesc

View File

@@ -51,17 +51,6 @@ 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
-- 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()