Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2Warehouse.lua
- -- Version 1.8
- -- Author: Modified by AshGrey
- -- Date: 2024-10-16
- -- This script monitors MineColonies warehouse work requests and tries to fulfill them
- -- using the AE2 network. It automates requests through storage access and crafting patterns.
- -------------------------------------------------------------------------------
- -- INITIALIZATION
- -------------------------------------------------------------------------------
- -- Initialize the AE2 Bridge Realative to the computer
- local aeBridge = peripheral.find("meBridge")
- if not aeBridge then error("AE2 Bridge not found.") end
- print("AE2 Bridge initialized.")
- -- Initialize Colony Integrator
- local colony = peripheral.find("colonyIntegrator")
- if not colony then error("Colony Integrator not found.") end
- if not colony.isInColony() then error("Colony Integrator is not in a colony.") end
- print("Colony Integrator initialized.")
- -- Initialize Ender Modem
- local modem = peripheral.wrap("left") -- Wrap the Ender Modem on the left side
- if not modem then error("Ender Modem not found.") end
- print("Ender Modem initialized.")
- -- Point to storage container relative to the ME Bridge (connected via ME Export Bus or Storage Bus)
- local storage = "up"
- print("Storage initialized.")
- -------------------------------------------------------------------------------
- -- FUNCTIONS
- -------------------------------------------------------------------------------
- -- Function to strip NBT data from item names
- local function stripNBT(itemName)
- return itemName:match("([^%{]+)") -- Match only the name before any NBT data
- end
- -- Export items from AE2 network to storage container
- function exportItem(name, count)
- local itemData = {name = name, count = count}
- local success, err = aeBridge.exportItem(itemData, storage)
- -- Debugging output
- if success then
- print("Successfully exported:", name, "Count:", count)
- return count -- Return the count requested
- else
- print("Failed to export:", name, "Error:", err)
- return 0 -- No items exported
- end
- end
- -- Start crafting job for missing items
- function craftItem(name, count)
- local success = aeBridge.craftItem({name = name, count = count})
- if success then
- print("Crafting job scheduled:", count, "x", name)
- else
- print("Crafting failed:", name)
- end
- return success
- end
- -- Check all open work requests from MineColonies Warehouse
- function handleWorkRequests()
- local requests = colony.getRequests()
- local items = aeBridge.listItems() -- Fetch all AE2 items
- local itemMap = {}
- local missingItems = {}
- -- 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
- print("Processing request:", itemName, "x", needed)
- -- Try to export items from AE2 to storage
- if itemMap[itemName] and itemMap[itemName] > 0 then
- provided = exportItem(itemName, needed)
- else
- print("Requested item not available in AE2:", itemName)
- end
- -- If not enough items, schedule crafting job
- if provided < needed then
- local toCraft = needed - provided
- local crafting = craftItem(itemName, toCraft)
- if crafting then
- print("Crafting initiated:", toCraft, "x", itemName)
- else
- print("Unable to craft:", itemName)
- end
- -- Track missing items for wireless monitor
- if missingItems[itemName] then
- missingItems[itemName] = missingItems[itemName] + toCraft -- Increment if already exists
- else
- missingItems[itemName] = toCraft -- Set initial value if new
- end
- end
- end
- -- Check if there are missing items to transmit
- if next(missingItems) then -- Only transmit if there are missing items
- local serializedData = ""
- for itemName, count in pairs(missingItems) do
- -- Prepare data in a way that avoids serialization errors
- serializedData = serializedData .. itemName .. ":" .. count .. ";"
- end
- -- Transmit the serialized data
- modem.transmit(1, 1, serializedData) -- Transmit to channel 1
- print("Missing items transmitted:", serializedData)
- end
- end
- -------------------------------------------------------------------------------
- -- MAIN LOOP
- -------------------------------------------------------------------------------
- print("Starting AE2 Warehouse Monitor...")
- while true do
- -- Only scan requests during daytime (MineColonies logic)
- local time = os.time()
- if time >= 6 and time < 18 then
- handleWorkRequests()
- else
- print("Nighttime detected. Pausing...")
- end
- -- Wait 30 seconds before next scan
- sleep(30)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement