I am attempting to add a "recipe" (Table) into a file for future look up. My issue is that when I upload the data into the file, it creates a new table and does not insert into the "Main" table as a nested table. This causes issues when trying to search for a "recipe" within the main "recipe book". The following is the code for this section of the project. The issue I believe is near the uploading of the data to a file but I am stumped. Can someone explain what the heck I am doing wrong?
When I run the code a second time it adds in the information into the main table without issue, but it adds it under the original table. When I run the code any more times it just adds in a new table.
Code:
--recursive search in--recursive search into tables
local Deep = require "DeepPrint"
--Working from the following info
--http://www.computercraft.info/forums2/index.php?/topic/23076-crafting-api-learn-recipes/
--the solution found: https://stackoverflow.com/questions/19299162/lua-table-expected-got-nilo
--variables--
--data holding place for recipes
Recipes = {}
--Crafting Grid
grid = {1,2,3,5,6,7,9,10,11};
--check for recipe book, if exists then read the data
local readRecipe = fs.open("recipeBook.lua","r")
if readRecipe == nil then
print("recipe book not found")
else
recipeList = readRecipe.readAll()
readRecipe.close()
ok, Recipes = pcall(textutils.unserialize, recipeList)
if not ok then
return false, recipeList
end
if type(Recipes) ~= "table" then
print("Main table not found, creating base table")
Recipes = {}
end
end
if readRecipe == nil then
print("recipe book not found")
else
recipeList = readRecipe.readAll()
readRecipe.close()
ok, Recipes = pcall(textutils.unserialize, recipeList)
if not ok then
return false, recipeList
end
if type(Recipes) ~= "table" then
print("Main table not found, creating base table")
Recipes = {}
end
end
--get recipe location and details to push into recipeBook.lua
itemLayout = {}
for key,value in pairs(grid) do
turtle.select(value)
if turtle.getItemDetail() then
details = turtle.getItemDetail()
table.insert(itemLayout,{
name = details.name,
count = details.count,
location = value
})
end
end
--craft item to get the name of crafted item
turtle.select(16)
turtle.craft()
itemMade = turtle.getItemDetail()
--check if table exists and if not creates a table
if not Recipes[itemMade.name] then
print("didnt find the table for item, creating new table")
Recipes[itemMade.name] = {}
else
print("found recipe")
end
--add in info into main product table
Recipes[itemMade.name]["components"] = itemLayout
Recipes[itemMade.name]["quantityMade"] = itemMade.count
--add recipe to list
local ok, str = pcall(textutils.serialize, Recipes)
if not ok then
return false, str
else
book = fs.open("recipeBook.lua","a")
book.writeLine(str)
book.close()
end
Results in the following type of table
{
[ "minecraft:crafting_table" ] = {
components = {
{
location = 1,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 2,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 5,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 6,
name = "minecraft:oak_planks",
count = 1,
},
},
quantityMade = 1,
},
}
{
[ "minecraft:oak_button" ] = {
components = {
{
location = 1,
name = "minecraft:oak_planks",
count = 1,
},
},
quantityMade = 1,
},
[ "minecraft:crafting_table" ] = {
components = {
{
location = 1,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 2,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 5,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 6,
name = "minecraft:oak_planks",
count = 1,
},
},
quantityMade = 1,
},
}
{
[ "minecraft:oak_button" ] = {
components = {
{
location = 1,
name = "minecraft:oak_planks",
count = 1,
},
},
quantityMade = 1,
},
}
{
[ "minecraft:oak_pressure_plate" ] = {
components = {
{
location = 1,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 2,
name = "minecraft:oak_planks",
count = 1,
},
},
quantityMade = 1,
},
}
{
[ "minecraft:crafting_table" ] = {
components = {
{
location = 1,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 2,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 5,
name = "minecraft:oak_planks",
count = 1,
},
{
location = 6,
name = "minecraft:oak_planks",
count = 1,
},
},
quantityMade = 1,
},
}