Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 Stockpile Manager - Main Program
- -- Load all required modules
- local config = dofile("ae2_config.lua")
- local display = dofile("ae2_display.lua")
- local ae2 = dofile("ae2_interface.lua")
- local menu = dofile("ae2_menu.lua")
- -- Global state
- local running = false
- local currentCraftingJobs = {}
- -- Helper function to get system status
- local function getSystemStatus()
- if ae2.isConnected() and not config.isTestMode() then
- return "online"
- elseif config.isTestMode() then
- return "test"
- else
- return "offline"
- end
- end
- -- Initialize all systems
- local function initialize()
- print("=== AE2 Stockpile Manager ===")
- print("Initializing systems...")
- -- Load configuration
- config.load()
- -- Initialize AE2 connection
- local ae2Connected = ae2.initialize()
- -- Initialize display
- display.initialize()
- if not ae2Connected then
- print()
- print("No proper AE2 connection found.")
- print("You can still use the program in TEST MODE.")
- print()
- print("Press any key to continue...")
- os.pullEvent("key")
- end
- -- Initial display update
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- return ae2Connected
- end
- -- Monitor items and request crafting as needed
- local function monitorAndCraft()
- local quantities = ae2.getCurrentQuantities()
- local settings = config.getSettings()
- -- Clean up old crafting jobs (remove jobs older than 2 minutes or likely completed)
- for i = #currentCraftingJobs, 1, -1 do
- local job = currentCraftingJobs[i]
- local jobAge = os.clock() - job.time
- -- Remove if job is old OR if we have enough items (likely completed)
- local currentAmount = quantities[job.item] or 0
- local hasEnoughItems = false
- -- Check if any monitored item for this job has enough stock
- for _, itemConfig in pairs(settings) do
- if itemConfig.item == job.item and currentAmount >= itemConfig.target then
- hasEnoughItems = true
- break
- end
- end
- if jobAge > 120 or hasEnoughItems then -- 2 minutes or enough stock
- table.remove(currentCraftingJobs, i)
- if hasEnoughItems then
- print("Crafting completed: " .. job.item)
- end
- end
- end
- for _, itemConfig in pairs(settings) do
- local currentAmount = quantities[itemConfig.item] or 0
- if currentAmount < itemConfig.threshold then
- -- Check if we already have a crafting job for this item
- local alreadyCrafting = false
- for _, job in pairs(currentCraftingJobs) do
- if job.item == itemConfig.item then
- alreadyCrafting = true
- break
- end
- end
- if not alreadyCrafting then
- local craftAmount = itemConfig.target - currentAmount
- print("LOW STOCK: " .. itemConfig.item .. " (" .. currentAmount .. "/" .. itemConfig.threshold .. ")")
- print("Requesting craft of " .. craftAmount .. " items...")
- if ae2.requestCrafting(itemConfig.item, craftAmount) then
- -- Add to current crafting jobs for display
- table.insert(currentCraftingJobs, {
- item = itemConfig.item,
- amount = craftAmount,
- time = os.clock()
- })
- -- Add delay to prevent spam requests
- os.sleep(2)
- end
- end
- end
- end
- -- Update display with current quantities
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- end
- -- Main monitoring loop
- local function startMonitoring()
- term.clear()
- term.setCursorPos(1, 1)
- print("=== AE2 Stockpile Manager ===")
- print("Starting monitoring loop...")
- print("Press 'q' to stop monitoring")
- if display.hasMonitor() then
- print("Status will be shown on connected monitor")
- end
- print()
- running = true
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- -- Start monitoring in parallel
- parallel.waitForAny(
- function()
- while running do
- print("Checking item levels... (" .. os.date() .. ")")
- monitorAndCraft()
- -- Simulate test mode crafting completion
- if config.isTestMode() then
- ae2.simulateTestCrafting(currentCraftingJobs)
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- end
- for i = 1, config.CHECK_INTERVAL do
- os.sleep(1)
- if not running then break end
- -- Update display more frequently - every 3 seconds
- if i % 3 == 0 then
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- end
- end
- end
- end,
- function()
- while running do
- local event, key = os.pullEvent("key")
- if key == keys.q then
- running = false
- end
- end
- end
- )
- print()
- print("Monitoring stopped")
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- os.sleep(2)
- end
- -- Test crafting function
- local function testCrafting()
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Test Crafting Request ===")
- print()
- write("Item name: ")
- local itemName = read()
- write("Quantity: ")
- local amount = tonumber(read())
- if itemName ~= "" and amount and amount > 0 then
- -- Auto-add minecraft: prefix if not present
- if not itemName:find(":") then
- itemName = "minecraft:" .. itemName
- end
- print("Testing crafting request...")
- if ae2.requestCrafting(itemName, amount) then
- table.insert(currentCraftingJobs, {
- item = itemName,
- amount = amount,
- time = os.clock()
- })
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- end
- else
- print("Invalid input")
- end
- print("Press any key to continue...")
- os.pullEvent("key")
- end
- -- Main program loop
- local function main()
- initialize()
- -- Main menu loop
- while true do
- local choice = menu.showMainMenu(config.isTestMode(), display.hasMonitor())
- if choice == "1" then
- menu.viewSettings(config.getSettings())
- elseif choice == "2" then
- menu.addEditItem(config, ae2, display, currentCraftingJobs, running)
- elseif choice == "3" then
- menu.removeItem(config, ae2, display, currentCraftingJobs, running)
- elseif choice == "4" then
- startMonitoring()
- elseif choice == "5" then
- testCrafting()
- elseif choice == "6" then
- menu.viewSystemStatus(ae2, display.hasMonitor())
- elseif choice == "7" then
- config.toggleTestMode()
- print("Test mode " .. (config.isTestMode() and "enabled" or "disabled"))
- ae2.initialize() -- Reinitialize with new mode
- local quantities = ae2.getCurrentQuantities()
- display.update(config.getSettings(), currentCraftingJobs, running, quantities, getSystemStatus())
- os.sleep(1)
- elseif choice == "8" then
- break
- end
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("=== AE2 Stockpile Manager ===")
- print("Thank you for using AE2 Stockpile Manager!")
- if display.hasMonitor() then
- display.showShutdown()
- end
- end
- -- Start the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement