zamoth

nbt reader

Jul 8th, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. -- 1) Wrap your Block Reader (here on the “back” of the computer)
  2. local reader = peripheral.wrap("back")
  3. if not reader or peripheral.getType("back") ~= "blockReader" then
  4.   error("Block Reader not found on back")
  5. end
  6.  
  7. -- 2) Grab the full chest NBT
  8. local data = reader.getBlockData()
  9. if not data or type(data.Items) ~= "table" then
  10.   error("No chest data found")
  11. end
  12.  
  13. -- 3) Helper to recursively dump a table
  14. local function dump(t, indent)
  15.   indent = indent or ""
  16.   for k, v in pairs(t) do
  17.     if type(v) == "table" then
  18.       print(indent .. tostring(k) .. ":")
  19.       dump(v, indent .. "  ")
  20.     else
  21.       print(indent .. tostring(k) .. " = " .. tostring(v))
  22.     end
  23.   end
  24. end
  25.  
  26. -- 4) Iterate your slots
  27. for _, item in ipairs(data.Items) do
  28.   print(string.format("Slot %d - %s x%d", item.Slot, item.id, item.Count))
  29.  
  30.   -- 5) Dump full tag to inspect nutrition/expiration fields
  31.   if item.tag then
  32.     print("  Full NBT tag for this stack:")
  33.     dump(item.tag, "    ")
  34.   else
  35.     print("  No tag data present on this item.")
  36.   end
  37.  
  38.   -- 6) Once you know the path, you can extract directly:
  39.   --[[
  40.   local food = item.tag.FoodData
  41.   if food then
  42.     print("  Nutrition: " .. tostring(food.Nutrition))
  43.     print("  Expiration (ticks): " .. tostring(food.Expiration))
  44.   end
  45.   ]]
  46.  
  47.   print("---")
  48. end
  49.  
Add Comment
Please, Sign In to add comment