Factorio Standard Library

by Nexela

The Factorio Standard Library is a project to bring Factorio modders high-quality, commonly-required utilities and tools.

Internal
1 year, 4 months ago
0.17 - 1.1
366K

b add ingredient functions Fix

4 years ago
(updated 4 years ago)

i have 2 Mod packs installed, Bobs Mods & Pyanodons Mods!

Factorio Start Error:
Zitat from Factorio Screen printet in the "factorio-current.log" File:
53.494 Mods to disable:Fehler beim Laden der Mods: Error while running setup for recipe prototype "advanced-circuit" (recipe): Duplicate item ingredients are not allowed (solder exists 2 or more times).

Mods, die deaktiviert werden müssen:
• bobelectronics
• pyhightech
• bobassembly
• bobelectronics
• pyhightech
• pyrawores

My Analyse for the "advanced-circuit" recipe:
My "data-updates.lua":

if mods["pyhightech"]
and mods["bobelectronics"] -- and mods["bobassembly"] and mods["pyrawores"]
and data.raw.recipe["advanced-circuit"]
then

TableInfo = {}
TableInfo[" pyhightech "] = " is ok "
TableInfo[" bobelectronics "] = " is ok "
--TableInfo[" bobassembly "] = " is ok "
--TableInfo[" pyrawores "] = " is ok "

if data.raw.recipe["advanced-circuit"].ingredients
then
    TableInfo[" advanced-circuit.ingredients "] = data.raw.recipe["advanced-circuit"].ingredients
else
    TableInfo[" advanced-circuit.ingredients "] = " false "
end

if data.raw.recipe["advanced-circuit"].normal
    and data.raw.recipe["advanced-circuit"].normal.ingredients
then
    TableInfo[" advanced-circuit.normal.ingredients "] = data.raw.recipe["advanced-circuit"].normal.ingredients
else
    TableInfo[" advanced-circuit.normal.ingredients "] = " false "
end
if data.raw.recipe["advanced-circuit"].expensive
    and data.raw.recipe["advanced-circuit"].expensive.ingredients
then
    TableInfo[" advanced-circuit.expensive.ingredients "] = data.raw.recipe["advanced-circuit"].expensive.ingredients
else
    TableInfo[" advanced-circuit.expensive.ingredients "] = " false "
end

--Stop Factorio, and print all Tables in This on the Error scren
    if require('__---Jonnys-debug__/Data.lua') then
        JoErrRep({ AnalyseFunktion = "true", TableInfo } )
    else 
        data:extend({  {{ AnalyseFunktion = "false", TableInfo }}  })
    end
--Stop Factorio

end

This Code Printet this in the "factorio-current.log" file:
40.401 ................
{
{
{
[" advanced-circuit.expensive.ingredients "] = " false ",
[" advanced-circuit.ingredients "] = {
{ type = "item", amount = 1, name = "pcb2" },
{ type = "item", amount = 1, name = "electronic-circuit" },
{ type = "item", amount = 3, name = "transistor" },
{ type = "item", amount = 3, name = "microchip" },
{ type = "item", amount = 4, name = "inductor2" },
{ type = "item", amount = 3, name = "diode" },
{ type = "item", amount = 5, name = "capacitor2" },
{ type = "item", amount = 15, name = "resistor2" },
{ type = "item", amount = 1, name = "solder" },
{ type = "item", amount = 2, name = "optical-fiber" },
{ type = "item", amount = 2, name = "solder" }
},
[" advanced-circuit.normal.ingredients "] = " false ",
[" bobelectronics "] = " is ok ",
[" pyhightech "] = " is ok "
},
AnalyseFunktion = "true"
}
}
stack traceback:
[C]: in function 'error'
core/lualib/dataloader.lua:18: in function 'extend'
---Jonnys-debug/Data.lua:37: in function 'JoErrRep'
Py-Fixes-by-1Jonny/data-updates.lua:37: in main chunk

"advanced-circuit":
"Data.lua":
1. Vanilla Createt data.raw.recipe["advanced-circuit"].
2. bob Createt data.raw.recipe["advanced-circuit"]
3. Py Createt data.raw.recipe["advanced-circuit"]
--the First 8 Items in "data.raw.recipe['advanced-circuit'].ingredients" is createt by Py in the "Data.lua".
"data-updates.lua":
4. Bob Updatet "data.raw.recipe['advanced-circuit'].ingredients" by "solder"
5. Py Updatet "data.raw.recipe['advanced-circuit'].ingredients" by your:
--RECIPE("advanced-circuit"):add_ingredient({type = "item", name = "solder", amount = 2}) --py

Bobs Mods have the "boblibrary", and Py- Mods have your "stdlib".

Please Fix the "add_ingredient" Function, and add to the "RECIPE():add_ingredient()" Function:
"if data.raw.recipe['advanced-circuit'].ingredients.name == "solder" then
Update by name: "solder"
else
Add: "solder"
end

Please fix the add Function for Bob and Py User, and all another Mods white your stlib and all multi-moduser.

thanks :D
from Jonny

4 years ago

or a simple Version: "Return false"

RECIPE("advanced-circuit"):add_ingredient("solder")
-- and a For Loop for the ingredients
if data.raw.recipe['advanced-circuit'].ingredients[s].name == "solder"
or data.raw.recipe['advanced-circuit'].normal.ingredients[s].name == "solder"
or data.raw.recipe['advanced-circuit'].expensive.ingredients[s].name == "solder"
then
-- 1. no Function
-- or 2. Return false
else
recipe_add_ingredient( "advanced-circuit", "solder" )
end

4 years ago

ok, i have fixed your Mod Function: RECIPE():add_ingredient()
"---stdlib_1.3.0/stdlib/data/recipe.lua"

your New Lua Code Function + 2 New Functions:

-- Function 3
-- find Name of ingredient[N]
function ingredientNumber_returnName(iN)
if iN.name then
-- { type = "item" amount = 5, name = "ItamName" }
return iN.name -- iN.name = "ItamName"
elseif iN[1] then
-- { "ItamName", 5 }
return iN[1] -- iN[1] = "ItamName"
elseif iN then
-- "ItamName"
return iN -- iN = "ItamName"
else
return false -- no name found
end
end

-- Function 2
-- if esist ingredient[New] on ingredients[]
function ingredientIfExist(ingredients,NewIngredient)
a = 1
for s=#ingredients,1,-1 do
if ingredients[s] then
thisName = ingredientNumber_returnName(ingredients[s])
if thisName == ingredientNumber_returnName(NewIngredient) then
s = 0
a = 0
end
end
end
if a == 1 then
if thisName == false then
ingredients = false
else
ingredients[#ingredients + 1] = NewIngredient
end
else
ingredients = false
end
return ingredients
end

-- Your Fixed Function 1
--- Add an ingredient to a recipe.
-- @tparam string|Concepts.ingredient normal
-- @tparam[opt] string|Concepts.ingredient|boolean expensive
-- @treturn Recipe
function Recipe:add_ingredient(normal, expensive)
if self:is_valid() then
normal, expensive = get_difficulties(normal, expensive)
if self.normal then
if normal then
ingredients = self.normal.ingredients or {}
ingredients = ingredientIfExist( ingredients, normal )
if ingredients then
self.normal.ingredients = ingredients
else
self = false
end
end
if expensive then
ingredients = self.expensive.ingredients or {}
ingredients = ingredientIfExist( ingredients, expensive )
if ingredients then
self.expensive.ingredients = ingredients
else
self = false
end
end
elseif normal then
ingredients = self.ingredients or {}
ingredients = ingredientIfExist( ingredients, normal )
if ingredients then
self.ingredients = ingredients
else
self = false
end
end
end
return self
end
Recipe.add_ing = Recipe.add_ingredient

Please Updete your Mod
Bob and Py is Compatible :D

MfG Jonny

4 years ago

I will be taking a look at this (as well as finishing recipe stuff) in the very near future.

3 years ago
(updated 3 years ago)

Rather than having two distinct functions for adding or replacing ingredients, have add try to call the replace function for the ingredient, and only add if it didn't succeed.

3 years ago

Since nothing was done about it, I added a pull request with a simple fix: call the remove_ingredient function while calling the add_ingredient.

2 years ago

I will try and look at this this weekend

2 years ago

My pull request was denied, but you can probably still see it.

1 year, 10 months ago

This mod is now in active development again, how is this still a thing...

New response