TheYellowBush

ae2_display.lua

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