Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2Warehouse.lua
- -- Improved Version by AshGrey
- -------------------------------------------------------------------------------
- -- CONFIGURATION
- -------------------------------------------------------------------------------
- local config = {
- WarehouseDirection = "up",
- aeBridgeSide = "top",
- transmissionChannel = 1,
- daytimeStart = 6,
- daytimeEnd = 18,
- scanInterval = 30,
- }
- -------------------------------------------------------------------------------
- -- INITIALIZATION
- -------------------------------------------------------------------------------
- -- Initialize peripherals with error handling
- local aeBridge = peripheral.find("meBridge") or error("AE2 Bridge not found.")
- local colony = peripheral.find("colonyIntegrator") or error("Colony Integrator not found.")
- assert(colony.isInColony(), "Colony Integrator is not in a colony.")
- local modem = peripheral.find("modem") or error("Modem not found")
- -- Initialize monitor (optional)
- local monitor = peripheral.find("monitor")
- if monitor then
- monitor.clear()
- monitor.setTextScale(0.5)
- end
- -------------------------------------------------------------------------------
- -- FUNCTIONS
- -------------------------------------------------------------------------------
- -- Function to split a string by a separator (e.g., space, newline)
- local function split(inputStr, separator)
- local result = {}
- for match in (inputStr .. separator):gmatch("(.-)" .. separator) do
- table.insert(result, match)
- end
- return result
- end
- -- Improved Log function with monitor support (optional)
- local function log(level, message)
- local formattedMessage = string.format("[%s] [%s] %s", textutils.formatTime(os.time()), level, message)
- -- Print to the console
- print(formattedMessage)
- -- Display message on the monitor (if available)
- if monitor then
- local cursorX, cursorY = monitor.getCursorPos()
- local monitorWidth, monitorHeight = monitor.getSize()
- if cursorY >= monitorHeight then
- monitor.scroll(1)
- cursorY = monitorHeight - 1
- end
- local wrappedMessage = ""
- local startIndex = 1
- while startIndex <= #formattedMessage do
- local cutIndex = startIndex + monitorWidth - 1
- if cutIndex > #formattedMessage then
- cutIndex = #formattedMessage
- else
- while cutIndex > startIndex and formattedMessage:sub(cutIndex, cutIndex) ~= " " do
- cutIndex = cutIndex - 1
- end
- end
- wrappedMessage = wrappedMessage .. formattedMessage:sub(startIndex, cutIndex) .. "\n"
- startIndex = cutIndex + 1
- end
- monitor.setCursorPos(1, cursorY + 1)
- monitor.write(wrappedMessage)
- end
- end
- -- Function to strip NBT data
- local function stripNBT(itemName)
- return itemName:match("([^%{]+)")
- end
- -- Export items to storage
- local function exportItem(name, count)
- local itemData = {name = name, count = count}
- local success, err = aeBridge.exportItem(itemData, config.WarehouseDirection)
- if success then
- log("INFO", "Exported: " .. name .. " x " .. count)
- return count
- else
- log("ERROR", "Failed to export: " .. name .. " - " .. err)
- return 0
- end
- end
- -- Craft items using AE2
- local function craftItem(name, count)
- local success = aeBridge.craftItem({name = name, count = count})
- if success then
- log("INFO", "Crafting initiated: " .. name .. " x " .. count)
- else
- log("ERROR", "Crafting failed for: " .. name)
- end
- return success
- end
- -- Enhanced block request handler with more logging
- function handleWorkRequests()
- local requests = colony.getRequests()
- local items = aeBridge.listItems() -- Fetch all AE2 items
- -- Check if items were successfully fetched
- if not items then
- log("ERROR", "Failed to fetch items from AE2. Retrying in the next cycle.")
- return -- Skip this cycle and try again later
- end
- local itemMap = {}
- local missingItems = {}
- local domumData = {} -- Store domum_ornamentum item data
- local normalItemsDisplayName = {} -- Store display names for normal items
- local consolidatedMessage = "" -- Collect all messages
- -- Map item amounts for quick access
- for _, item in ipairs(items) do
- local strippedName = stripNBT(item.name) -- Strip NBT data
- itemMap[strippedName] = item.amount
- end
- for _, req in pairs(requests) do
- local itemName = stripNBT(req.items[1].name) -- Strip NBT from requested item
- local needed = req.count
- local provided = 0
- -- Handle 'domum_ornamentum' items
- if itemName:match("^domum_ornamentum") then
- local textureData = {}
- if req.items[1].nbt and req.items[1].nbt.textureData then
- for _, textureValues in pairs(req.items[1].nbt.textureData) do
- table.insert(textureData, textureValues)
- end
- end
- local blockData = {}
- local blockCount = 1
- for _, block in ipairs(textureData) do
- table.insert(blockData, " Block" .. blockCount .. ": " .. block)
- blockCount = blockCount + 1
- end
- domumData[itemName] = {
- displayName = req.items[1].displayName,
- textureData = blockData
- }
- else
- -- Handle normal items (non-"domum_ornamentum")
- normalItemsDisplayName[itemName] = req.items[1].displayName
- end
- -- Export items
- if itemMap[itemName] and itemMap[itemName] > 0 then
- provided = exportItem(itemName, needed)
- else
- log("WARNING", "Requested item not available in AE2: " .. itemName)
- end
- -- Craft missing items
- if provided < needed then
- local toCraft = needed - provided
- local crafting = craftItem(itemName, toCraft)
- if crafting then
- log("INFO", "Crafting initiated: " .. toCraft .. " x " .. itemName)
- else
- log("ERROR", "Unable to craft: " .. itemName)
- end
- -- Track missing items
- missingItems[itemName] = (missingItems[itemName] or 0) + toCraft
- end
- end
- -- Append missing items to the consolidated message
- if next(missingItems) then
- for itemName, count in pairs(missingItems) do
- if itemName:match("^domum_ornamentum") then
- local itemData = domumData[itemName]
- consolidatedMessage = consolidatedMessage .. string.format("%s:\nDisplay Name: %s\nBlocks Needed: %s\nMissing Count: %d\n",
- itemName, itemData.displayName, table.concat(itemData.textureData, ", "), count)
- else
- local displayName = normalItemsDisplayName[itemName] or "Unknown Display Name"
- consolidatedMessage = consolidatedMessage .. string.format("%s:\nDisplay Name: %s\nMissing Count: %d\n", itemName, displayName, count)
- end
- end
- end
- -- Transmit the consolidated message
- modem.transmit(config.transmissionChannel, config.transmissionChannel, consolidatedMessage)
- end
- -------------------------------------------------------------------------------
- -- MAIN LOOP
- -------------------------------------------------------------------------------
- log("INFO", "Starting AE2 Warehouse Monitor...")
- while true do
- local time = os.time()
- if time >= config.daytimeStart and time < config.daytimeEnd then
- handleWorkRequests()
- else
- log("INFO", "Nighttime detected. Pausing...")
- end
- sleep(config.scanInterval)
- end
Advertisement
Add Comment
Please, Sign In to add comment