Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script: save_inventory_final.lua
- -- Description: Scans turtle's inventory (16 slots), saves all item details to item.json.
- -- Corrected pcall handling and includes full item details.
- local FILENAME = "item.json"
- local TURTLE_INVENTORY_SIZE = 16 -- Turtles always have 16 inventory slots
- local function scanInventory()
- if not turtle or type(turtle.getItemDetail) ~= "function" then
- printError("------------------------------------------------------")
- printError("CRITICAL ERROR: Turtle API or turtle.getItemDetail is not available/functional.")
- printError("This script MUST be run on a CC:Tweaked TURTLE.")
- printError("------------------------------------------------------")
- return nil
- end
- print("Turtle API check passed. Scanning " .. TURTLE_INVENTORY_SIZE .. " inventory slots...")
- local inventoryData = {}
- for slot = 1, TURTLE_INVENTORY_SIZE do
- local pcallSuccess, itemDetailOrError = pcall(turtle.getItemDetail, slot)
- local itemDetail = nil
- if pcallSuccess then
- itemDetail = itemDetailOrError -- This is the item table or nil if slot is empty
- else
- printError("ERROR (scanInventory): Call to turtle.getItemDetail(" .. slot .. ") FAILED!")
- printError(" pcall error: " .. tostring(itemDetailOrError))
- itemDetail = nil
- end
- if itemDetail then
- -- Add the slot number to the itemDetail table itself before storing
- itemDetail.slot = slot -- Modifies the table returned by turtle.getItemDetail
- table.insert(inventoryData, itemDetail) -- Store the entire itemDetail table
- print("Slot " .. slot .. ": " .. (itemDetail.displayName or itemDetail.name or "Unknown Item") .. " x" .. (itemDetail.count or 0))
- else
- -- Optional: print if slot is empty or had an error
- -- print("Slot " .. slot .. ": Empty or unreadable")
- end
- end
- if #inventoryData == 0 then
- print("Inventory is empty (or no items could be read).")
- end
- return inventoryData
- end
- local function saveToFile(data, filename)
- if not fs or type(fs.open) ~= "function" then
- printError("Error (saveToFile): 'fs' API not available or fs.open is not a function.")
- return false
- end
- if not textutils or type(textutils.serializeJSON) ~= "function" then
- printError("Error (saveToFile): 'textutils' API not available or textutils.serializeJSON is not a function.")
- return false
- end
- -- Correctly handle pcall return values for textutils.serializeJSON
- local serializeSuccess, result = pcall(textutils.serializeJSON, data, { compact = false })
- local actualJsonString
- if serializeSuccess then
- actualJsonString = result -- 'result' is the actual JSON string on success
- else
- -- 'result' is the error message on failure
- printError("Error (saveToFile): Could not serialize data to JSON.")
- printError(" pcall error: " .. tostring(result))
- return false
- end
- -- Proceed with writing 'actualJsonString' to the file
- local file, openErr = fs.open(filename, "w")
- if not file then
- printError("Error (saveToFile): Opening file '" .. filename .. "' failed: " .. (openErr or "unknown error"))
- return false
- end
- local writeSuccess, writeErr = pcall(function() file.write(actualJsonString); file.close() end)
- if not writeSuccess then
- printError("Error (saveToFile): Writing to file '" .. filename .. "' failed: " .. (writeErr or "unknown error"))
- pcall(function() if file and file.close then file.close() end end) -- Attempt to close on error
- return false
- end
- print("Inventory data successfully saved to " .. filename)
- return true
- end
- -- Main execution
- print("===== Script save_inventory_final.lua starting... =====")
- local inventoryContents = scanInventory()
- if inventoryContents == nil then
- printError("------------------------------------------------------")
- printError("Inventory scanning FAILED due to critical turtle API issues.")
- printError("No data will be saved to " .. FILENAME .. ".")
- printError("------------------------------------------------------")
- else
- print("Inventory scanning process completed.")
- if #inventoryContents > 0 then
- saveToFile(inventoryContents, FILENAME)
- elseif fs and fs.exists and fs.exists(FILENAME) then
- print("Inventory is empty. Overwriting " .. FILENAME .. " with an empty list '[]'.")
- saveToFile({}, FILENAME) -- Save an empty JSON array
- elseif #inventoryContents == 0 then
- print("Inventory is empty, and " .. FILENAME .. " does not exist. Not creating an empty file.")
- end
- end
- print("===== Script finished. =====")
Add Comment
Please, Sign In to add comment