GrunclePug

startup

Apr 30th, 2022
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. -- Program that monitors ME storage and outputs
  2. -- to a display
  3. -- Author: moomoocat28
  4.  
  5. -- Initialization
  6. local ME = peripheral.wrap("back") -- which side the ME bridge is connected
  7. local monitor = peripheral.wrap("bottom") -- which side the monitors are connected
  8. local minAmount = 100 -- minimum quantity for an item to be displayed
  9. local space = 8 -- i honestly dont remember what this does
  10.  
  11. monX, monY = monitor.getSize()
  12. local title = "ME Storage Monitor"
  13. local lineBreak = ""
  14. local tableTitle = "Quantity   Item"
  15. for x = 0, monX, 1 do
  16.     lineBreak = lineBreak .. "-"
  17. end
  18.  
  19. while true do
  20.  
  21. local outputTable = ME.listAll()
  22. local testTable
  23.  
  24. -- Functions
  25. function loadTable()
  26.    
  27. end
  28.  
  29. function getTotalItems()
  30.     local count = 0
  31.     for i, data in ipairs(ME.listAll()) do
  32.         for k, v in pairs(data) do
  33.             if k ~= nil then
  34.                 if k == "amount" then
  35.                     count = count + v
  36.                 end
  37.             end
  38.         end
  39.     end
  40.     return count
  41. end
  42.  
  43. function centerText(text)
  44.     for x = 1, math.floor((monX - string.len(text)) / 2), 1 do
  45.         monitor.write(" ")
  46.     end
  47. end
  48.  
  49. function reformatInt(int)
  50.     local formatted = int
  51.     while true do
  52.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2")
  53.         if k == 0 then
  54.             break
  55.         end
  56.     end
  57.     return formatted
  58. end
  59.  
  60. function printTable()
  61.     local count = 1
  62.     local line = 9
  63.     local length = 0
  64.     local spaces = ""
  65.     for i, data in ipairs(outputTable) do
  66.     table.sort(data)
  67.         for k, v in pairs(data) do
  68.             if k~= nil then
  69.                 if k == "amount" and v > minAmount then
  70.                     length = string.len(reformatInt(v))
  71.                     monitor.setCursorPos(space / 2, line)
  72.                     monitor.write("x" .. reformatInt(v))
  73.                 end
  74.                 if k == "displayName" and length ~= 0 then
  75.                     spaces = ""
  76.                     for x = 0, space - length, 1 do
  77.                         spaces = spaces .. " "
  78.                     end
  79.                     monitor.write(spaces .. reformatInt(v))
  80.                     line = line + 1
  81.                     length = 0
  82.                 end
  83.             end
  84.         end
  85.     end
  86. end
  87.  
  88. function compare(a, b)
  89.     if a[1] and b[1] ~= nil then
  90.         return a[1][2] < b[1][2]
  91.     end
  92. end
  93.  
  94. -- Program
  95. monitor.clear()
  96. monitor.setTextColour(colors.white)
  97. monitor.setCursorPos(1, 1)
  98. centerText(title)
  99. monitor.write(title)
  100. monitor.setCursorPos(1, 2)
  101. monitor.write(lineBreak)
  102. monitor.setCursorPos(1, 4)
  103. monitor.setTextColour(colors.red)
  104. monitor.write("Items Stored: ")
  105. monitor.setTextColour(colors.green)
  106. monitor.write(reformatInt(getTotalItems()))
  107. monitor.setTextColour(colors.white)
  108. monitor.setCursorPos(1, 7)
  109. centerText(tableTitle)
  110. monitor.write(tableTitle)
  111. monitor.setCursorPos(1, 9)
  112. monitor.setTextColour(colors.green)
  113. table.sort(outputTable, compare)
  114. printTable()
  115. monitor.setCursorPos(1, monY)
  116. for x = 0, monX, 1 do
  117.     monitor.write(" ")
  118. end
  119. sleep(1)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment