Advertisement
TheYellowBush

ae2_display.lua

Jul 8th, 2025 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.39 KB | None | 0 0
  1. -- AE2 Stockpile Manager - Display Module
  2.  
  3. local display = {}
  4.  
  5. -- Private variables
  6. local monitor = nil
  7.  
  8. -- Helper function for formatted printing
  9. local function printf(format, ...)
  10.     print(string.format(format, ...))
  11. end
  12.  
  13. -- Draw a colored progress bar
  14. local function drawProgressBar(monitor, x, y, width, current, target, threshold)
  15.     monitor.setCursorPos(x, y)
  16.    
  17.     local percentage = math.min(current / target, 1.0)
  18.     local filledWidth = math.floor(percentage * width)
  19.    
  20.     -- Choose color based on status
  21.     local barColor = colors.red
  22.     if current >= target then
  23.         barColor = colors.lime
  24.     elseif current >= threshold then
  25.         barColor = colors.yellow
  26.     end
  27.    
  28.     -- Draw filled portion
  29.     monitor.setBackgroundColor(barColor)
  30.     monitor.setTextColor(colors.white)
  31.     for i = 1, filledWidth do
  32.         monitor.write(" ")
  33.     end
  34.    
  35.     -- Draw empty portion
  36.     monitor.setBackgroundColor(colors.gray)
  37.     monitor.setTextColor(colors.white)
  38.     for i = filledWidth + 1, width do
  39.         monitor.write(" ")
  40.     end
  41.    
  42.     -- Reset background
  43.     monitor.setBackgroundColor(colors.black)
  44. end
  45.  
  46. -- Initialize monitor/display
  47. function display.initialize()
  48.     local foundMonitor = peripheral.find("monitor")
  49.     if foundMonitor then
  50.         monitor = foundMonitor
  51.         monitor.clear()
  52.         monitor.setTextScale(0.5)
  53.         print("Monitor connected")
  54.         return true
  55.     else
  56.         print("No monitor found (optional)")
  57.         return false
  58.     end
  59. end
  60.  
  61. -- Check if monitor is available
  62. function display.hasMonitor()
  63.     return monitor ~= nil
  64. end
  65.  
  66. -- Update monitor display
  67. function display.update(settings, currentCraftingJobs, running, quantities, systemStatus)
  68.     if not monitor then return end
  69.    
  70.     monitor.clear()
  71.     monitor.setCursorPos(1, 1)
  72.     monitor.setBackgroundColor(colors.black)
  73.    
  74.     -- Header with gradient effect
  75.     monitor.setTextColor(colors.cyan)
  76.     monitor.write("AE2 Stockpile Manager")
  77.     monitor.setCursorPos(1, 2)
  78.     monitor.setTextColor(colors.lightBlue)
  79.     monitor.write("Last Update: " .. os.date("%H:%M:%S"))
  80.    
  81.     local line = 4
  82.    
  83.     -- System Status Section
  84.     monitor.setCursorPos(1, line)
  85.     monitor.setTextColor(colors.orange)
  86.     monitor.write("SYSTEM STATUS")
  87.     line = line + 1
  88.    
  89.     monitor.setCursorPos(1, line)
  90.     if systemStatus == "online" then
  91.         monitor.setTextColor(colors.lime)
  92.         monitor.write("[ONLINE] ME Bridge Active")
  93.     elseif systemStatus == "test" then
  94.         monitor.setTextColor(colors.yellow)
  95.         monitor.write("[TEST] Simulation Mode")
  96.     else
  97.         monitor.setTextColor(colors.red)
  98.         monitor.write("[OFFLINE] No Connection")
  99.     end
  100.     line = line + 2
  101.    
  102.     -- Current Crafting Jobs Section
  103.     monitor.setCursorPos(1, line)
  104.     monitor.setTextColor(colors.orange)
  105.     monitor.write("ACTIVE CRAFTING JOBS")
  106.     line = line + 1
  107.    
  108.     if #currentCraftingJobs > 0 then
  109.         for i, job in ipairs(currentCraftingJobs) do
  110.             if line <= 12 then -- Leave more room for items
  111.                 monitor.setCursorPos(1, line)
  112.                 monitor.setTextColor(colors.cyan)
  113.                 monitor.write("> ")
  114.                 monitor.setTextColor(colors.white)
  115.                 local itemName = job.item:gsub("minecraft:", ""):gsub("_", " ")
  116.                 monitor.write(job.amount .. "x " .. itemName)
  117.                 line = line + 1
  118.             end
  119.         end
  120.     else
  121.         monitor.setCursorPos(1, line)
  122.         monitor.setTextColor(colors.gray)
  123.         monitor.write("No active jobs")
  124.         line = line + 1
  125.     end
  126.     line = line + 1
  127.    
  128.     -- Monitored Items Section
  129.     monitor.setCursorPos(1, line)
  130.     monitor.setTextColor(colors.orange)
  131.     monitor.write("MONITORED ITEMS")
  132.     line = line + 1
  133.    
  134.     if #settings > 0 then
  135.         local maxY = select(2, monitor.getSize())
  136.        
  137.         for i, itemConfig in ipairs(settings) do
  138.             if line <= maxY - 2 then -- Leave room for footer
  139.                 local currentAmount = quantities[itemConfig.item] or 0
  140.                 local itemName = itemConfig.item:gsub("minecraft:", ""):gsub("_", " ")
  141.                
  142.                 monitor.setCursorPos(1, line)
  143.                
  144.                 -- Status indicator
  145.                 if currentAmount >= itemConfig.target then
  146.                     monitor.setTextColor(colors.lime)
  147.                     monitor.write("[OK] ")
  148.                 elseif currentAmount >= itemConfig.threshold then
  149.                     monitor.setTextColor(colors.yellow)
  150.                     monitor.write("[LOW]")
  151.                 else
  152.                     monitor.setTextColor(colors.red)
  153.                     monitor.write("[OUT]")
  154.                 end
  155.                
  156.                 -- Item name (longer names)
  157.                 monitor.setTextColor(colors.white)
  158.                 monitor.write(" " .. itemName:sub(1, 18))
  159.                
  160.                 -- Numbers
  161.                 monitor.setCursorPos(25, line)
  162.                 monitor.setTextColor(colors.lightBlue)
  163.                 monitor.write(string.format("%d/%d", currentAmount, itemConfig.target))
  164.                
  165.                 -- Progress bar
  166.                 if line + 1 <= maxY - 2 then
  167.                     drawProgressBar(monitor, 1, line + 1, 30, currentAmount, itemConfig.target, itemConfig.threshold)
  168.                     line = line + 2
  169.                 else
  170.                     line = line + 1
  171.                 end
  172.             end
  173.         end
  174.     else
  175.         monitor.setCursorPos(1, line)
  176.         monitor.setTextColor(colors.gray)
  177.         monitor.write("No items configured")
  178.     end
  179.    
  180.     -- Footer with padding
  181.     local maxY = select(2, monitor.getSize())
  182.     monitor.setCursorPos(1, maxY - 1)  -- Added padding-top
  183.     monitor.setTextColor(colors.white)
  184.     monitor.setBackgroundColor(running and colors.green or colors.gray)
  185.     monitor.write(running and " MONITORING " or "   IDLE    ")
  186.     monitor.setBackgroundColor(colors.black)
  187. end
  188.  
  189. -- Show shutdown message on monitor
  190. function display.showShutdown()
  191.     if not monitor then return end
  192.    
  193.     monitor.clear()
  194.     monitor.setCursorPos(1, 1)
  195.     monitor.setTextColor(colors.red)
  196.     monitor.write("AE2 Stockpile Manager")
  197.     monitor.setCursorPos(1, 2)
  198.     monitor.setTextColor(colors.white)
  199.     monitor.write("Program Stopped")
  200. end
  201.  
  202. return display
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement