Advertisement
TheYellowBush

ae2_interface.lua

Jul 8th, 2025 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.20 KB | None | 0 0
  1. -- AE2 Stockpile Manager - AE2 Interface Module
  2.  
  3. local ae2 = {}
  4.  
  5. -- Configuration
  6. local INTERFACE_SIDE = "back"
  7. local INTERFACE_TYPE = "meBridge" -- Advanced Peripherals ME Bridge
  8.  
  9. -- Private variables
  10. local ae2Interface = nil
  11. local config = nil
  12.  
  13. -- Test mode simulation data
  14. local testItems = {
  15.     ["minecraft:iron_ingot"] = 450,
  16.     ["minecraft:gold_ingot"] = 200,
  17.     ["minecraft:diamond"] = 50,
  18.     ["minecraft:redstone"] = 1000,
  19.     ["minecraft:coal"] = 800,
  20.     ["minecraft:stick"] = 300
  21. }
  22.  
  23. local testCraftableItems = {
  24.     "minecraft:iron_ingot",
  25.     "minecraft:gold_ingot",
  26.     "minecraft:diamond_sword",
  27.     "minecraft:stick",
  28.     "minecraft:crafting_table"
  29. }
  30.  
  31. local testCraftingQueue = {}
  32.  
  33. -- Fake AE2 interface for testing
  34. local function createFakeInterface()
  35.     return {
  36.         listItems = function()
  37.             local items = {}
  38.             for name, count in pairs(testItems) do
  39.                 table.insert(items, {
  40.                     name = name,
  41.                     displayName = name:gsub("minecraft:", ""):gsub("_", " "),
  42.                     amount = count
  43.                 })
  44.             end
  45.             return items
  46.         end,
  47.        
  48.         listCraftableItems = function()
  49.             local items = {}
  50.             for _, name in pairs(testCraftableItems) do
  51.                 table.insert(items, {
  52.                     name = name,
  53.                     displayName = name:gsub("minecraft:", ""):gsub("_", " ")
  54.                 })
  55.             end
  56.             return items
  57.         end,
  58.        
  59.         craftItem = function(item)
  60.             print("FAKE: Requesting craft of " .. item.count .. "x " .. item.name)
  61.             table.insert(testCraftingQueue, {item = item.name, amount = item.count, time = os.clock()})
  62.             return true
  63.         end
  64.     }
  65. end
  66.  
  67. -- Initialize AE2 connection
  68. function ae2.initialize()
  69.     -- Load config module
  70.     config = dofile("ae2_config.lua")
  71.    
  72.     if config.isTestMode() then
  73.         print("Starting in TEST MODE")
  74.         ae2Interface = createFakeInterface()
  75.         return true
  76.     else
  77.         -- Try to find Advanced Peripherals ME Bridge
  78.         local bridge = peripheral.find(INTERFACE_TYPE)
  79.         if bridge then
  80.             print("Found Advanced Peripherals ME Bridge")
  81.             ae2Interface = bridge
  82.            
  83.             -- Test the connection
  84.             local success, items = pcall(function() return ae2Interface.listItems() end)
  85.             if success then
  86.                 print("ME Bridge connected successfully - found " .. #items .. " items")
  87.                 return true
  88.             else
  89.                 print("ME Bridge connection failed: " .. tostring(items))
  90.                 return false
  91.             end
  92.         else
  93.             -- Fallback: try to find on specific side
  94.             local interface = peripheral.wrap(INTERFACE_SIDE)
  95.             if interface then
  96.                 local ptype = peripheral.getType(INTERFACE_SIDE)
  97.                 print("Found peripheral: " .. ptype .. " on " .. INTERFACE_SIDE)
  98.                
  99.                 if ptype == INTERFACE_TYPE then
  100.                     ae2Interface = interface
  101.                     print("ME Bridge found on " .. INTERFACE_SIDE)
  102.                     return true
  103.                 else
  104.                     print("Wrong peripheral type. Expected '" .. INTERFACE_TYPE .. "', got '" .. ptype .. "'")
  105.                 end
  106.             else
  107.                 print("No peripheral found")
  108.             end
  109.            
  110.             -- Show available peripherals for debugging
  111.             local peripherals = peripheral.getNames()
  112.             if #peripherals > 0 then
  113.                 print("Available peripherals:")
  114.                 for _, name in pairs(peripherals) do
  115.                     local ptype = peripheral.getType(name)
  116.                     print("  " .. name .. " (" .. ptype .. ")")
  117.                 end
  118.                 print()
  119.                 print("Make sure you have:")
  120.                 print("1. Advanced Peripherals mod installed")
  121.                 print("2. ME Bridge block placed and connected to your ME system")
  122.                 print("3. ME Bridge connected to computer or on the peripheral network")
  123.             end
  124.             return false
  125.         end
  126.     end
  127. end
  128.  
  129. -- Get current item quantities from AE2
  130. function ae2.getCurrentQuantities()
  131.     if not ae2Interface then return {} end
  132.    
  133.     local success, items = pcall(function()
  134.         return ae2Interface.listItems()
  135.     end)
  136.    
  137.     if not success then
  138.         print("ERROR: Failed to get items from ME Bridge: " .. tostring(items))
  139.         return {}
  140.     end
  141.    
  142.     items = items or {}
  143.     local quantities = {}
  144.    
  145.     for _, item in pairs(items) do
  146.         local name = item.name or item.id
  147.         local count = item.amount or item.count or 0
  148.         if name then
  149.             quantities[name] = count
  150.         end
  151.     end
  152.    
  153.     return quantities
  154. end
  155.  
  156. -- Get craftable items from AE2
  157. function ae2.getCraftableItems()
  158.     if not ae2Interface then return {} end
  159.    
  160.     local success, items = pcall(function()
  161.         return ae2Interface.listCraftableItems()
  162.     end)
  163.    
  164.     if not success then
  165.         print("ERROR: Failed to get craftable items: " .. tostring(items))
  166.         return {}
  167.     end
  168.    
  169.     local craftable = {}
  170.     items = items or {}
  171.    
  172.     for _, item in pairs(items) do
  173.         local name = item.name or item.id
  174.         if name then
  175.             craftable[name] = item
  176.         end
  177.     end
  178.    
  179.     return craftable
  180. end
  181.  
  182. -- Find item by name in AE2 system
  183. function ae2.findItem(itemName, inCraftable)
  184.     if not ae2Interface then return nil end
  185.    
  186.     local items = {}
  187.     if inCraftable then
  188.         local craftableItems = ae2.getCraftableItems()
  189.         for name, item in pairs(craftableItems) do
  190.             table.insert(items, item)
  191.         end
  192.     else
  193.         local success, availableItems = pcall(function()
  194.             return ae2Interface.listItems()
  195.         end)
  196.        
  197.         if success and availableItems then
  198.             items = availableItems
  199.         end
  200.     end
  201.    
  202.     for _, item in pairs(items) do
  203.         local name = item.name or item.id
  204.         local displayName = item.displayName or item.label or name
  205.        
  206.         if name == itemName or (displayName and displayName:lower():find(itemName:lower())) then
  207.             return item
  208.         end
  209.     end
  210.     return nil
  211. end
  212.  
  213. -- Request crafting for an item
  214. function ae2.requestCrafting(itemName, amount)
  215.     if not ae2Interface then
  216.         print("ERROR: No AE2 interface connected")
  217.         return false
  218.     end
  219.    
  220.     -- For Advanced Peripherals ME Bridge, we can craft directly by item name
  221.     local success, result = pcall(function()
  222.         return ae2Interface.craftItem({name = itemName, count = amount})
  223.     end)
  224.    
  225.     if success and result then
  226.         print("Crafting requested: " .. amount .. "x " .. itemName)
  227.         return true
  228.     else
  229.         print("ERROR: Failed to request crafting for " .. itemName)
  230.         if not success then
  231.             print("Error details: " .. tostring(result))
  232.         end
  233.        
  234.         -- Check if item is craftable
  235.         local craftableItem = ae2.findItem(itemName, true)
  236.         if not craftableItem then
  237.             print("Item '" .. itemName .. "' is not craftable")
  238.         end
  239.        
  240.         return false
  241.     end
  242. end
  243.  
  244. -- Simulate test mode crafting completion
  245. function ae2.simulateTestCrafting(currentCraftingJobs)
  246.     if not config.isTestMode() then return end
  247.    
  248.     for i = #testCraftingQueue, 1, -1 do
  249.         local craft = testCraftingQueue[i]
  250.         if os.clock() - craft.time > 10 then -- Simulate 10 second crafting
  251.             testItems[craft.item] = (testItems[craft.item] or 0) + craft.amount
  252.             print("FAKE: Completed crafting " .. craft.amount .. "x " .. craft.item)
  253.             table.remove(testCraftingQueue, i)
  254.            
  255.             -- Remove from currentCraftingJobs when completed
  256.             for j = #currentCraftingJobs, 1, -1 do
  257.                 local job = currentCraftingJobs[j]
  258.                 if job.item == craft.item and job.amount == craft.amount then
  259.                     table.remove(currentCraftingJobs, j)
  260.                     break
  261.                 end
  262.             end
  263.         end
  264.     end
  265. end
  266.  
  267. -- Check if connected
  268. function ae2.isConnected()
  269.     return ae2Interface ~= nil
  270. end
  271.  
  272. -- Get system information
  273. function ae2.getSystemInfo()
  274.     if not ae2Interface then
  275.         return {
  276.             connected = false,
  277.             type = "None",
  278.             itemCount = 0,
  279.             craftableCount = 0
  280.         }
  281.     end
  282.    
  283.     local quantities = ae2.getCurrentQuantities()
  284.     local craftable = ae2.getCraftableItems()
  285.    
  286.     local itemCount = 0
  287.     for _ in pairs(quantities) do itemCount = itemCount + 1 end
  288.    
  289.     local craftableCount = 0
  290.     for _ in pairs(craftable) do craftableCount = craftableCount + 1 end
  291.    
  292.     return {
  293.         connected = true,
  294.         type = config.isTestMode() and "Test Mode" or "Advanced Peripherals ME Bridge",
  295.         itemCount = itemCount,
  296.         craftableCount = craftableCount,
  297.         quantities = quantities,
  298.         craftable = craftable
  299.     }
  300. end
  301.  
  302. return ae2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement