Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 Crafting Monitor - optimized for getCraftingCPUs method
- -- Configuration
- local REFRESH_RATE = 0.5 -- Update every 0.5 seconds
- local MANUAL_CPU_COUNT = 24 -- Set this to your actual CPU count
- -- Global variables
- local bridge = nil
- local monitor = nil
- -- Auto-detect peripherals
- function findPeripherals()
- local foundBridge = false
- local foundMonitor = false
- print("Searching for peripherals...")
- -- Get all peripherals
- local peripherals = peripheral.getNames()
- -- First look for ME Bridge
- for _, name in ipairs(peripherals) do
- local type = peripheral.getType(name)
- -- Check for various possible ME bridge types
- if type == "meBridge" or type == "me_bridge" or
- type:find("ae2") or type:find("appliedenergistics") then
- print("Found ME Bridge: " .. name)
- bridge = peripheral.wrap(name)
- foundBridge = true
- break
- end
- end
- -- Then look for a monitor
- for _, name in ipairs(peripherals) do
- if peripheral.getType(name) == "monitor" then
- print("Found monitor: " .. name)
- monitor = peripheral.wrap(name)
- monitor.setTextScale(1)
- monitor.clear()
- foundMonitor = true
- break
- end
- end
- return foundBridge, foundMonitor
- end
- -- Initialize system
- function init()
- print("Initializing AE2 Crafting Monitor...")
- -- Auto-detect peripherals
- local foundBridge, foundMonitor = findPeripherals()
- if not foundBridge then
- error("ME Bridge not found! Please connect an ME Bridge peripheral.")
- end
- if not foundMonitor then
- print("Warning: Monitor not found, using terminal output")
- end
- print("Initialization complete!")
- end
- -- Display output to terminal or monitor
- function write(x, y, text, color)
- if monitor then
- if color then monitor.setTextColor(color) end
- monitor.setCursorPos(x, y)
- monitor.write(text)
- else
- term.setCursorPos(x, y)
- if color then term.setTextColor(color) end
- term.write(text)
- end
- end
- -- Center text on screen
- function writeCenter(y, text, color)
- local width = monitor and monitor.getSize() or term.getSize()
- local x = math.floor((width - #text) / 2) + 1
- write(x, y, text, color)
- end
- -- Clear screen (monitor or terminal)
- function clearScreen()
- if monitor then
- monitor.clear()
- else
- term.clear()
- end
- end
- -- Get crafting CPUs and their status
- function getCraftingStatus()
- if not bridge.getCraftingCPUs then
- error("getCraftingCPUs method not available")
- end
- return bridge.getCraftingCPUs()
- end
- -- Get color based on progress
- function getProgressColor(progress)
- if progress < 0.25 then
- return colors.red
- elseif progress < 0.75 then
- return colors.yellow
- else
- return colors.green
- end
- end
- -- Display crafting CPU status
- function displayCraftingStatus(cpus)
- clearScreen()
- local width = monitor and monitor.getSize() or term.getSize()
- -- Display header
- writeCenter(1, "AE2 CRAFTING MONITOR", colors.yellow)
- writeCenter(2, string.rep("-", width - 10), colors.yellow)
- -- Track active crafting count and collect data
- local activeCPUs = 0
- local itemsBeingCrafted = {}
- local totalItemsRequested = 0
- local totalItemsCompleted = 0
- -- Use manual CPU count if set, otherwise use detected count
- local cpuCount = MANUAL_CPU_COUNT > 0 and MANUAL_CPU_COUNT or #cpus
- -- Analyze CPU data
- for i, cpu in ipairs(cpus) do
- -- Check if CPU is busy/crafting
- local busy = false
- if cpu.busy ~= nil then
- busy = cpu.busy
- elseif cpu.isBusy ~= nil then
- busy = cpu.isBusy
- elseif cpu.crafting ~= nil then
- busy = cpu.crafting
- end
- if busy then
- activeCPUs = activeCPUs + 1
- -- Try to get what's being crafted
- local itemName = "Unknown Item"
- if cpu.craftingItem or cpu.item or cpu.crafting then
- local item = cpu.craftingItem or cpu.item or cpu.crafting
- if type(item) == "table" and item.name then
- itemName = item.name
- elseif type(item) == "string" then
- itemName = item
- end
- end
- -- Add to items being crafted
- if not itemsBeingCrafted[itemName] then
- itemsBeingCrafted[itemName] = 1
- else
- itemsBeingCrafted[itemName] = itemsBeingCrafted[itemName] + 1
- end
- -- Look for crafting count information
- -- Try various possible field names
- local itemsRequired = 0
- local itemsFinished = 0
- -- Look for requested/total items
- if cpu.requestedItems and type(cpu.requestedItems) == "number" then
- itemsRequired = cpu.requestedItems
- elseif cpu.totalItems and type(cpu.totalItems) == "number" then
- itemsRequired = cpu.totalItems
- elseif cpu.toCreate and type(cpu.toCreate) == "number" then
- itemsRequired = cpu.toCreate
- elseif cpu.requested and type(cpu.requested) == "number" then
- itemsRequired = cpu.requested
- end
- -- Look for completed items
- if cpu.craftedItems and type(cpu.craftedItems) == "number" then
- itemsFinished = cpu.craftedItems
- elseif cpu.completedItems and type(cpu.completedItems) == "number" then
- itemsFinished = cpu.completedItems
- elseif cpu.created and type(cpu.created) == "number" then
- itemsFinished = cpu.created
- end
- -- If we have progress percentage but not counts, try to calculate
- if itemsRequired == 0 and cpu.progress and type(cpu.progress) == "number" then
- -- Try to find total count
- if cpu.amount and type(cpu.amount) == "number" then
- itemsRequired = cpu.amount
- itemsFinished = math.floor(itemsRequired * cpu.progress)
- end
- end
- -- Add to totals
- totalItemsRequested = totalItemsRequested + itemsRequired
- totalItemsCompleted = totalItemsCompleted + itemsFinished
- -- Debug print what we found
- if i == 1 then -- Just print the first CPU to avoid spam
- print("CPU " .. i .. " info:")
- for k, v in pairs(cpu) do
- if type(v) ~= "table" and type(v) ~= "function" then
- print(" " .. k .. " = " .. tostring(v))
- end
- end
- end
- end
- end
- -- Convert table of items to a list
- local craftingItems = {}
- for item, count in pairs(itemsBeingCrafted) do
- table.insert(craftingItems, {name = item, count = count})
- end
- -- Display system status based on activity
- if activeCPUs == 0 then
- -- No active crafting
- writeCenter(5, "System Status: IDLE", colors.lightGray)
- writeCenter(7, "No crafting operations in progress", colors.white)
- writeCenter(9, "Total CPUs Available: " .. cpuCount, colors.cyan)
- -- Draw some decoration when idle
- writeCenter(12, "System ready for crafting tasks", colors.green)
- writeCenter(14, "[ READY ]", colors.green)
- else
- -- Active crafting
- writeCenter(4, "System Status: ACTIVE", colors.orange)
- -- Overview
- local cpuText = activeCPUs .. " of " .. cpuCount .. " CPUs active"
- writeCenter(6, cpuText, colors.cyan)
- -- Display crafting completion information
- writeCenter(8, "Crafting Progress:", colors.white)
- -- Show items crafted
- local itemsText
- if totalItemsRequested > 0 then
- itemsText = totalItemsCompleted .. "/" .. totalItemsRequested .. " items crafted"
- else
- -- If we couldn't find item counts, display generic message
- itemsText = "Items being crafted..."
- end
- writeCenter(9, itemsText, colors.yellow)
- -- Calculate percentage if possible
- if totalItemsRequested > 0 then
- local percentage = math.floor((totalItemsCompleted / totalItemsRequested) * 100)
- local percentText = "(" .. percentage .. "% complete)"
- writeCenter(10, percentText, getProgressColor(percentage / 100))
- end
- -- Items being crafted
- writeCenter(12, "Items Being Crafted:", colors.white)
- local y = 14
- for i, item in ipairs(craftingItems) do
- local name = item.name
- if #name > 30 then
- name = string.sub(name, 1, 27) .. "..."
- end
- local itemText = name .. " x" .. item.count
- writeCenter(y, itemText, colors.green)
- y = y + 1
- if y > (monitor and monitor.getSize() or 15) - 2 then
- writeCenter(y, "... and " .. (#craftingItems - i) .. " more items", colors.lightGray)
- break
- end
- end
- end
- end
- -- Main monitoring function
- function monitorCrafting()
- while true do
- -- Get and display crafting status
- local ok, result = pcall(getCraftingStatus)
- if ok then
- if type(result) == "table" then
- displayCraftingStatus(result)
- else
- clearScreen()
- write(1, 1, "Unexpected result format:", colors.red)
- write(1, 2, type(result), colors.red)
- end
- else
- clearScreen()
- write(1, 1, "Error getting crafting status:", colors.red)
- write(1, 2, tostring(result), colors.red)
- end
- sleep(REFRESH_RATE)
- end
- end
- -- Main program function
- function run()
- init()
- monitorCrafting()
- end
- -- Start the program
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement