Advertisement
TheYellowBush

Untitled

Jul 8th, 2025 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.11 KB | Source Code | 0 0
  1. -- AE2 Stockpile Manager for CC: Tweaked
  2. -- Manages automatic crafting requests to maintain item quantities
  3. -- Compatible with CC: Tweaked and modern AE2 versions
  4.  
  5. -- Configuration
  6. local CONFIG_FILE = "ae2_stockpile.txt"
  7. local TEST_MODE = false -- Set to true for emulator testing
  8. local INTERFACE_SIDE = "back"
  9. local INTERFACE_TYPE = "meBridge" -- Advanced Peripherals ME Bridge
  10. local CHECK_INTERVAL = 30 -- seconds between checks
  11.  
  12. -- Global variables
  13. local ae2Interface = nil
  14. local monitor = nil
  15. local settings = {}
  16. local running = false
  17. local currentCraftingJobs = {}
  18. local lastUpdate = 0
  19.  
  20. -- Helper function for formatted printing
  21. local function printf(format, ...)
  22.     print(string.format(format, ...))
  23. end
  24.  
  25. -- Test mode simulation data
  26. local testItems = {
  27.     ["minecraft:iron_ingot"] = 450,
  28.     ["minecraft:gold_ingot"] = 200,
  29.     ["minecraft:diamond"] = 50,
  30.     ["minecraft:redstone"] = 1000,
  31.     ["minecraft:coal"] = 800,
  32.     ["minecraft:stick"] = 300
  33. }
  34.  
  35. local testCraftableItems = {
  36.     "minecraft:iron_ingot",
  37.     "minecraft:gold_ingot",
  38.     "minecraft:diamond_sword",
  39.     "minecraft:stick",
  40.     "minecraft:crafting_table"
  41. }
  42.  
  43. local testCraftingQueue = {}
  44.  
  45. -- Fake AE2 interface for testing
  46. local function createFakeInterface()
  47.     return {
  48.         getAvailableItems = function()
  49.             local items = {}
  50.             for name, count in pairs(testItems) do
  51.                 table.insert(items, {
  52.                     type = "item",
  53.                     id = name,
  54.                     displayName = name:gsub("minecraft:", ""):gsub("_", " "),
  55.                     amount = count
  56.                 })
  57.             end
  58.             return items
  59.         end,
  60.        
  61.         getCraftableItems = function()
  62.             local items = {}
  63.             for _, name in pairs(testCraftableItems) do
  64.                 table.insert(items, {
  65.                     type = "item",
  66.                     id = name,
  67.                     displayName = name:gsub("minecraft:", ""):gsub("_", " ")
  68.                 })
  69.             end
  70.             return items
  71.         end,
  72.        
  73.         getCraftingJob = function(item, amount)
  74.             print("FAKE: Requesting craft of " .. amount .. "x " .. item.id)
  75.             table.insert(testCraftingQueue, {item = item.id, amount = amount, time = os.clock()})
  76.             return {id = "fake_job_" .. os.clock()}
  77.         end,
  78.        
  79.         getCraftingJobs = function()
  80.             return testCraftingQueue
  81.         end
  82.     }
  83. end
  84.  
  85. -- Initialize AE2 connection
  86. local function initAE2()
  87.     if TEST_MODE then
  88.         print("Starting in TEST MODE")
  89.         ae2Interface = createFakeInterface()
  90.         return true
  91.     else
  92.         -- Try to find Advanced Peripherals ME Bridge
  93.         local bridge = peripheral.find(INTERFACE_TYPE)
  94.         if bridge then
  95.             print("Found Advanced Peripherals ME Bridge")
  96.             ae2Interface = bridge
  97.            
  98.             -- Test the connection
  99.             local success, items = pcall(function() return ae2Interface.listItems() end)
  100.             if success then
  101.                 print("✓ ME Bridge connected successfully - found " .. #items .. " items")
  102.                 return true
  103.             else
  104.                 print("✗ ME Bridge connection failed: " .. tostring(items))
  105.                 return false
  106.             end
  107.         else
  108.             -- Fallback: try to find on specific side
  109.             local interface = peripheral.wrap(INTERFACE_SIDE)
  110.             if interface then
  111.                 local ptype = peripheral.getType(INTERFACE_SIDE)
  112.                 print("Found peripheral: " .. ptype .. " on " .. INTERFACE_SIDE)
  113.                
  114.                 if ptype == INTERFACE_TYPE then
  115.                     ae2Interface = interface
  116.                     print("✓ ME Bridge found on " .. INTERFACE_SIDE)
  117.                     return true
  118.                 else
  119.                     print("✗ Wrong peripheral type. Expected '" .. INTERFACE_TYPE .. "', got '" .. ptype .. "'")
  120.                 end
  121.             else
  122.                 print("✗ No peripheral found")
  123.             end
  124.            
  125.             -- Show available peripherals for debugging
  126.             local peripherals = peripheral.getNames()
  127.             if #peripherals > 0 then
  128.                 print("Available peripherals:")
  129.                 for _, name in pairs(peripherals) do
  130.                     local ptype = peripheral.getType(name)
  131.                     print("  " .. name .. " (" .. ptype .. ")")
  132.                 end
  133.                 print()
  134.                 print("Make sure you have:")
  135.                 print("1. Advanced Peripherals mod installed")
  136.                 print("2. ME Bridge block placed and connected to your ME system")
  137.                 print("3. ME Bridge connected to computer or on the peripheral network")
  138.             end
  139.             return false
  140.         end
  141.     end
  142. end
  143.  
  144. -- Initialize monitor/display
  145. local function initMonitor()
  146.     local foundMonitor = peripheral.find("monitor")
  147.     if foundMonitor then
  148.         monitor = foundMonitor
  149.         monitor.clear()
  150.         monitor.setTextScale(0.5)
  151.         print("✓ Monitor connected")
  152.         return true
  153.     else
  154.         print("No monitor found (optional)")
  155.         return false
  156.     end
  157. end
  158.  
  159. -- Load settings from file
  160. local function loadSettings()
  161.     if fs.exists(CONFIG_FILE) then
  162.         local file = fs.open(CONFIG_FILE, "r")
  163.         if file then
  164.             local data = file.readAll()
  165.             file.close()
  166.             settings = textutils.unserialize(data) or {}
  167.             print("Loaded " .. #settings .. " configured items")
  168.         end
  169.     else
  170.         settings = {}
  171.         print("No existing configuration found")
  172.     end
  173. end
  174.  
  175. -- Save settings to file
  176. local function saveSettings()
  177.     local file = fs.open(CONFIG_FILE, "w")
  178.     if file then
  179.         file.write(textutils.serialize(settings))
  180.         file.close()
  181.         print("Settings saved")
  182.     else
  183.         print("ERROR: Could not save settings")
  184.     end
  185. end
  186.  
  187. -- Clear screen and show header
  188. local function showHeader()
  189.     term.clear()
  190.     term.setCursorPos(1, 1)
  191.     print("=== AE2 Stockpile Manager ===")
  192.     if TEST_MODE then
  193.         print("*** RUNNING IN TEST MODE ***")
  194.     end
  195.     if monitor then
  196.         print("Monitor: Connected")
  197.     end
  198.     print()
  199. end
  200.  
  201. -- Update monitor display
  202. local function updateMonitor()
  203.     if not monitor then return end
  204.    
  205.     monitor.clear()
  206.     monitor.setCursorPos(1, 1)
  207.     monitor.setBackgroundColor(colors.black)
  208.     monitor.setTextColor(colors.white)
  209.    
  210.     -- Header
  211.     monitor.setTextColor(colors.yellow)
  212.     monitor.write("=== AE2 Stockpile Manager ===")
  213.     monitor.setCursorPos(1, 2)
  214.     monitor.setTextColor(colors.gray)
  215.     monitor.write("Last Update: " .. os.date("%H:%M:%S"))
  216.    
  217.     local line = 4
  218.    
  219.     -- System Status
  220.     monitor.setCursorPos(1, line)
  221.     monitor.setTextColor(colors.lightBlue)
  222.     monitor.write("System Status:")
  223.     line = line + 1
  224.    
  225.     monitor.setCursorPos(1, line)
  226.     if ae2Interface and not ae2Interface._isBasicInventory then
  227.         monitor.setTextColor(colors.lime)
  228.         monitor.write("Connected - ME Bridge Active")
  229.     elseif TEST_MODE then
  230.         monitor.setTextColor(colors.orange)
  231.         monitor.write("Test Mode Active")
  232.     else
  233.         monitor.setTextColor(colors.red)
  234.         monitor.write("Disconnected")
  235.     end
  236.     line = line + 2
  237.    
  238.     -- Current Crafting Jobs
  239.     monitor.setCursorPos(1, line)
  240.     monitor.setTextColor(colors.lightBlue)
  241.     monitor.write("Current Crafting:")
  242.     line = line + 1
  243.    
  244.     if #currentCraftingJobs > 0 then
  245.         for i, job in ipairs(currentCraftingJobs) do
  246.             if line <= 15 then -- Don't overflow monitor
  247.                 monitor.setCursorPos(1, line)
  248.                 monitor.setTextColor(colors.yellow)
  249.                 local itemName = job.item:gsub("minecraft:", ""):gsub("_", " ")
  250.                 monitor.write("• " .. job.amount .. "x " .. itemName)
  251.                 line = line + 1
  252.             end
  253.         end
  254.     else
  255.         monitor.setCursorPos(1, line)
  256.         monitor.setTextColor(colors.gray)
  257.         monitor.write("No active crafting jobs")
  258.         line = line + 1
  259.     end
  260.     line = line + 1
  261.    
  262.     -- Monitored Items Status
  263.     monitor.setCursorPos(1, line)
  264.     monitor.setTextColor(colors.lightBlue)
  265.     monitor.write("Monitored Items:")
  266.     line = line + 1
  267.    
  268.     if #settings > 0 then
  269.         local quantities = getCurrentQuantities()
  270.         for i, config in ipairs(settings) do
  271.             if line <= 19 then -- Leave room at bottom
  272.                 local currentAmount = quantities[config.item] or 0
  273.                 local itemName = config.item:gsub("minecraft:", ""):gsub("_", " ")
  274.                
  275.                 monitor.setCursorPos(1, line)
  276.                
  277.                 -- Color code based on stock level
  278.                 if currentAmount >= config.target then
  279.                     monitor.setTextColor(colors.lime)
  280.                     monitor.write("✓ ")
  281.                 elseif currentAmount >= config.threshold then
  282.                     monitor.setTextColor(colors.yellow)
  283.                     monitor.write("○ ")
  284.                 else
  285.                     monitor.setTextColor(colors.red)
  286.                     monitor.write("! ")
  287.                 end
  288.                
  289.                 monitor.setTextColor(colors.white)
  290.                 monitor.write(string.format("%-12s %d/%d",
  291.                     itemName:sub(1, 12), currentAmount, config.target))
  292.                 line = line + 1
  293.             end
  294.         end
  295.     else
  296.         monitor.setCursorPos(1, line)
  297.         monitor.setTextColor(colors.gray)
  298.         monitor.write("No items configured")
  299.     end
  300.    
  301.     -- Footer
  302.     local maxY = select(2, monitor.getSize())
  303.     monitor.setCursorPos(1, maxY)
  304.     monitor.setTextColor(colors.gray)
  305.     monitor.write("Status: " .. (running and "MONITORING" or "IDLE"))
  306. end
  307.  
  308. -- Get current item quantities from AE2
  309. local function getCurrentQuantities()
  310.     if not ae2Interface then return {} end
  311.    
  312.     if ae2Interface._isBasicInventory then
  313.         -- Handle basic inventory interface (fallback)
  314.         local success, items = pcall(function()
  315.             return ae2Interface.list()
  316.         end)
  317.        
  318.         if not success then
  319.             print("ERROR: Failed to get items from inventory: " .. tostring(items))
  320.             return {}
  321.         end
  322.        
  323.         local quantities = {}
  324.         items = items or {}
  325.        
  326.         for slot, item in pairs(items) do
  327.             if item and item.name then
  328.                 quantities[item.name] = (quantities[item.name] or 0) + item.count
  329.             end
  330.         end
  331.        
  332.         return quantities
  333.     else
  334.         -- Handle Advanced Peripherals ME Bridge
  335.         local success, items = pcall(function()
  336.             return ae2Interface.listItems()
  337.         end)
  338.        
  339.         if not success then
  340.             print("ERROR: Failed to get items from ME Bridge: " .. tostring(items))
  341.             return {}
  342.         end
  343.        
  344.         items = items or {}
  345.         local quantities = {}
  346.        
  347.         for _, item in pairs(items) do
  348.             local name = item.name or item.id
  349.             local count = item.amount or item.count or 0
  350.             if name then
  351.                 quantities[name] = count
  352.             end
  353.         end
  354.        
  355.         return quantities
  356.     end
  357. end
  358.  
  359. -- Get craftable items from AE2
  360. local function getCraftableItems()
  361.     if not ae2Interface then return {} end
  362.    
  363.     if ae2Interface._isBasicInventory then
  364.         print("Craftable items not available in basic inventory mode")
  365.         return {}
  366.     end
  367.    
  368.     local success, items = pcall(function()
  369.         return ae2Interface.listCraftableItems()
  370.     end)
  371.    
  372.     if not success then
  373.         print("ERROR: Failed to get craftable items: " .. tostring(items))
  374.         return {}
  375.     end
  376.    
  377.     local craftable = {}
  378.     items = items or {}
  379.    
  380.     for _, item in pairs(items) do
  381.         local name = item.name or item.id
  382.         if name then
  383.             craftable[name] = item
  384.         end
  385.     end
  386.    
  387.     return craftable
  388. end
  389.  
  390. -- Find item by name in AE2 system
  391. local function findItem(itemName, inCraftable)
  392.     if not ae2Interface then return nil end
  393.    
  394.     local items = {}
  395.     if inCraftable then
  396.         local craftableItems = getCraftableItems()
  397.         for name, item in pairs(craftableItems) do
  398.             table.insert(items, item)
  399.         end
  400.     else
  401.         local success, availableItems = pcall(function()
  402.             if ae2Interface._isBasicInventory then
  403.                 return ae2Interface.list()
  404.             else
  405.                 return ae2Interface.listItems()
  406.             end
  407.         end)
  408.        
  409.         if success and availableItems then
  410.             items = availableItems
  411.         end
  412.     end
  413.    
  414.     for _, item in pairs(items) do
  415.         local name = item.name or item.id
  416.         local displayName = item.displayName or item.label or name
  417.        
  418.         if name == itemName or (displayName and displayName:lower():find(itemName:lower())) then
  419.             return item
  420.         end
  421.     end
  422.     return nil
  423. end
  424.  
  425. -- Request crafting for an item
  426. local function requestCrafting(itemName, amount)
  427.     if not ae2Interface then
  428.         print("ERROR: No AE2 interface connected")
  429.         return false
  430.     end
  431.    
  432.     if ae2Interface._isBasicInventory then
  433.         print("ERROR: Cannot request crafting - basic inventory mode")
  434.         print("You need Advanced Peripherals ME Bridge for auto-crafting")
  435.         return false
  436.     end
  437.    
  438.     -- For Advanced Peripherals ME Bridge, we can craft directly by item name
  439.     local success, result = pcall(function()
  440.         return ae2Interface.craftItem({name = itemName, count = amount})
  441.     end)
  442.    
  443.     if success and result then
  444.         print("Crafting requested: " .. amount .. "x " .. itemName)
  445.        
  446.         -- Add to current crafting jobs for display
  447.         table.insert(currentCraftingJobs, {
  448.             item = itemName,
  449.             amount = amount,
  450.             time = os.clock()
  451.         })
  452.        
  453.         updateMonitor()
  454.         return true
  455.     else
  456.         print("ERROR: Failed to request crafting for " .. itemName)
  457.         if not success then
  458.             print("Error details: " .. tostring(result))
  459.         end
  460.        
  461.         -- Check if item is craftable
  462.         local craftableItem = findItem(itemName, true)
  463.         if not craftableItem then
  464.             print("Item '" .. itemName .. "' is not craftable")
  465.         end
  466.        
  467.         return false
  468.     end
  469. end
  470.  
  471. -- Monitor items and request crafting as needed
  472. local function monitorAndCraft()
  473.     local quantities = getCurrentQuantities()
  474.    
  475.     -- Clean up old crafting jobs (remove jobs older than 5 minutes)
  476.     for i = #currentCraftingJobs, 1, -1 do
  477.         local job = currentCraftingJobs[i]
  478.         if os.clock() - job.time > 300 then -- 5 minutes
  479.             table.remove(currentCraftingJobs, i)
  480.         end
  481.     end
  482.    
  483.     for _, config in pairs(settings) do
  484.         local currentAmount = quantities[config.item] or 0
  485.        
  486.         if currentAmount < config.threshold then
  487.             local craftAmount = config.target - currentAmount
  488.             print("LOW STOCK: " .. config.item .. " (" .. currentAmount .. "/" .. config.threshold .. ")")
  489.             print("Requesting craft of " .. craftAmount .. " items...")
  490.            
  491.             if requestCrafting(config.item, craftAmount) then
  492.                 -- Add delay to prevent spam requests
  493.                 os.sleep(2)
  494.             end
  495.         end
  496.     end
  497.    
  498.     updateMonitor()
  499. end
  500.  
  501. -- Show main menu
  502. local function showMainMenu()
  503.     showHeader()
  504.     print("1. View Current Settings")
  505.     print("2. Add/Edit Item Configuration")
  506.     print("3. Remove Item Configuration")
  507.     print("4. Start Monitoring")
  508.     print("5. Test Crafting Request")
  509.     print("6. View AE2 System Status")
  510.     print("7. Toggle Test Mode (" .. (TEST_MODE and "ON" or "OFF") .. ")")
  511.     print("8. Exit")
  512.     print()
  513.     write("Select option: ")
  514. end
  515.  
  516. -- Show current settings
  517. local function viewSettings()
  518.     showHeader()
  519.     print("Current Item Configurations:")
  520.     print()
  521.    
  522.     if #settings == 0 then
  523.         print("No items configured")
  524.     else
  525.         print("Item Name                    Target    Threshold")
  526.         print("----------------------------------------")
  527.         for i, config in pairs(settings) do
  528.             local name = config.item:gsub("minecraft:", "")
  529.             printf("%-28s %-9d %d", name, config.target, config.threshold)
  530.         end
  531.     end
  532.    
  533.     print()
  534.     print("Press any key to continue...")
  535.     os.pullEvent("key")
  536. end
  537.  
  538. -- Show AE2 system status
  539. local function viewSystemStatus()
  540.     showHeader()
  541.     print("AE2 System Status:")
  542.     print()
  543.    
  544.     if not ae2Interface then
  545.         print("Not connected to any interface")
  546.         print("Enable test mode or install Advanced Peripherals")
  547.         print()
  548.         print("Press any key to continue...")
  549.         os.pullEvent("key")
  550.         return
  551.     end
  552.    
  553.     if ae2Interface._isBasicInventory then
  554.         print("Connection Type: Basic Inventory (Limited)")
  555.         print("⚠ No auto-crafting support available")
  556.         print()
  557.         print("To get full AE2 integration:")
  558.         print("1. Install Advanced Peripherals mod")
  559.         print("2. Craft and place an ME Bridge")
  560.         print("3. Connect ME Bridge to your ME system")
  561.         print()
  562.     else
  563.         print("Connection Type: Advanced Peripherals ME Bridge")
  564.         print("✓ Full AE2 integration with auto-crafting")
  565.         print()
  566.     end
  567.    
  568.     -- Show available items
  569.     local quantities = getCurrentQuantities()
  570.     local itemCount = 0
  571.     for _ in pairs(quantities) do itemCount = itemCount + 1 end
  572.    
  573.     print("Available Items: " .. itemCount)
  574.     if itemCount > 0 then
  575.         print("Sample items:")
  576.         local count = 0
  577.         for name, amount in pairs(quantities) do
  578.             if count < 5 then
  579.                 local displayName = name:gsub("minecraft:", "")
  580.                 printf("  %-20s: %d", displayName, amount)
  581.                 count = count + 1
  582.             end
  583.         end
  584.         if itemCount > 5 then
  585.             print("  ... and " .. (itemCount - 5) .. " more items")
  586.         end
  587.     end
  588.    
  589.     print()
  590.    
  591.     -- Show craftable items only if we have proper AE2 integration
  592.     if not ae2Interface._isBasicInventory then
  593.         local craftable = getCraftableItems()
  594.         local craftableCount = 0
  595.         for _ in pairs(craftable) do craftableCount = craftableCount + 1 end
  596.        
  597.         print("Craftable Items: " .. craftableCount)
  598.         if craftableCount > 0 then
  599.             print("Sample craftable items:")
  600.             local count = 0
  601.             for name, item in pairs(craftable) do
  602.                 if count < 3 then
  603.                     local displayName = name:gsub("minecraft:", "")
  604.                     print("  " .. displayName)
  605.                     count = count + 1
  606.                 end
  607.             end
  608.             if craftableCount > 3 then
  609.                 print("  ... and " .. (craftableCount - 3) .. " more")
  610.             end
  611.         end
  612.     else
  613.         print("Craftable Items: Not available (basic inventory mode)")
  614.     end
  615.    
  616.     print()
  617.     print("Press any key to continue...")
  618.     os.pullEvent("key")
  619. end
  620.  
  621. -- Add or edit item configuration
  622. local function addEditItem()
  623.     showHeader()
  624.     print("Add/Edit Item Configuration")
  625.     print()
  626.     print("Examples: iron_ingot, gold_ingot, diamond, redstone")
  627.     print("Tip: You can omit 'minecraft:' prefix")
  628.     print()
  629.    
  630.     write("Item name: ")
  631.     local itemName = read()
  632.    
  633.     if itemName == "" then
  634.         print("Invalid item name")
  635.         os.sleep(2)
  636.         return
  637.     end
  638.    
  639.     -- Auto-add minecraft: prefix if not present
  640.     if not itemName:find(":") then
  641.         itemName = "minecraft:" .. itemName
  642.     end
  643.    
  644.     -- Check if item exists in AE2 system
  645.     local item = findItem(itemName, false)
  646.     if not item and not TEST_MODE then
  647.         print("WARNING: Item not found in AE2 system")
  648.         write("Continue anyway? (y/n): ")
  649.         local confirm = read()
  650.         if confirm:lower() ~= "y" then
  651.             return
  652.         end
  653.     end
  654.    
  655.     -- Check if item is craftable
  656.     local craftableItem = findItem(itemName, true)
  657.     if not craftableItem and not TEST_MODE then
  658.         print("WARNING: Item is not craftable in AE2 system")
  659.         write("Continue anyway? (y/n): ")
  660.         local confirm = read()
  661.         if confirm:lower() ~= "y" then
  662.             return
  663.         end
  664.     end
  665.    
  666.     write("Target quantity: ")
  667.     local target = tonumber(read())
  668.     if not target or target <= 0 then
  669.         print("Invalid target quantity")
  670.         os.sleep(2)
  671.         return
  672.     end
  673.    
  674.     write("Threshold (craft when below): ")
  675.     local threshold = tonumber(read())
  676.     if not threshold or threshold <= 0 or threshold >= target then
  677.         print("Invalid threshold (must be positive and less than target)")
  678.         os.sleep(2)
  679.         return
  680.     end
  681.    
  682.     -- Find existing config or create new one
  683.     local found = false
  684.     for i, config in pairs(settings) do
  685.         if config.item == itemName then
  686.             config.target = target
  687.             config.threshold = threshold
  688.             found = true
  689.             break
  690.         end
  691.     end
  692.    
  693.     if not found then
  694.         table.insert(settings, {
  695.             item = itemName,
  696.             target = target,
  697.             threshold = threshold
  698.         })
  699.     end
  700.    
  701.     saveSettings()
  702.     print("Configuration saved!")
  703.     updateMonitor()
  704.     os.sleep(2)
  705. end
  706.  
  707. -- Remove item configuration
  708. local function removeItem()
  709.     showHeader()
  710.     print("Remove Item Configuration")
  711.     print()
  712.    
  713.     if #settings == 0 then
  714.         print("No items configured")
  715.         os.sleep(2)
  716.         return
  717.     end
  718.    
  719.     print("Configured Items:")
  720.     for i, config in pairs(settings) do
  721.         print(i .. ". " .. config.item)
  722.     end
  723.    
  724.     print()
  725.     write("Enter number to remove (0 to cancel): ")
  726.     local choice = tonumber(read())
  727.    
  728.     if choice and choice > 0 and choice <= #settings then
  729.         local removed = table.remove(settings, choice)
  730.         print("Removed: " .. removed.item)
  731.         saveSettings()
  732.         os.sleep(2)
  733.     end
  734. end
  735.  
  736. -- Test crafting request
  737. local function testCrafting()
  738.     showHeader()
  739.     print("Test Crafting Request")
  740.     print()
  741.    
  742.     write("Enter item name to test: ")
  743.     local itemName = read()
  744.     write("Enter quantity: ")
  745.     local amount = tonumber(read())
  746.    
  747.     if itemName ~= "" and amount and amount > 0 then
  748.         print("Testing crafting request...")
  749.         requestCrafting(itemName, amount)
  750.     else
  751.         print("Invalid input")
  752.     end
  753.    
  754.     print("Press any key to continue...")
  755.     os.pullEvent("key")
  756. end
  757.  
  758. -- Monitoring loop
  759. local function startMonitoring()
  760.     showHeader()
  761.     print("Starting monitoring loop...")
  762.     print("Press 'q' to stop monitoring")
  763.     if monitor then
  764.         print("Status will be shown on connected monitor")
  765.     end
  766.     print()
  767.    
  768.     running = true
  769.     updateMonitor()
  770.    
  771.     -- Start monitoring in parallel
  772.     parallel.waitForAny(
  773.         function()
  774.             while running do
  775.                 print("Checking item levels... (" .. os.date() .. ")")
  776.                 monitorAndCraft()
  777.                
  778.                 -- Simulate test mode crafting completion
  779.                 if TEST_MODE then
  780.                     for i = #testCraftingQueue, 1, -1 do
  781.                         local craft = testCraftingQueue[i]
  782.                         if os.clock() - craft.time > 10 then -- Simulate 10 second crafting
  783.                             testItems[craft.item] = (testItems[craft.item] or 0) + craft.amount
  784.                             print("FAKE: Completed crafting " .. craft.amount .. "x " .. craft.item)
  785.                             table.remove(testCraftingQueue, i)
  786.                            
  787.                             -- Remove from currentCraftingJobs when completed
  788.                             for j = #currentCraftingJobs, 1, -1 do
  789.                                 local job = currentCraftingJobs[j]
  790.                                 if job.item == craft.item and job.amount == craft.amount then
  791.                                     table.remove(currentCraftingJobs, j)
  792.                                     break
  793.                                 end
  794.                             end
  795.                             updateMonitor()
  796.                         end
  797.                     end
  798.                 end
  799.                
  800.                 for i = 1, CHECK_INTERVAL do
  801.                     os.sleep(1)
  802.                     if not running then break end
  803.                    
  804.                     -- Update monitor every 10 seconds
  805.                     if i % 10 == 0 then
  806.                         updateMonitor()
  807.                     end
  808.                 end
  809.             end
  810.         end,
  811.         function()
  812.             while running do
  813.                 local event, key = os.pullEvent("key")
  814.                 if key == keys.q then
  815.                     running = false
  816.                 end
  817.             end
  818.         end
  819.     )
  820.    
  821.     print()
  822.     print("Monitoring stopped")
  823.     updateMonitor()
  824.     os.sleep(2)
  825. end
  826.  
  827. -- Main program loop
  828. local function main()
  829.     -- Initialize
  830.     loadSettings()
  831.    
  832.     print("Attempting to connect to AE2...")
  833.     local ae2Connected = initAE2()
  834.    
  835.     print("Looking for monitor...")
  836.     initMonitor()
  837.    
  838.     if not ae2Connected then
  839.         print()
  840.         print("No proper AE2 connection found.")
  841.         print("You can still use the program in TEST MODE.")
  842.         print()
  843.         print("Press any key to continue...")
  844.         os.pullEvent("key")
  845.     end
  846.    
  847.     -- Initial monitor update
  848.     updateMonitor()
  849.    
  850.     -- Main menu loop
  851.     while true do
  852.         showMainMenu()
  853.         local choice = read()
  854.        
  855.         if choice == "1" then
  856.             viewSettings()
  857.         elseif choice == "2" then
  858.             addEditItem()
  859.         elseif choice == "3" then
  860.             removeItem()
  861.         elseif choice == "4" then
  862.             startMonitoring()
  863.         elseif choice == "5" then
  864.             testCrafting()
  865.         elseif choice == "6" then
  866.             viewSystemStatus()
  867.         elseif choice == "7" then
  868.             TEST_MODE = not TEST_MODE
  869.             print("Test mode " .. (TEST_MODE and "enabled" or "disabled"))
  870.             initAE2() -- Reinitialize with new mode
  871.             updateMonitor()
  872.             os.sleep(1)
  873.         elseif choice == "8" then
  874.             break
  875.         end
  876.     end
  877.    
  878.     showHeader()
  879.     print("Thank you for using AE2 Stockpile Manager!")
  880.     if monitor then
  881.         monitor.clear()
  882.         monitor.setCursorPos(1, 1)
  883.         monitor.setTextColor(colors.white)
  884.         monitor.write("AE2 Stockpile Manager")
  885.         monitor.setCursorPos(1, 2)
  886.         monitor.write("Program Stopped")
  887.     end
  888. end()
  889.         elseif choice == "7" then
  890.             TEST_MODE = not TEST_MODE
  891.             print("Test mode " .. (TEST_MODE and "enabled" or "disabled"))
  892.             initAE2() -- Reinitialize with new mode
  893.             updateMonitor()
  894.             os.sleep(1)
  895.         elseif choice == "8" then
  896.             break
  897.         end
  898.     end
  899.    
  900.     showHeader()
  901.     print("Thank you for using AE2 Stockpile Manager!")
  902.     if monitor then
  903.         monitor.clear()
  904.         monitor.setCursorPos(1, 1)
  905.         monitor.setTextColor(colors.white)
  906.         monitor.write("AE2 Stockpile Manager")
  907.         monitor.setCursorPos(1, 2)
  908.         monitor.write("Program Stopped")
  909.     end
  910. end()
  911.         elseif choice == "7" then
  912.             TEST_MODE = not TEST_MODE
  913.             print("Test mode " .. (TEST_MODE and "enabled" or "disabled"))
  914.             initAE2() -- Reinitialize with new mode
  915.             os.sleep(1)
  916.         elseif choice == "8" then
  917.             break
  918.         end
  919.     end
  920.    
  921.     showHeader()
  922.     print("Thank you for using AE2 Stockpile Manager!")
  923. end
  924.  
  925. -- Start the program
  926. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement