Myros27

inventoryItemDataDumper

May 19th, 2025 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1. -- Script: save_inventory_final.lua
  2. -- Description: Scans turtle's inventory (16 slots), saves all item details to item.json.
  3. -- Corrected pcall handling and includes full item details.
  4.  
  5. local FILENAME = "item.json"
  6. local TURTLE_INVENTORY_SIZE = 16 -- Turtles always have 16 inventory slots
  7.  
  8. local function scanInventory()
  9.     if not turtle or type(turtle.getItemDetail) ~= "function" then
  10.         printError("------------------------------------------------------")
  11.         printError("CRITICAL ERROR: Turtle API or turtle.getItemDetail is not available/functional.")
  12.         printError("This script MUST be run on a CC:Tweaked TURTLE.")
  13.         printError("------------------------------------------------------")
  14.         return nil
  15.     end
  16.  
  17.     print("Turtle API check passed. Scanning " .. TURTLE_INVENTORY_SIZE .. " inventory slots...")
  18.     local inventoryData = {}
  19.  
  20.     for slot = 1, TURTLE_INVENTORY_SIZE do
  21.         local pcallSuccess, itemDetailOrError = pcall(turtle.getItemDetail, slot)
  22.         local itemDetail = nil
  23.  
  24.         if pcallSuccess then
  25.             itemDetail = itemDetailOrError -- This is the item table or nil if slot is empty
  26.         else
  27.             printError("ERROR (scanInventory): Call to turtle.getItemDetail(" .. slot .. ") FAILED!")
  28.             printError("  pcall error: " .. tostring(itemDetailOrError))
  29.             itemDetail = nil
  30.         end
  31.  
  32.         if itemDetail then
  33.             -- Add the slot number to the itemDetail table itself before storing
  34.             itemDetail.slot = slot -- Modifies the table returned by turtle.getItemDetail
  35.  
  36.             table.insert(inventoryData, itemDetail) -- Store the entire itemDetail table
  37.             print("Slot " .. slot .. ": " .. (itemDetail.displayName or itemDetail.name or "Unknown Item") .. " x" .. (itemDetail.count or 0))
  38.         else
  39.             -- Optional: print if slot is empty or had an error
  40.             -- print("Slot " .. slot .. ": Empty or unreadable")
  41.         end
  42.     end
  43.  
  44.     if #inventoryData == 0 then
  45.         print("Inventory is empty (or no items could be read).")
  46.     end
  47.    
  48.     return inventoryData
  49. end
  50.  
  51. local function saveToFile(data, filename)
  52.     if not fs or type(fs.open) ~= "function" then
  53.         printError("Error (saveToFile): 'fs' API not available or fs.open is not a function.")
  54.         return false
  55.     end
  56.     if not textutils or type(textutils.serializeJSON) ~= "function" then
  57.         printError("Error (saveToFile): 'textutils' API not available or textutils.serializeJSON is not a function.")
  58.         return false
  59.     end
  60.  
  61.     -- Correctly handle pcall return values for textutils.serializeJSON
  62.     local serializeSuccess, result = pcall(textutils.serializeJSON, data, { compact = false })
  63.    
  64.     local actualJsonString
  65.     if serializeSuccess then
  66.         actualJsonString = result -- 'result' is the actual JSON string on success
  67.     else
  68.         -- 'result' is the error message on failure
  69.         printError("Error (saveToFile): Could not serialize data to JSON.")
  70.         printError("  pcall error: " .. tostring(result))
  71.         return false
  72.     end
  73.  
  74.     -- Proceed with writing 'actualJsonString' to the file
  75.     local file, openErr = fs.open(filename, "w")
  76.     if not file then
  77.         printError("Error (saveToFile): Opening file '" .. filename .. "' failed: " .. (openErr or "unknown error"))
  78.         return false
  79.     end
  80.  
  81.     local writeSuccess, writeErr = pcall(function() file.write(actualJsonString); file.close() end)
  82.     if not writeSuccess then
  83.         printError("Error (saveToFile): Writing to file '" .. filename .. "' failed: " .. (writeErr or "unknown error"))
  84.         pcall(function() if file and file.close then file.close() end end) -- Attempt to close on error
  85.         return false
  86.     end
  87.    
  88.     print("Inventory data successfully saved to " .. filename)
  89.     return true
  90. end
  91.  
  92. -- Main execution
  93. print("===== Script save_inventory_final.lua starting... =====")
  94.  
  95. local inventoryContents = scanInventory()
  96.  
  97. if inventoryContents == nil then
  98.     printError("------------------------------------------------------")
  99.     printError("Inventory scanning FAILED due to critical turtle API issues.")
  100.     printError("No data will be saved to " .. FILENAME .. ".")
  101.     printError("------------------------------------------------------")
  102. else
  103.     print("Inventory scanning process completed.")
  104.     if #inventoryContents > 0 then
  105.         saveToFile(inventoryContents, FILENAME)
  106.     elseif fs and fs.exists and fs.exists(FILENAME) then
  107.         print("Inventory is empty. Overwriting " .. FILENAME .. " with an empty list '[]'.")
  108.         saveToFile({}, FILENAME) -- Save an empty JSON array
  109.     elseif #inventoryContents == 0 then
  110.          print("Inventory is empty, and " .. FILENAME .. " does not exist. Not creating an empty file.")
  111.     end
  112. end
  113.  
  114. print("===== Script finished. =====")
Add Comment
Please, Sign In to add comment