Advertisement
DubSlime

ae2-crafting-monitor

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