Eldskogen

Untitled

Mar 11th, 2025
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. local meBridge = peripheral.wrap("left") -- Connect to ME Bridge on the left side
  2. local monitor = peripheral.find("monitor") -- Find a connected monitor
  3.  
  4. if not meBridge then
  5. print("Error: ME Bridge not found on the left side!")
  6. return
  7. else
  8. print("ME Bridge successfully detected!")
  9. end
  10.  
  11. if not monitor then
  12. print("Error: No monitor found!")
  13. return
  14. else
  15. print("Monitor successfully detected!")
  16. monitor.setTextScale(1) -- Set text scale for better visibility
  17. end
  18.  
  19. -- Function to safely print table contents without serialization issues
  20. function printTable(tbl, indent)
  21. indent = indent or ""
  22. for k, v in pairs(tbl) do
  23. if type(v) == "table" then
  24. print(indent .. tostring(k) .. " = {")
  25. printTable(v, indent .. " ")
  26. print(indent .. "}")
  27. else
  28. print(indent .. tostring(k) .. " = " .. tostring(v))
  29. end
  30. end
  31. end
  32.  
  33. -- Function to calculate total item count safely
  34. function getTotalItemCount()
  35. local items = meBridge.listItems()
  36. if not items or type(items) ~= "table" then
  37. print("No items detected in ME system or invalid data!")
  38. return 0
  39. end
  40.  
  41. local total = 0
  42. for _, item in pairs(items) do
  43. if type(item) == "table" and (item.amount or item.count) then
  44. total = total + (item.amount or item.count or 0)
  45. else
  46. print("Warning: Skipping invalid item entry:")
  47. printTable(item, " ")
  48. end
  49. end
  50. return total
  51. end
  52.  
  53. -- Function to update the monitor display
  54. function updateMonitor(total)
  55. monitor.clear()
  56. monitor.setCursorPos(1, 1)
  57. monitor.write("Total Items in ME System:")
  58. monitor.setCursorPos(1, 3)
  59. monitor.write(tostring(total))
  60. end
  61.  
  62. -- Debugging: Print the list of items in a safe way
  63. print("Debug: Raw ME System Data")
  64. local items = meBridge.listItems()
  65. if items and type(items) == "table" then
  66. printTable(items)
  67. else
  68. print("Invalid or empty item list.")
  69. end
  70.  
  71. -- Continuously update the monitor and print to terminal
  72. while true do
  73. local totalCount = getTotalItemCount()
  74. print("Total Items in ME System: " .. tostring(totalCount))
  75. updateMonitor(totalCount)
  76. sleep(5) -- Updates every 5 seconds
  77. end
  78.  
Advertisement
Add Comment
Please, Sign In to add comment