From a9782564bfc10bb2cfcf5475983384c9fcb2c302 Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sun, 23 Nov 2025 19:02:28 +0100 Subject: [PATCH] Add Recipe:addResult --- cf-lib/data/Recipe.lua | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 01358d5..199005c 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -112,14 +112,50 @@ function Recipe:removeIngredient(ingredientName) return self end +--- Adds a result to the recipe +--- @param resultName string The name of the result +--- @param amount number|table The amount of the result (default: 1) or a table with custom properties +function Recipe:addResult(resultName, amount) + local props = amount + if type(props) == "string" then + props = { amount = props } + end + if not props.amount then + props.amount = 1 + end + local resultType = data.raw.item[resultName] and "item" + or data.raw.module[resultName] and "item" + or data.raw.fluid[resultName] and "fluid" + or nil + if not resultType then + log("Unknown result: " .. resultName) + return + end + self.prototype.results = self.prototype.results or {} + for _, result in pairs(self.prototype.results) do + if result.name == resultName then + result.amount = nil + result.amount_min = nil + result.amount_max = nil + result.probability = nil + for k, v in pairs(props) do + result[k] = v + end + return self + end + end + props.name = resultName + props.type = resultType + table.insert(self.prototype.results, props) + return self +end + --- Replaces an existing result by name with a new result --- @param old string The name of the existing result --- @param new? string The name of the new result --- @param amount? number The amount of the new result (keeps the old value if not set) ---- @param expensiveAmount? number The amount of the new result for the expensive recipe (uses amount if not set) -function Recipe:replaceResult(old, new, amount, expensiveAmount) +function Recipe:replaceResult(old, new, amount) if type(new) == "number" then - expensiveAmount = amount amount = new new = old end