Advertisement
DubSlime

ae2-monitor v2

Apr 28th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. -- AE2 Monitor Deluxe Edition
  2. -- Light Theme + Crafting + Storage Recap + Sorted Items
  3. -- By ChatGPT
  4.  
  5. -- Find peripherals
  6. local monitor = peripheral.find("monitor")
  7. local me = peripheral.find("meBridge")
  8.  
  9. if not monitor then error("No monitor connected!") end
  10. if not me then error("No ME Bridge connected!") end
  11.  
  12. -- SETTINGS
  13. monitor.setTextScale(0.5)
  14. local w, h = monitor.getSize()
  15. local itemsPerPage = h - 8
  16. local pageCrafting = 1
  17. local pageStorage = 1
  18. local activeTab = "crafting"
  19. local blinkState = true
  20.  
  21. -- COLORS
  22. local bgColor = colors.white
  23. local textColor = colors.black
  24. local tabActive = colors.lightBlue
  25. local tabInactive = colors.gray
  26. local footerColor = colors.lightGray
  27. local busyColor = colors.blue
  28. local overloadedColor = colors.red
  29. local idleColor = colors.green
  30.  
  31. -- UTILITIES
  32. local function centerText(text, y, textCol, bgCol)
  33. monitor.setCursorPos(math.floor((w - #text) / 2) + 1, y)
  34. monitor.setTextColor(textCol or textColor)
  35. monitor.setBackgroundColor(bgCol or bgColor)
  36. monitor.write(text)
  37. end
  38.  
  39. local function drawBlock(x1, y1, x2, y2, bg)
  40. monitor.setBackgroundColor(bg)
  41. for y = y1, y2 do
  42. monitor.setCursorPos(x1, y)
  43. monitor.write(string.rep(" ", x2 - x1 + 1))
  44. end
  45. end
  46.  
  47. local function drawButton(x1, x2, label, enabled)
  48. monitor.setBackgroundColor(enabled and colors.lightBlue or colors.gray)
  49. local mid = math.floor((x1 + x2) / 2) - math.floor(#label / 2)
  50. monitor.setCursorPos(mid, h)
  51. monitor.setTextColor(colors.black)
  52. monitor.write(label)
  53. end
  54.  
  55. local function drawTabs()
  56. local half = math.floor(w / 2)
  57. drawBlock(1, 1, w, 2, footerColor)
  58.  
  59. -- Left tab
  60. drawBlock(1, 1, half, 2, activeTab == "crafting" and tabActive or tabInactive)
  61. centerText("Crafting", 1, colors.white, activeTab == "crafting" and tabActive or tabInactive)
  62.  
  63. -- Right tab
  64. drawBlock(half + 1, 1, w, 2, activeTab == "storage" and tabActive or tabInactive)
  65. monitor.setCursorPos(half + math.floor((half - 7) / 2), 1)
  66. monitor.setTextColor(colors.white)
  67. monitor.write("Storage")
  68. end
  69.  
  70. local function drawProgressBar(x, y, width, percentage, fgColor, bgColor)
  71. local fillWidth = math.floor(width * percentage + 0.5)
  72. monitor.setCursorPos(x, y)
  73. monitor.setBackgroundColor(fgColor)
  74. monitor.write(string.rep(" ", fillWidth))
  75. monitor.setBackgroundColor(bgColor)
  76. monitor.write(string.rep(" ", width - fillWidth))
  77. end
  78.  
  79. local function drawCraftingTab()
  80. monitor.setBackgroundColor(bgColor)
  81. monitor.clear()
  82. drawTabs()
  83. centerText("Ongoing Crafting Jobs", 4, textColor, bgColor)
  84.  
  85. local cpus = me.getCraftingCPUs() or {}
  86. local totalPages = math.max(1, math.ceil(#cpus / itemsPerPage))
  87. pageCrafting = math.min(pageCrafting, totalPages)
  88.  
  89. local startIdx = (pageCrafting - 1) * itemsPerPage + 1
  90. local endIdx = math.min(#cpus, startIdx + itemsPerPage - 1)
  91.  
  92. local y = 6
  93. for idx = startIdx, endIdx do
  94. local cpu = cpus[idx]
  95. if cpu then
  96. local name = cpu.name or ("CPU #" .. idx)
  97. if name == "" then name = "Unnamed CPU" end
  98. local busy = cpu.isBusy
  99.  
  100. local cpuColor = idleColor
  101. if busy then
  102. cpuColor = blinkState and busyColor or overloadedColor
  103. end
  104.  
  105. drawBlock(2, y, w - 1, y + 2, footerColor)
  106. monitor.setCursorPos(4, y + 1)
  107. monitor.setTextColor(textColor)
  108. monitor.write(name:sub(1, w - 15))
  109.  
  110. -- Simulate CPU usage
  111. local usage = busy and 0.6 or 0.1 -- fake usage %
  112. monitor.setCursorPos(w - 15, y + 1)
  113. drawProgressBar(w - 15, y + 1, 13, usage, cpuColor, colors.gray)
  114.  
  115. y = y + 4
  116. end
  117. end
  118.  
  119. drawBlock(1, h, w, h, footerColor)
  120. drawButton(1, math.floor(w / 2) - 1, "< Prev", pageCrafting > 1)
  121. drawButton(math.floor(w / 2) + 1, w, "Next >", pageCrafting < totalPages)
  122. centerText("Page " .. pageCrafting .. "/" .. totalPages, h - 1, textColor, footerColor)
  123. end
  124.  
  125. local function sortByAmountDesc(a, b)
  126. return (a.amount or 0) > (b.amount or 0)
  127. end
  128.  
  129. local function drawStorageTab()
  130. monitor.setBackgroundColor(bgColor)
  131. monitor.clear()
  132. drawTabs()
  133. centerText("Storage Overview", 4, textColor, bgColor)
  134.  
  135. local storage = me.listItems() or {}
  136. table.sort(storage, sortByAmountDesc)
  137.  
  138. local totalPages = math.max(1, math.ceil(#storage / itemsPerPage))
  139. pageStorage = math.min(pageStorage, totalPages)
  140.  
  141. local startIdx = (pageStorage - 1) * itemsPerPage + 1
  142. local endIdx = math.min(#storage, startIdx + itemsPerPage - 1)
  143.  
  144. local y = 6
  145. for idx = startIdx, endIdx do
  146. local item = storage[idx]
  147. local name = item.displayName or "Unknown"
  148. local amount = item.amount or 0
  149.  
  150. if idx - startIdx < 5 then
  151. -- Top 5 highlighted
  152. drawBlock(2, y, w - 1, y, colors.lightBlue)
  153. monitor.setTextColor(colors.white)
  154. else
  155. monitor.setBackgroundColor(bgColor)
  156. monitor.setTextColor(textColor)
  157. end
  158.  
  159. monitor.setCursorPos(3, y)
  160. monitor.write(name:sub(1, 20))
  161.  
  162. monitor.setCursorPos(w - 10, y)
  163. monitor.write("x" .. amount)
  164.  
  165. y = y + 1
  166. end
  167.  
  168. drawBlock(1, h, w, h, footerColor)
  169. drawButton(1, math.floor(w / 2) - 1, "< Prev", pageStorage > 1)
  170. drawButton(math.floor(w / 2) + 1, w, "Next >", pageStorage < totalPages)
  171. centerText("Page " .. pageStorage .. "/" .. totalPages, h - 1, textColor, footerColor)
  172. end
  173.  
  174. local function handleTouch(x, y)
  175. x = tonumber(x)
  176. if not x then return end
  177.  
  178. local half = math.floor(w / 2)
  179. if y <= 2 then
  180. if x <= half then
  181. activeTab = "crafting"
  182. pageCrafting = 1
  183. else
  184. activeTab = "storage"
  185. pageStorage = 1
  186. end
  187. elseif y == h then
  188. if activeTab == "crafting" then
  189. local cpus = me.getCraftingCPUs() or {}
  190. if x < w / 2 and pageCrafting > 1 then
  191. pageCrafting = pageCrafting - 1
  192. elseif x >= w / 2 and pageCrafting < math.ceil(#cpus / itemsPerPage) then
  193. pageCrafting = pageCrafting + 1
  194. end
  195. elseif activeTab == "storage" then
  196. local storage = me.listItems() or {}
  197. if x < w / 2 and pageStorage > 1 then
  198. pageStorage = pageStorage - 1
  199. elseif x >= w / 2 and pageStorage < math.ceil(#storage / itemsPerPage) then
  200. pageStorage = pageStorage + 1
  201. end
  202. end
  203. end
  204. end
  205.  
  206. -- MAIN LOOP
  207. parallel.waitForAny(
  208. function()
  209. while true do
  210. blinkState = not blinkState
  211. if activeTab == "crafting" then
  212. drawCraftingTab()
  213. elseif activeTab == "storage" then
  214. drawStorageTab()
  215. end
  216. sleep(0.5) -- faster refresh
  217. end
  218. end,
  219. function()
  220. while true do
  221. local _, side, x, y = os.pullEvent("monitor_touch")
  222. handleTouch(x, y)
  223. -- Immediately refresh
  224. if activeTab == "crafting" then
  225. drawCraftingTab()
  226. elseif activeTab == "storage" then
  227. drawStorageTab()
  228. end
  229. end
  230. end
  231. )
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement