From ea7785c761d0cdc7efa03590626423274e203b3d Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Sun, 16 Mar 2025 18:40:40 +0100 Subject: [PATCH] Functions are chainable --- cf-lib/data/Recipe.lua | 12 +++++++++--- cf-lib/data/Technology.lua | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 4bbd0f1..1a5c67e 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -39,6 +39,7 @@ end ---- @param data table The data to assign function Recipe:assign(data) table.assign(self.prototype, data) + return self end --- Adds an ingredient to the recipe @@ -65,6 +66,7 @@ function Recipe:addIngredient(ingredientName, amount) end end table.insert(self.prototype.ingredients, { name = ingredientName, amount = amount, type = ingredientType }) + return self end --- Replaces an existing ingredient by name with a new ingredient or adjusts the amount @@ -81,10 +83,11 @@ function Recipe:replaceIngredient(old, new, amount) if result.name == old then result.name = new result.amount = amount or result.amount - return + return self end end end + return self end --- Removes an existing ingredient by name @@ -94,10 +97,11 @@ function Recipe:removeIngredient(ingredientName) for i, result in pairs(self.prototype.ingredients) do if result.name == ingredientName then self.prototype.ingredients[i] = nil - return + return self end end end + return self end --- Replaces an existing result by name with a new result @@ -122,6 +126,7 @@ function Recipe:replaceResult(old, new, amount, expensiveAmount) table.main_product = new end end + return self end --- Adds the recipe to a technology @@ -136,10 +141,11 @@ function Recipe:unlockedByTechnology(technology) end for _, effect in pairs(technology.effects) do if effect.type == "unlock-recipe" and effect.recipe == self.prototype.name then - return + return self end end table.insert(technology.effects, { type = "unlock-recipe", recipe = self.prototype.name }) + return self end ---- Clones the recipe diff --git a/cf-lib/data/Technology.lua b/cf-lib/data/Technology.lua index 2c641d5..f30d739 100644 --- a/cf-lib/data/Technology.lua +++ b/cf-lib/data/Technology.lua @@ -47,12 +47,14 @@ end --- @param data table The data to assign function Technology:assign(data) table.assign(self.prototype, data) + return self end --- Sets the prerequisite for the technology --- @param prerequisites table The names of the prerequisites function Technology:setPrerequisites(prerequisites) self.prototype.prerequisites = prerequisites + return self end --- Adds a prerequisite to the technology @@ -63,6 +65,7 @@ function Technology:addPrerequisite(prerequisite) if (not table.contains(self.prototype.prerequisites, _prerequisite)) then table.insert(self.prototype.prerequisites, _prerequisite) end + return self end --- Adds multiple prerequisites to the technology @@ -75,6 +78,7 @@ function Technology:addPrerequisites(prerequisites) table.insert(self.prototype.prerequisites, _prerequisite) end end + return self end --- Replaces a prerequisite in a technology @@ -88,6 +92,7 @@ function Technology:replacePrerequisite(old, new) self.prototype.prerequisites[i] = _new end end + return self end --- Removes a prerequisite from the technology @@ -97,9 +102,10 @@ function Technology:removePrerequisite(prerequisite) for i, techPrerequisite in pairs(self.prototype.prerequisites) do if techPrerequisite == _prerequisite then table.remove(self.prototype.prerequisites, i) - return + return self end end + return self end --- Adds an ingredient to the technology @@ -109,6 +115,7 @@ function Technology:addIngredient(ingredientName, amount) self.prototype.unit = self.prototype.unit or { ingredients = {} } self.prototype.unit.ingredients = self.prototype.unit.ingredients or {} table.insert(self.prototype.unit.ingredients, { ingredientName, amount or 1 }) + return self end --- Adds a list of ingredients to the technology with the default amount of 1 @@ -117,6 +124,7 @@ function Technology:addIngredients(ingredientNames) for _, ingredientName in pairs(ingredientNames) do self:addIngredient(ingredientName) end + return self end --- Removes an existing ingredient by name @@ -129,6 +137,7 @@ function Technology:removeIngredient(ingredientName) end end end + return self end --- Adds a recipe unlock to the technology @@ -136,16 +145,18 @@ end function Technology:addRecipe(recipeName) for _, effect in pairs(self.prototype.effects) do if effect.type == "unlock-recipe" and effect.recipe == recipeName then - return + return self end end table.insert(self.prototype.effects, { type = "unlock-recipe", recipe = recipeName }) + return self end --- Adds an effect to the technology --- @param effect table The effect to add function Technology:addEffect(effect) table.insert(self.prototype.effects, effect) + return self end --- Removes a recipe unlock from the technology @@ -154,9 +165,10 @@ function Technology:removeRecipe(recipeName) for i, effect in pairs(self.prototype.effects) do if effect.type == "unlock-recipe" and effect.recipe == recipeName then table.remove(self.prototype.effects, i) - return + return self end end + return self end --- Clones the technology