保存到文件时尝试将表插入表而不创建新表

时间:2021-07-09 20:51:51

标签: lua minecraft computercraft

我正在尝试将“食谱”(表格)添加到文件中以备将来查找。 我的问题是,当我将数据上传到文件中时,它会创建一个新表并且不会作为嵌套表插入到“主”表中。当试图在主“食谱书”中搜索“食谱”时,这会导致问题。 以下是该部分项目的代码。我认为问题接近将数据上传到文件,但我很难过。有人能解释一下我到底做错了什么吗?

当我第二次运行代码时,它会毫无问题地将信息添加到主表中,但将其添加到原始表下。 当我再次运行代码时,它只会添加一个新表。

代码:

--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

结果为以下类型的表格

{
    [ "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,
    },
  }

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。

  1. 是因为我将它附加到文件中,而在将信息上传回文件时没有写入。

2) 第二个是在开始从文件中收集信息之前,以变量 Recipes 开头的无意义数组。这导致上传有另一个不需要的嵌套

相关问题