Advertisement
TheYellowBush

ae2_stockpile.lua

Jul 8th, 2025 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.71 KB | None | 0 0
  1. -- AE2 Stockpile Manager - Main Program
  2. -- Load all required modules
  3. local config = dofile("ae2_config.lua")
  4. local display = dofile("ae2_display.lua")
  5. local ae2 = dofile("ae2_interface.lua")
  6. local menu = dofile("ae2_menu.lua")
  7.  
  8. -- Global state
  9. local running = false
  10. local currentCraftingJobs = {}
  11.  
  12. -- Helper function to get system status
  13. local function getSystemStatus()
  14.     if ae2.isConnected() and not config.isTestMode() then
  15.         return "online"
  16.     elseif config.isTestMode() then
  17.         return "test"
  18.     else
  19.         return "offline"
  20.     end
  21. end
  22.  
  23. -- Initialize all systems
  24. local function initialize()
  25.     print("=== AE2 Stockpile Manager ===")
  26.     print("Initializing systems...")
  27.    
  28.     -- Load configuration
  29.     config.load()
  30.    
  31.     -- Initialize AE2 connection
  32.     local ae2Connected = ae2.initialize()
  33.    
  34.     -- Initialize display
  35.     display.initialize()
  36.    
  37.     if not ae2Connected then
  38.         print()
  39.         print("No proper AE2 connection found.")
  40.         print("You can still use the program in TEST MODE.")
  41.         print()
  42.         print("Press any key to continue...")
  43.         os.pullEvent("key")
  44.     end
  45.    
  46.     -- Initial display update
  47.     local quantities = ae2.getCurrentQuantities()
  48.     display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  49.    
  50.     return ae2Connected
  51. end
  52.  
  53. -- Monitor items and request crafting as needed
  54. local function monitorAndCraft()
  55.     local quantities = ae2.getCurrentQuantities()
  56.     local settings = config.getSettings()
  57.    
  58.     -- Clean up old crafting jobs (remove jobs older than 2 minutes or likely completed)
  59.     for i = #currentCraftingJobs, 1, -1 do
  60.         local job = currentCraftingJobs[i]
  61.         local jobAge = os.clock() - job.time
  62.        
  63.         -- Remove if job is old OR if we have enough items (likely completed)
  64.         local currentAmount = quantities[job.item] or 0
  65.         local hasEnoughItems = false
  66.        
  67.         -- Check if any monitored item for this job has enough stock
  68.         for _, itemConfig in pairs(settings) do
  69.             if itemConfig.item == job.item and currentAmount >= itemConfig.target then
  70.                 hasEnoughItems = true
  71.                 break
  72.             end
  73.         end
  74.        
  75.         if jobAge > 120 or hasEnoughItems then -- 2 minutes or enough stock
  76.             table.remove(currentCraftingJobs, i)
  77.             if hasEnoughItems then
  78.                 print("Crafting completed: " .. job.item)
  79.             end
  80.         end
  81.     end
  82.    
  83.     for _, itemConfig in pairs(settings) do
  84.         local currentAmount = quantities[itemConfig.item] or 0
  85.        
  86.         if currentAmount < itemConfig.threshold then
  87.             -- Check if we already have a crafting job for this item
  88.             local alreadyCrafting = false
  89.             for _, job in pairs(currentCraftingJobs) do
  90.                 if job.item == itemConfig.item then
  91.                     alreadyCrafting = true
  92.                     break
  93.                 end
  94.             end
  95.            
  96.             if not alreadyCrafting then
  97.                 local craftAmount = itemConfig.target - currentAmount
  98.                 print("LOW STOCK: " .. itemConfig.item .. " (" .. currentAmount .. "/" .. itemConfig.threshold .. ")")
  99.                 print("Requesting craft of " .. craftAmount .. " items...")
  100.                
  101.                 if ae2.requestCrafting(itemConfig.item, craftAmount) then
  102.                     -- Add to current crafting jobs for display
  103.                     table.insert(currentCraftingJobs, {
  104.                         item = itemConfig.item,
  105.                         amount = craftAmount,
  106.                         time = os.clock()
  107.                     })
  108.                    
  109.                     -- Add delay to prevent spam requests
  110.                     os.sleep(2)
  111.                 end
  112.             end
  113.         end
  114.     end
  115.    
  116.     -- Update display with current quantities
  117.     display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  118. end
  119.  
  120. -- Main monitoring loop
  121. local function startMonitoring()
  122.     term.clear()
  123.     term.setCursorPos(1, 1)
  124.     print("=== AE2 Stockpile Manager ===")
  125.     print("Starting monitoring loop...")
  126.     print("Press 'q' to stop monitoring")
  127.     if display.hasMonitor() then
  128.         print("Status will be shown on connected monitor")
  129.     end
  130.     print()
  131.    
  132.     running = true
  133.     local quantities = ae2.getCurrentQuantities()
  134.     display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  135.    
  136.     -- Start monitoring in parallel
  137.     parallel.waitForAny(
  138.         function()
  139.             while running do
  140.                 print("Checking item levels... (" .. os.date() .. ")")
  141.                 monitorAndCraft()
  142.                
  143.                 -- Simulate test mode crafting completion
  144.                 if config.isTestMode() then
  145.                     ae2.simulateTestCrafting(currentCraftingJobs)
  146.                     local quantities = ae2.getCurrentQuantities()
  147.                     display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  148.                 end
  149.                
  150.                 for i = 1, config.CHECK_INTERVAL do
  151.                     os.sleep(1)
  152.                     if not running then break end
  153.                    
  154.                     -- Update display more frequently - every 3 seconds
  155.                     if i % 3 == 0 then
  156.                         local quantities = ae2.getCurrentQuantities()
  157.                         display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  158.                     end
  159.                 end
  160.             end
  161.         end,
  162.         function()
  163.             while running do
  164.                 local event, key = os.pullEvent("key")
  165.                 if key == keys.q then
  166.                     running = false
  167.                 end
  168.             end
  169.         end
  170.     )
  171.    
  172.     print()
  173.     print("Monitoring stopped")
  174.     local quantities = ae2.getCurrentQuantities()
  175.     display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  176.     os.sleep(2)
  177. end
  178.  
  179. -- Test crafting function
  180. local function testCrafting()
  181.     term.clear()
  182.     term.setCursorPos(1, 1)
  183.     print("=== Test Crafting Request ===")
  184.     print()
  185.    
  186.     write("Item name: ")
  187.     local itemName = read()
  188.     write("Quantity: ")
  189.     local amount = tonumber(read())
  190.    
  191.     if itemName ~= "" and amount and amount > 0 then
  192.         -- Auto-add minecraft: prefix if not present
  193.         if not itemName:find(":") then
  194.             itemName = "minecraft:" .. itemName
  195.         end
  196.        
  197.         print("Testing crafting request...")
  198.         if ae2.requestCrafting(itemName, amount) then
  199.             table.insert(currentCraftingJobs, {
  200.                 item = itemName,
  201.                 amount = amount,
  202.                 time = os.clock()
  203.             })
  204.             local quantities = ae2.getCurrentQuantities()
  205.             display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  206.         end
  207.     else
  208.         print("Invalid input")
  209.     end
  210.    
  211.     print("Press any key to continue...")
  212.     os.pullEvent("key")
  213. end
  214.  
  215. -- Main program loop
  216. local function main()
  217.     initialize()
  218.    
  219.     -- Main menu loop
  220.     while true do
  221.         local choice = menu.showMainMenu(config.isTestMode(), display.hasMonitor())
  222.        
  223.         if choice == "1" then
  224.             menu.viewSettings(config.getSettings())
  225.         elseif choice == "2" then
  226.             menu.addEditItem(config, ae2, display, currentCraftingJobs, running)
  227.         elseif choice == "3" then
  228.             menu.removeItem(config, ae2, display, currentCraftingJobs, running)
  229.         elseif choice == "4" then
  230.             startMonitoring()
  231.         elseif choice == "5" then
  232.             testCrafting()
  233.         elseif choice == "6" then
  234.             menu.viewSystemStatus(ae2, display.hasMonitor())
  235.         elseif choice == "7" then
  236.             config.toggleTestMode()
  237.             print("Test mode " .. (config.isTestMode() and "enabled" or "disabled"))
  238.             ae2.initialize() -- Reinitialize with new mode
  239.             local quantities = ae2.getCurrentQuantities()
  240.             display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
  241.             os.sleep(1)
  242.         elseif choice == "8" then
  243.             break
  244.         end
  245.     end
  246.    
  247.     term.clear()
  248.     term.setCursorPos(1, 1)
  249.     print("=== AE2 Stockpile Manager ===")
  250.     print("Thank you for using AE2 Stockpile Manager!")
  251.    
  252.     if display.hasMonitor() then
  253.         display.showShutdown()
  254.     end
  255. end
  256.  
  257. -- Start the program
  258. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement