Advertisement
alexnolan

AE2 Crafting System

Mar 27th, 2025 (edited)
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.56 KB | None | 0 0
  1. -- AE2 Crafting Interface System
  2. -- All-in-one file version
  3.  
  4. -- Configuration
  5. local MONITOR_WIDTH = 5  -- 5 monitors wide
  6. local MONITOR_HEIGHT = 3 -- 3 monitors high
  7.  
  8. -- Global variables
  9. local monitors = {}
  10. local bridge = nil
  11. local totalWidth = 0
  12. local totalHeight = 0
  13. local currentScreen = "main" -- main, category, quantity
  14. local selectedCategory = nil
  15. local selectedItem = nil
  16. local buttons = {}
  17.  
  18. -- Categories and items (focused on popular items)
  19. local categories = {
  20.     {
  21.         id = "building",
  22.         name = "Building Blocks",
  23.         items = {
  24.             {id = "minecraft:stone", name = "Stone"},
  25.             {id = "minecraft:oak_planks", name = "Oak Planks"},
  26.             {id = "minecraft:glass", name = "Glass"},
  27.             {id = "minecraft:cobblestone", name = "Cobblestone"},
  28.             {id = "minecraft:smooth_stone", name = "Smooth Stone"},
  29.             {id = "minecraft:bricks", name = "Bricks"},
  30.             {id = "minecraft:stone_bricks", name = "Stone Bricks"},
  31.             {id = "minecraft:quartz_block", name = "Quartz Block"}
  32.         }
  33.     },
  34.     {
  35.         id = "tools",
  36.         name = "Tools",
  37.         items = {
  38.             {id = "minecraft:diamond_pickaxe", name = "Diamond Pickaxe"},
  39.             {id = "minecraft:diamond_axe", name = "Diamond Axe"},
  40.             {id = "minecraft:diamond_shovel", name = "Diamond Shovel"},
  41.             {id = "minecraft:diamond_hoe", name = "Diamond Hoe"},
  42.             {id = "minecraft:iron_pickaxe", name = "Iron Pickaxe"},
  43.             {id = "minecraft:iron_axe", name = "Iron Axe"},
  44.             {id = "minecraft:shears", name = "Shears"},
  45.             {id = "minecraft:fishing_rod", name = "Fishing Rod"}
  46.         }
  47.     },
  48.     {
  49.         id = "redstone",
  50.         name = "Redstone",
  51.         items = {
  52.             {id = "minecraft:redstone", name = "Redstone Dust"},
  53.             {id = "minecraft:repeater", name = "Repeater"},
  54.             {id = "minecraft:piston", name = "Piston"},
  55.             {id = "minecraft:sticky_piston", name = "Sticky Piston"},
  56.             {id = "minecraft:hopper", name = "Hopper"},
  57.             {id = "minecraft:observer", name = "Observer"},
  58.             {id = "minecraft:dispenser", name = "Dispenser"},
  59.             {id = "minecraft:dropper", name = "Dropper"}
  60.         }
  61.     },
  62.     {
  63.         id = "combat",
  64.         name = "Combat",
  65.         items = {
  66.             {id = "minecraft:diamond_sword", name = "Diamond Sword"},
  67.             {id = "minecraft:bow", name = "Bow"},
  68.             {id = "minecraft:arrow", name = "Arrow"},
  69.             {id = "minecraft:shield", name = "Shield"},
  70.             {id = "minecraft:diamond_helmet", name = "Diamond Helmet"},
  71.             {id = "minecraft:diamond_chestplate", name = "Diamond Chestplate"},
  72.             {id = "minecraft:diamond_leggings", name = "Diamond Leggings"},
  73.             {id = "minecraft:diamond_boots", name = "Diamond Boots"}
  74.         }
  75.     },
  76.     {
  77.         id = "food",
  78.         name = "Food",
  79.         items = {
  80.             {id = "minecraft:bread", name = "Bread"},
  81.             {id = "minecraft:cooked_beef", name = "Steak"},
  82.             {id = "minecraft:golden_apple", name = "Golden Apple"},
  83.             {id = "minecraft:cooked_chicken", name = "Cooked Chicken"},
  84.             {id = "minecraft:cake", name = "Cake"},
  85.             {id = "minecraft:cookie", name = "Cookie"},
  86.             {id = "minecraft:pumpkin_pie", name = "Pumpkin Pie"},
  87.             {id = "minecraft:golden_carrot", name = "Golden Carrot"}
  88.         }
  89.     },
  90.     {
  91.         id = "transportation",
  92.         name = "Transportation",
  93.         items = {
  94.             {id = "minecraft:rail", name = "Rail"},
  95.             {id = "minecraft:powered_rail", name = "Powered Rail"},
  96.             {id = "minecraft:detector_rail", name = "Detector Rail"},
  97.             {id = "minecraft:minecart", name = "Minecart"},
  98.             {id = "minecraft:chest_minecart", name = "Storage Minecart"},
  99.             {id = "minecraft:furnace_minecart", name = "Furnace Minecart"},
  100.             {id = "minecraft:hopper_minecart", name = "Hopper Minecart"},
  101.             {id = "minecraft:boat", name = "Boat"}
  102.         }
  103.     }
  104. }
  105.  
  106. -- Initialize the system
  107. function init()
  108.     -- Find monitors - improved detection
  109.     local allPeripherals = peripheral.getNames()
  110.     local monitorCount = 0
  111.    
  112.     -- Print all available peripherals for debugging
  113.     print("Available peripherals:")
  114.     for _, name in ipairs(allPeripherals) do
  115.         print("- " .. name .. " (" .. peripheral.getType(name) .. ")")
  116.     end
  117.    
  118.     -- First try the standard naming convention with a wider range
  119.     for i = 0, 15 do  -- Try a wider range of indices
  120.         local name = "monitor_" .. i
  121.         if peripheral.isPresent(name) then
  122.             print("Found monitor: " .. name)
  123.             monitors[monitorCount+1] = peripheral.wrap(name)
  124.             monitors[monitorCount+1].setTextScale(0.5)
  125.             monitors[monitorCount+1].clear()
  126.             monitorCount = monitorCount + 1
  127.         end
  128.     end
  129.    
  130.     -- If we didn't find enough monitors, look for any monitor peripherals
  131.     if monitorCount < (MONITOR_WIDTH * MONITOR_HEIGHT) then
  132.         for _, name in ipairs(allPeripherals) do
  133.             -- Check if it's a monitor and not already in our list
  134.             if peripheral.getType(name) == "monitor" then
  135.                 local alreadyAdded = false
  136.                 for _, existingMonitor in pairs(monitors) do
  137.                     if peripheral.getName(existingMonitor) == name then
  138.                         alreadyAdded = true
  139.                         break
  140.                     end
  141.                 end
  142.                
  143.                 if not alreadyAdded and monitorCount < (MONITOR_WIDTH * MONITOR_HEIGHT) then
  144.                     monitorCount = monitorCount + 1
  145.                     monitors[monitorCount] = peripheral.wrap(name)
  146.                     monitors[monitorCount].setTextScale(0.5)
  147.                     monitors[monitorCount].clear()
  148.                     print("Added monitor: " .. name)
  149.                 end
  150.             end
  151.         end
  152.     end
  153.    
  154.     if monitorCount < (MONITOR_WIDTH * MONITOR_HEIGHT) then
  155.         print("Warning: Not all monitors detected. Found " .. monitorCount .. "/" .. (MONITOR_WIDTH * MONITOR_HEIGHT))
  156.         -- If we found at least one monitor, let's continue anyway
  157.         if monitorCount > 0 then
  158.             print("Continuing with available monitors...")
  159.             -- Adjust the expected dimensions based on what we have
  160.             MONITOR_WIDTH = 1
  161.             MONITOR_HEIGHT = monitorCount
  162.         else
  163.             return false
  164.         end
  165.     else
  166.         print("Successfully connected to " .. monitorCount .. " monitors")
  167.     end
  168.    
  169.     -- Find ME Bridge
  170.     bridge = peripheral.find("meBridge")
  171.     if not bridge then
  172.         print("Error: ME Bridge not found!")
  173.         return false
  174.     end
  175.    
  176.     -- Get monitor dimensions
  177.     if monitorCount > 0 then
  178.         local w, h = monitors[1].getSize()
  179.         totalWidth = w * MONITOR_WIDTH
  180.         totalHeight = h * MONITOR_HEIGHT
  181.         print("Total display size: " .. totalWidth .. "x" .. totalHeight)
  182.     end
  183.    
  184.     return true
  185. end
  186.  
  187.  
  188.  
  189. -- Convert global coordinates to monitor-specific coordinates
  190. function getMonitorAt(x, y)
  191.     if x < 1 or y < 1 or x > totalWidth or y > totalHeight then
  192.         return nil
  193.     end
  194.    
  195.     local monitorWidth = totalWidth / MONITOR_WIDTH
  196.     local monitorHeight = totalHeight / MONITOR_HEIGHT
  197.    
  198.     local monX = math.ceil(x / monitorWidth)
  199.     local monY = math.ceil(y / monitorHeight)
  200.     local index = (monY - 1) * MONITOR_WIDTH + monX
  201.    
  202.     -- Check if the monitor exists at this index
  203.     if not monitors[index] then
  204.         print("Warning: No monitor found at index " .. index .. " (position " .. monX .. "," .. monY .. ")")
  205.         return nil
  206.     end
  207.    
  208.     local relX = (x - 1) % monitorWidth + 1
  209.     local relY = (y - 1) % monitorHeight + 1
  210.    
  211.     return monitors[index], relX, relY
  212. end
  213.  
  214.  
  215. -- Clear all monitors
  216. function clearScreen()
  217.     for _, monitor in pairs(monitors) do
  218.         monitor.clear()
  219.     end
  220. end
  221.  
  222. -- Write text at global coordinates
  223. function writeText(x, y, text, textColor, bgColor)
  224.     local monitor, relX, relY = getMonitorAt(x, y)
  225.     if monitor then
  226.         if textColor then monitor.setTextColor(textColor) end
  227.         if bgColor then monitor.setBackgroundColor(bgColor) end
  228.         monitor.setCursorPos(relX, relY)
  229.         monitor.write(text)
  230.     end
  231. end
  232.  
  233. -- Draw a filled rectangle
  234. function fillRect(startX, startY, endX, endY, color)
  235.     for y = startY, endY do
  236.         for x = startX, endX do
  237.             local monitor, relX, relY = getMonitorAt(x, y)
  238.             if monitor then
  239.                 monitor.setBackgroundColor(color)
  240.                 monitor.setCursorPos(relX, relY)
  241.                 monitor.write(" ")
  242.             end
  243.         end
  244.     end
  245. end
  246.  
  247. -- Draw a button with text
  248. function drawButton(x, y, width, height, text, bgColor, textColor)
  249.     fillRect(x, y, x + width - 1, y + height - 1, bgColor or colors.blue)
  250.    
  251.     -- Center the text
  252.     local textX = x + math.floor((width - #text) / 2)
  253.     local textY = y + math.floor(height / 2)
  254.     writeText(textX, textY, text, textColor or colors.white, bgColor or colors.blue)
  255.    
  256.     return {
  257.         x = x,
  258.         y = y,
  259.         width = width,
  260.         height = height,
  261.         text = text
  262.     }
  263. end
  264.  
  265. -- Check if coordinates are within a button
  266. function isInButton(button, x, y)
  267.     return x >= button.x and x < button.x + button.width and
  268.            y >= button.y and y < button.y + button.height
  269. end
  270.  
  271. -- Draw a centered title
  272. function drawTitle(y, text, color)
  273.     local textX = math.floor((totalWidth - #text) / 2) + 1
  274.     writeText(textX, y, text, color or colors.yellow, colors.black)
  275. end
  276.  
  277. -- Get AE2 system status
  278. function getAE2Status()
  279.     if not bridge then return {items = "Unknown", crafting = "Unknown"} end
  280.    
  281.     local status = {
  282.         items = "0",
  283.         crafting = "None"
  284.     }
  285.    
  286.     -- Get item count
  287.     local items = bridge.listItems()
  288.     status.items = tostring(#items)
  289.    
  290.     -- Get crafting status
  291.     local crafting = bridge.getCraftingCPUs()
  292.     local activeCPUs = 0
  293.     for _, cpu in pairs(crafting) do
  294.         if cpu.busy then activeCPUs = activeCPUs + 1 end
  295.     end
  296.    
  297.     if activeCPUs > 0 then
  298.         status.crafting = activeCPUs .. " Active"
  299.     else
  300.         status.crafting = "Idle"
  301.     end
  302.    
  303.     return status
  304. end
  305.  
  306. -- Craft an item using AE2
  307. function craftItem(itemId, amount)
  308.     if not bridge then return false, "ME Bridge not connected" end
  309.    
  310.     local success = false
  311.     local message = "Unknown error"
  312.    
  313.     -- Print debug info
  314.     print("Attempting to craft: " .. itemId .. " x" .. amount)
  315.    
  316.     -- Check if the item exists in the system
  317.     local items = bridge.listItems()
  318.     local itemExists = false
  319.     for _, item in pairs(items) do
  320.         if item.name == itemId or item.fingerprint == itemId then
  321.             itemExists = true
  322.             print("Item found in system: " .. item.name)
  323.             break
  324.         end
  325.     end
  326.    
  327.     if not itemExists then
  328.         print("Warning: Item not found in AE2 system")
  329.     end
  330.    
  331.     -- Try to craft the item
  332.     local status, result = pcall(function()
  333.         return bridge.craftItem({name = itemId, count = amount})
  334.     end)
  335.    
  336.     if status then
  337.         if result then
  338.             success = true
  339.             message = "Crafting started"
  340.             print("Crafting successful")
  341.         else
  342.             message = "Failed to craft item"
  343.             print("Crafting failed - item may not have a pattern")
  344.         end
  345.     else
  346.         message = "Error: " .. tostring(result)
  347.         print("Crafting error: " .. tostring(result))
  348.     end
  349.    
  350.     -- Try alternative method if first one failed
  351.     if not success then
  352.         print("Trying alternative crafting method...")
  353.         status, result = pcall(function()
  354.             return bridge.requestCrafting({name = itemId, count = amount})
  355.         end)
  356.        
  357.         if status and result then
  358.             success = true
  359.             message = "Crafting started (alt method)"
  360.             print("Alternative crafting successful")
  361.         end
  362.     end
  363.    
  364.     return success, message
  365. end
  366.  
  367.  
  368. -- Draw the main category selection screen
  369. function drawMainScreen()
  370.     clearScreen()
  371.     buttons = {}
  372.    
  373.     -- Draw title
  374.     drawTitle(2, "AE2 Crafting System", colors.yellow)
  375.    
  376.     -- Draw categories
  377.     local cols = 3
  378.     local rows = math.ceil(#categories / cols)
  379.     local buttonWidth = 20
  380.     local buttonHeight = 3
  381.     local startX = math.floor((totalWidth - (cols * buttonWidth)) / 2) + 1
  382.     local startY = 5
  383.    
  384.     for i, category in ipairs(categories) do
  385.         local col = (i - 1) % cols
  386.         local row = math.floor((i - 1) / cols)
  387.         local x = startX + (col * (buttonWidth + 2))
  388.         local y = startY + (row * (buttonHeight + 1))
  389.        
  390.         local button = drawButton(x, y, buttonWidth, buttonHeight, category.name, colors.blue)
  391.         button.action = "category"
  392.         button.category = category
  393.         table.insert(buttons, button)
  394.     end
  395.    
  396.     -- Draw status
  397.     local status = getAE2Status()
  398.     writeText(2, totalHeight - 1, "Items: " .. status.items, colors.white)
  399.     writeText(totalWidth - 20, totalHeight - 1, "Crafting: " .. status.crafting, colors.white)
  400. end
  401.  
  402. -- Draw the items in a category
  403. function drawCategoryScreen(category)
  404.     clearScreen()
  405.     buttons = {}
  406.    
  407.     -- Draw title
  408.     drawTitle(2, category.name .. " Items", colors.yellow)
  409.    
  410.     -- Draw items
  411.     local itemList = category.items
  412.     local cols = 4
  413.     local rows = math.ceil(#itemList / cols)
  414.     local buttonWidth = 16
  415.     local buttonHeight = 3
  416.     local startX = math.floor((totalWidth - (cols * buttonWidth)) / 2) + 1
  417.     local startY = 5
  418.    
  419.     for i, item in ipairs(itemList) do
  420.         local col = (i - 1) % cols
  421.         local row = math.floor((i - 1) / cols)
  422.         local x = startX + (col * (buttonWidth + 2))
  423.         local y = startY + (row * (buttonHeight + 1))
  424.        
  425.         local button = drawButton(x, y, buttonWidth, buttonHeight, item.name, colors.cyan)
  426.         button.action = "item"
  427.         button.item = item
  428.         table.insert(buttons, button)
  429.     end
  430.    
  431.     -- Back button
  432.     local backButton = drawButton(2, totalHeight - 3, 10, 3, "Back", colors.red)
  433.     backButton.action = "back"
  434.     table.insert(buttons, backButton)
  435. end
  436.  
  437. -- Draw quantity selection screen
  438. function drawQuantityScreen(item)
  439.     clearScreen()
  440.     buttons = {}
  441.    
  442.     -- Draw title
  443.     drawTitle(2, "Craft: " .. item.name, colors.yellow)
  444.    
  445.     -- Draw quantity options
  446.     local quantities = {1, 4, 8, 16, 32, 64}
  447.     local buttonWidth = 10
  448.     local buttonHeight = 3
  449.     local startX = math.floor((totalWidth - (#quantities * buttonWidth)) / 2) + 1
  450.     local startY = totalHeight / 2 - buttonHeight
  451.    
  452.     for i, amount in ipairs(quantities) do
  453.         local x = startX + ((i-1) * (buttonWidth + 2))
  454.         local button = drawButton(x, startY, buttonWidth, buttonHeight, tostring(amount), colors.green)
  455.         button.action = "quantity"
  456.         button.amount = amount
  457.         table.insert(buttons, button)
  458.     end
  459.    
  460.     -- Back button
  461.     local backButton = drawButton(2, totalHeight - 3, 10, 3, "Back", colors.red)
  462.     backButton.action = "back"
  463.     table.insert(buttons, backButton)
  464. end
  465.  
  466. -- Handle touch events
  467. function handleTouch(x, y)
  468.     for _, button in ipairs(buttons) do
  469.         if isInButton(button, x, y) then
  470.             if button.action == "category" then
  471.                 selectedCategory = button.category
  472.                 currentScreen = "category"
  473.                 drawCategoryScreen(selectedCategory)
  474.             elseif button.action == "item" then
  475.                 selectedItem = button.item
  476.                 currentScreen = "quantity"
  477.                 drawQuantityScreen(selectedItem)
  478.             elseif button.action == "quantity" then
  479.                 local success, message = craftItem(selectedItem.id, button.amount)
  480.                
  481.                 -- Show feedback
  482.                 if success then
  483.                     fillRect(totalWidth/4, totalHeight/2, totalWidth*3/4, totalHeight/2 + 2, colors.green)
  484.                     writeText(totalWidth/2 - 10, totalHeight/2 + 1, "Crafting " .. button.amount .. " " .. selectedItem.name, colors.white, colors.green)
  485.                 else
  486.                     fillRect(totalWidth/4, totalHeight/2, totalWidth*3/4, totalHeight/2 + 2, colors.red)
  487.                     writeText(totalWidth/2 - 10, totalHeight/2 + 1, "Error: " .. message, colors.white, colors.red)
  488.                 end
  489.                
  490.                 -- Wait briefly to show feedback
  491.                 sleep(1)
  492.                 drawCategoryScreen(selectedCategory)
  493.             elseif button.action == "back" then
  494.                 if currentScreen == "category" then
  495.                     currentScreen = "main"
  496.                     drawMainScreen()
  497.                 elseif currentScreen == "quantity" then
  498.                     currentScreen = "category"
  499.                     drawCategoryScreen(selectedCategory)
  500.                 end
  501.             end
  502.             break
  503.         end
  504.     end
  505. end
  506.  
  507. -- Main program
  508. function run()
  509.     if not init() then
  510.         print("Failed to initialize the system")
  511.         return
  512.     end
  513.    
  514.     -- Draw initial screen
  515.     drawMainScreen()
  516.    
  517.     -- Main event loop
  518.     while true do
  519.         local event, side, x, y = os.pullEvent("monitor_touch")
  520.         handleTouch(x, y)
  521.     end
  522. end
  523.  
  524. -- Start the program
  525. run()
  526.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement