Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 Stockpile Manager - AE2 Interface Module
- local ae2 = {}
- -- Configuration
- local INTERFACE_SIDE = "back"
- local INTERFACE_TYPE = "meBridge" -- Advanced Peripherals ME Bridge
- -- Private variables
- local ae2Interface = nil
- local config = nil
- -- Test mode simulation data
- local testItems = {
- ["minecraft:iron_ingot"] = 450,
- ["minecraft:gold_ingot"] = 200,
- ["minecraft:diamond"] = 50,
- ["minecraft:redstone"] = 1000,
- ["minecraft:coal"] = 800,
- ["minecraft:stick"] = 300
- }
- local testCraftableItems = {
- "minecraft:iron_ingot",
- "minecraft:gold_ingot",
- "minecraft:diamond_sword",
- "minecraft:stick",
- "minecraft:crafting_table"
- }
- local testCraftingQueue = {}
- -- Fake AE2 interface for testing
- local function createFakeInterface()
- return {
- listItems = function()
- local items = {}
- for name, count in pairs(testItems) do
- table.insert(items, {
- name = name,
- displayName = name:gsub("minecraft:", ""):gsub("_", " "),
- amount = count
- })
- end
- return items
- end,
- listCraftableItems = function()
- local items = {}
- for _, name in pairs(testCraftableItems) do
- table.insert(items, {
- name = name,
- displayName = name:gsub("minecraft:", ""):gsub("_", " ")
- })
- end
- return items
- end,
- craftItem = function(item)
- print("FAKE: Requesting craft of " .. item.count .. "x " .. item.name)
- table.insert(testCraftingQueue, {item = item.name, amount = item.count, time = os.clock()})
- return true
- end
- }
- end
- -- Initialize AE2 connection
- function ae2.initialize()
- -- Load config module
- config = dofile("ae2_config.lua")
- if config.isTestMode() then
- print("Starting in TEST MODE")
- ae2Interface = createFakeInterface()
- return true
- else
- -- Try to find Advanced Peripherals ME Bridge
- local bridge = peripheral.find(INTERFACE_TYPE)
- if bridge then
- print("Found Advanced Peripherals ME Bridge")
- ae2Interface = bridge
- -- Test the connection
- local success, items = pcall(function() return ae2Interface.listItems() end)
- if success then
- print("ME Bridge connected successfully - found " .. #items .. " items")
- return true
- else
- print("ME Bridge connection failed: " .. tostring(items))
- return false
- end
- else
- -- Fallback: try to find on specific side
- local interface = peripheral.wrap(INTERFACE_SIDE)
- if interface then
- local ptype = peripheral.getType(INTERFACE_SIDE)
- print("Found peripheral: " .. ptype .. " on " .. INTERFACE_SIDE)
- if ptype == INTERFACE_TYPE then
- ae2Interface = interface
- print("ME Bridge found on " .. INTERFACE_SIDE)
- return true
- else
- print("Wrong peripheral type. Expected '" .. INTERFACE_TYPE .. "', got '" .. ptype .. "'")
- end
- else
- print("No peripheral found")
- end
- -- Show available peripherals for debugging
- local peripherals = peripheral.getNames()
- if #peripherals > 0 then
- print("Available peripherals:")
- for _, name in pairs(peripherals) do
- local ptype = peripheral.getType(name)
- print(" " .. name .. " (" .. ptype .. ")")
- end
- print()
- print("Make sure you have:")
- print("1. Advanced Peripherals mod installed")
- print("2. ME Bridge block placed and connected to your ME system")
- print("3. ME Bridge connected to computer or on the peripheral network")
- end
- return false
- end
- end
- end
- -- Get current item quantities from AE2
- function ae2.getCurrentQuantities()
- if not ae2Interface then return {} end
- local success, items = pcall(function()
- return ae2Interface.listItems()
- end)
- if not success then
- print("ERROR: Failed to get items from ME Bridge: " .. tostring(items))
- return {}
- end
- items = items or {}
- local quantities = {}
- for _, item in pairs(items) do
- local name = item.name or item.id
- local count = item.amount or item.count or 0
- if name then
- quantities[name] = count
- end
- end
- return quantities
- end
- -- Get craftable items from AE2
- function ae2.getCraftableItems()
- if not ae2Interface then return {} end
- local success, items = pcall(function()
- return ae2Interface.listCraftableItems()
- end)
- if not success then
- print("ERROR: Failed to get craftable items: " .. tostring(items))
- return {}
- end
- local craftable = {}
- items = items or {}
- for _, item in pairs(items) do
- local name = item.name or item.id
- if name then
- craftable[name] = item
- end
- end
- return craftable
- end
- -- Find item by name in AE2 system
- function ae2.findItem(itemName, inCraftable)
- if not ae2Interface then return nil end
- local items = {}
- if inCraftable then
- local craftableItems = ae2.getCraftableItems()
- for name, item in pairs(craftableItems) do
- table.insert(items, item)
- end
- else
- local success, availableItems = pcall(function()
- return ae2Interface.listItems()
- end)
- if success and availableItems then
- items = availableItems
- end
- end
- for _, item in pairs(items) do
- local name = item.name or item.id
- local displayName = item.displayName or item.label or name
- if name == itemName or (displayName and displayName:lower():find(itemName:lower())) then
- return item
- end
- end
- return nil
- end
- -- Request crafting for an item
- function ae2.requestCrafting(itemName, amount)
- if not ae2Interface then
- print("ERROR: No AE2 interface connected")
- return false
- end
- -- For Advanced Peripherals ME Bridge, we can craft directly by item name
- local success, result = pcall(function()
- return ae2Interface.craftItem({name = itemName, count = amount})
- end)
- if success and result then
- print("Crafting requested: " .. amount .. "x " .. itemName)
- return true
- else
- print("ERROR: Failed to request crafting for " .. itemName)
- if not success then
- print("Error details: " .. tostring(result))
- end
- -- Check if item is craftable
- local craftableItem = ae2.findItem(itemName, true)
- if not craftableItem then
- print("Item '" .. itemName .. "' is not craftable")
- end
- return false
- end
- end
- -- Simulate test mode crafting completion
- function ae2.simulateTestCrafting(currentCraftingJobs)
- if not config.isTestMode() then return end
- for i = #testCraftingQueue, 1, -1 do
- local craft = testCraftingQueue[i]
- if os.clock() - craft.time > 10 then -- Simulate 10 second crafting
- testItems[craft.item] = (testItems[craft.item] or 0) + craft.amount
- print("FAKE: Completed crafting " .. craft.amount .. "x " .. craft.item)
- table.remove(testCraftingQueue, i)
- -- Remove from currentCraftingJobs when completed
- for j = #currentCraftingJobs, 1, -1 do
- local job = currentCraftingJobs[j]
- if job.item == craft.item and job.amount == craft.amount then
- table.remove(currentCraftingJobs, j)
- break
- end
- end
- end
- end
- end
- -- Check if connected
- function ae2.isConnected()
- return ae2Interface ~= nil
- end
- -- Get system information
- function ae2.getSystemInfo()
- if not ae2Interface then
- return {
- connected = false,
- type = "None",
- itemCount = 0,
- craftableCount = 0
- }
- end
- local quantities = ae2.getCurrentQuantities()
- local craftable = ae2.getCraftableItems()
- local itemCount = 0
- for _ in pairs(quantities) do itemCount = itemCount + 1 end
- local craftableCount = 0
- for _ in pairs(craftable) do craftableCount = craftableCount + 1 end
- return {
- connected = true,
- type = config.isTestMode() and "Test Mode" or "Advanced Peripherals ME Bridge",
- itemCount = itemCount,
- craftableCount = craftableCount,
- quantities = quantities,
- craftable = craftable
- }
- end
- return ae2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement