alesandreo

Energizing Orb

Feb 4th, 2021 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.02 KB | Gaming | 0 0
  1. --
  2. -- Created by IntelliJ IDEA.
  3. -- User: amcconaughey
  4. -- Date: 2/3/21
  5. -- Time: 2:09 PM
  6. -- To change this template use File | Settings | File Templates.
  7. --
  8.  
  9. --------------------------------------------
  10. -- Ensure peripherals are properly wrapped
  11. local peripherals = {
  12.     crafting_station = "back",
  13.     input_inventory = "left",
  14.     output_inventory = "right"
  15. }
  16.  
  17. function wrap_peripheral(name, description)
  18.     local peripheral = peripheral.wrap(name)
  19.     if not peripheral then
  20.         error(description .. " peripheral not found at '" .. name .. "'")
  21.     end
  22.     return peripheral
  23. end
  24.  
  25. crafting_station = wrap_peripheral(peripherals.crafting_station, "Crafting station")
  26. input_inventory = wrap_peripheral(peripherals.input_inventory, "Input inventory")
  27. output_inventory = wrap_peripheral(peripherals.output_inventory, "Output inventory")
  28.  
  29. recipes = {}
  30.  
  31. recipes["powah:steel_energized"] = {
  32.     inputs = {
  33.         {
  34.             count = 1,
  35.             name = "minecraft:iron_ingot"
  36.         },
  37.         {
  38.             count = 1,
  39.             name = "minecraft:gold_ingot"
  40.         }
  41.     },
  42.     outputs = {
  43.         {
  44.             count = 2,
  45.             name = "powah:steel_energized"
  46.         }
  47.     }
  48. }
  49. recipes["powah:crystal_blazing"] = {
  50.     inputs = {
  51.         {
  52.             count = 1,
  53.             name = "minecraft:blaze_rod"
  54.         }
  55.     },
  56.     outputs = {
  57.         {
  58.             count = 1,
  59.             name = "powah:crystal_blazing"
  60.         }
  61.     }
  62. }
  63.  
  64. recipes['ae2:charged_certus_quartz_crystal'] = {
  65.     inputs = {
  66.         {
  67.             min_count = 1,
  68.             max_count = 6,
  69.             name = "ae2:certus_quartz_crystal"
  70.         },
  71.     },
  72.     outputs = {
  73.         {
  74.             min_count = 1,
  75.             max_count = 6,
  76.             name = "ae2:charged_certus_quartz_crystal"
  77.         }
  78.     }
  79. }
  80.  
  81.  
  82. recipes['powah:crystal_niotic'] = {
  83.     inputs = {
  84.         {
  85.             count = 1,
  86.             name = "minecraft:ciamond"
  87.         },
  88.     },
  89.     outputs = {
  90.         {
  91.             count = 1,
  92.             name = 'powah:crystal_niotic'
  93.         }
  94.     }
  95. }
  96.  
  97. recipes['powah:niotic_crystal_block'] = {
  98.     inputs = {
  99.         {
  100.             count = 1,
  101.             name = "minecraft:diamond_block"
  102.         },
  103.     },
  104.     outputs = {
  105.         {
  106.             count = 1,
  107.             name = 'powah:niotic_crystal_block'
  108.         }
  109.     }
  110. }
  111.  
  112. recipes['powah:crystal_spirited'] = {
  113.     inputs = {
  114.         {
  115.             count = 1,
  116.             name = "minecraft:emerald"
  117.         },
  118.     },
  119.     outputs = {
  120.         {
  121.             count = 1,
  122.             name = 'powah:crystal_spirited'
  123.         }
  124.     }
  125. }
  126.  
  127. recipes['powah:spirited_crystal_block'] = {
  128.     inputs = {
  129.         {
  130.             count = 1,
  131.             name = "minecraft:emerald_block"
  132.         },
  133.     },
  134.     outputs = {
  135.         {
  136.             count = 1,
  137.             name = 'powah:spirited_crystal_block'
  138.         }
  139.     }
  140. }
  141.  
  142. recipes['powah:crystal_nitro'] = {
  143.     inputs = {
  144.         {
  145.             count = 1,
  146.             name = "minecraft:nether_star"
  147.         },
  148.         {
  149.             count = 2,
  150.             name = "minecraft:redstone_block"
  151.         },
  152.         {
  153.             count = 1,
  154.             name = "powah:blazing_crystal_block"
  155.         },
  156.     },
  157.     outputs = {
  158.         {
  159.             count = 16,
  160.             name = 'powah:crystal_nitro'
  161.         }
  162.     },
  163.     priority = 1 -- Higher priority recipes are crafted first
  164. }
  165.  
  166. recipes['powah:ender_core'] = {
  167.     inputs = {
  168.         {
  169.             count = 1,
  170.             name = "minecraft:ender_eye"
  171.         },
  172.         {
  173.             count = 1,
  174.             name = "powah:capacitor_basic_tiny"
  175.         },
  176.         {
  177.             count = 1,
  178.             name = "powah:dielectric_casing"
  179.         },
  180.     },
  181.     outputs = {
  182.         {
  183.             count = 1,
  184.             name = 'powah:ender_core'
  185.         }
  186.     },
  187.     priority = 1 -- Higher priority recipes are crafted first
  188. }
  189.  
  190.  
  191. recipes['appflux:charged_redstone_block'] = {
  192.     inputs = {
  193.         {
  194.             count = 1,
  195.             name = "minecraft:redstone_block"
  196.         },
  197.     },
  198.     outputs = {
  199.         {
  200.             count = 1,
  201.             name = 'appflux:charged_redstone_block'
  202.         }
  203.     },
  204.     exclude = { "minecraft:nether_star" } -- Skip this recipe if 'minecraft:nether_star' is present
  205. }
  206.  
  207. recipes['powah:uraninite'] = {
  208.     inputs = {
  209.         {
  210.             count = 1,
  211.             name = "alltheores:uranium_ingot"
  212.         },
  213.     },
  214.     outputs = {
  215.         {
  216.             count = 1,
  217.             name = 'powah:uraninite'
  218.         }
  219.     }
  220. }
  221.  
  222. recipes['Energized Steel Bee Spawn Egg'] = {
  223.     inputs = {
  224.         {
  225.             count = 2,
  226.             displayName = 'Iron Bee Spawn Egg'
  227.         },
  228.         {
  229.             count = 1,
  230.             displayName = 'Gold Bee Spawn Egg'
  231.         },
  232.         {
  233.             count = 2,
  234.             name = 'powah:energized_steel_block'
  235.         }
  236.     },
  237.     outputs = {
  238.         {
  239.             count = 1,
  240.             displayName = 'Energized Steel Bee Spawn Egg'
  241.         }
  242.     }
  243. }
  244.  
  245. function search_slots_for_item(inventory, item)
  246.     local slots = {}
  247.     local slot_count = inventory.size()
  248.     for i = 1, slot_count do
  249.         local slot_item = inventory.getItemDetail(i)
  250.         if slot_item then
  251.             -- Check by name and displayName if applicable
  252.             if slot_item.name == item.name or
  253.                (item.displayName and slot_item.displayName == item.displayName) then
  254.                 table.insert(slots, i)
  255.             elseif item.name and string.match(item.name, "^productivebees:") then
  256.                 -- Special case for productivebees: check displayName
  257.                 if slot_item.displayName == item.displayName then
  258.                     table.insert(slots, i)
  259.                 end
  260.             end
  261.         end
  262.     end
  263.     return slots
  264. end
  265.  
  266. function get_item_details_by_name(inventory, item_name)
  267.     local slots = search_slots_for_item(inventory, item_name)
  268.     if #slots == 0 then
  269.         print("Item "..item_name.." not found in "..peripheral.getName(inventory))
  270.         return nil
  271.     end
  272.     local item_slot = slots[1]
  273.     local item = inventory.getItemDetail(item_slot)
  274.     if item then
  275.         item.slot = item_slot -- Add the slot number to the item details
  276.         return item
  277.     else
  278.         return nil
  279.     end
  280. end
  281.  
  282. function transfer_item(from_inv, to_inv, item_name, item_count)
  283.     if not from_inv or not to_inv then
  284.         error("Invalid inventory provided for transfer")
  285.     end
  286.     local item = get_item_details_by_name(from_inv, item_name)
  287.     if item then
  288.         local transfer_count = math.min(item.count, item_count)
  289.         print("Attempting to transfer "..transfer_count.." of "..item_name.." from slot "..item.slot.." in "..peripheral.getName(from_inv).." to "..peripheral.getName(to_inv))
  290.         if not check_empty_slots(to_inv) then
  291.             print("No empty slots available in "..peripheral.getName(to_inv))
  292.             return
  293.         end
  294.         local transferred = from_inv.pushItems(peripheral.getName(to_inv), item.slot, transfer_count)
  295.         if transferred > 0 then
  296.             print("Transferred "..transferred.." of "..item_name.." from "..peripheral.getName(from_inv).." to "..peripheral.getName(to_inv))
  297.         else
  298.             print("Failed to transfer "..item_name.." from "..peripheral.getName(from_inv).." to "..peripheral.getName(to_inv))
  299.         end
  300.     else
  301.         print("Item "..item_name.." not found in "..peripheral.getName(from_inv))
  302.     end
  303. end
  304.  
  305. function get_item_count(inventory, item_name)
  306.     local item = get_item_details_by_name(inventory, item_name)
  307.     return item and item.count or 0
  308. end
  309.  
  310. function transfer_items(from_inv, to_inv, items)
  311.     for _, item in ipairs(items) do
  312.         -- Determine the transfer count
  313.         local transfer_count = item.count or math.min(
  314.             get_item_count(from_inv, item.name or item.displayName),
  315.             item.max_count or item.min_count
  316.         )
  317.         if transfer_count > 0 then
  318.             local slots = search_slots_for_item(from_inv, item)
  319.             for _, slot in ipairs(slots) do
  320.                 local transferred = from_inv.pushItems(peripheral.getName(to_inv), slot, transfer_count)
  321.                 transfer_count = transfer_count - transferred
  322.                 if transfer_count <= 0 then
  323.                     break
  324.                 end
  325.             end
  326.         end
  327.     end
  328. end
  329.  
  330. function check_inventory(inventory, items)
  331.     for _, item in ipairs(items) do
  332.         local slots = search_slots_for_item(inventory, item)
  333.         local total_count = 0
  334.         for _, slot in ipairs(slots) do
  335.             local slot_item = inventory.getItemDetail(slot)
  336.             if slot_item then
  337.                 total_count = total_count + slot_item.count
  338.             end
  339.         end
  340.  
  341.         -- Determine the required count
  342.         local required_count = item.count or item.min_count
  343.         if total_count < required_count then
  344.             return false
  345.         end
  346.     end
  347.     return true
  348. end
  349.  
  350. function check_stock(inventory, recipe)
  351.     return check_inventory(inventory, recipe.inputs)
  352. end
  353.  
  354. function check_empty(inventory)
  355.     local sum_of_items = 0
  356.     local slot_count = inventory.size()
  357.     for i=1,slot_count do
  358.         local item = inventory.getItemDetail(i)
  359.         if item then
  360.             sum_of_items = sum_of_items + item.count
  361.         end
  362.     end
  363.     if sum_of_items > 0 then
  364.         return false
  365.     end
  366.     return true
  367. end
  368.  
  369. function check_output(inventory, recipe)
  370.     return check_inventory(inventory, recipe.outputs)
  371. end
  372.  
  373. function insert_ingredients(in_inventory, cs_inv, recipe)
  374.     print("Inserting Ingredients from " .. peripheral.getName(in_inventory) .. " to " .. peripheral.getName(cs_inv))
  375.     if recipe and check_stock(in_inventory, recipe) then
  376.         transfer_items(in_inventory, cs_inv, recipe.inputs)
  377.     end
  378. end
  379.  
  380. function transfer_output(cs_inv, out_inv, recipe)
  381.     print("Transferring outputs from " .. peripheral.getName(cs_inv) .. " to " .. peripheral.getName(out_inv))
  382.     if recipe and check_output(cs_inv, recipe) then
  383.         transfer_items(cs_inv, out_inv, recipe.outputs)
  384.     end
  385. end
  386.  
  387. function craft_recipe(recipe)
  388.     if not recipe then
  389.         error("Recipe not found")
  390.     end
  391.  
  392.     -- Use displayName if available, otherwise fallback to name
  393.     local output_name = recipe.outputs[1].displayName or recipe.outputs[1].name
  394.     print("Starting crafting for recipe: " .. output_name)
  395.  
  396.     while check_stock(input_inventory, recipe) or not check_empty(crafting_station) do
  397.         if check_empty(crafting_station) and check_stock(input_inventory, recipe) then
  398.             print("Inserting ingredients...")
  399.             insert_ingredients(input_inventory, crafting_station, recipe)
  400.         elseif check_output(crafting_station, recipe) then
  401.             print("Transferring outputs...")
  402.             transfer_output(crafting_station, output_inventory, recipe)
  403.         end
  404.         os.sleep(10)
  405.     end
  406. end
  407.  
  408. function check_empty_slots(inventory)
  409.     local slot_count = inventory.size()
  410.     for i = 1, slot_count do
  411.         if not inventory.getItemDetail(i) then
  412.             return true -- Found an empty slot
  413.         end
  414.     end
  415.     return false -- No empty slots
  416. end
  417.  
  418. -- Function to clean up the crafting station
  419. function clean_crafting_station(cs_inv, out_inv)
  420.     print("Checking crafting station for erroneous or leftover outputs...")
  421.     local slot_count = cs_inv.size()
  422.     for i = 1, slot_count do
  423.         local item = cs_inv.getItemDetail(i)
  424.         if item then
  425.             local is_known_output = false
  426.             local is_in_process_craft = false
  427.  
  428.             -- Check if the item matches any known recipe outputs
  429.             for _, recipe in pairs(recipes) do
  430.                 for _, output in ipairs(recipe.outputs) do
  431.                     if item.name == output.name then
  432.                         is_known_output = true
  433.                         print("Found leftover output: " .. item.name .. ". Transferring to output inventory.")
  434.                         transfer_item(cs_inv, out_inv, item.name, item.count)
  435.                         break
  436.                     end
  437.                 end
  438.                 if is_known_output then break end
  439.             end
  440.  
  441.             -- Check if the item matches any known recipe inputs (in-process craft)
  442.             if not is_known_output then
  443.                 for _, recipe in pairs(recipes) do
  444.                     for _, input in ipairs(recipe.inputs) do
  445.                         if item.name == input.name then
  446.                             is_in_process_craft = true
  447.                             print("Found in-process craft item: " .. item.name .. ". Leaving it in place.")
  448.                             break
  449.                         end
  450.                     end
  451.                     if is_in_process_craft then break end
  452.                 end
  453.             end
  454.  
  455.             -- Handle unknown items
  456.             if not is_known_output and not is_in_process_craft then
  457.                 print("Warning: Found unknown item '" .. item.name .. "' in crafting station. Transferring to output inventory.")
  458.                 transfer_item(cs_inv, out_inv, item.name, item.count)
  459.             end
  460.         end
  461.     end
  462. end
  463.  
  464. function has_excluded_items(inventory, exclude_list)
  465.     if not exclude_list then return false end
  466.     for _, excluded_item in ipairs(exclude_list) do
  467.         if get_item_count(inventory, excluded_item) > 0 then
  468.             return true
  469.         end
  470.     end
  471.     return false
  472. end
  473.  
  474. function sort_recipes_by_priority(recipes)
  475.     local sorted_recipes = {}
  476.     for name, recipe in pairs(recipes) do
  477.         table.insert(sorted_recipes, { name = name, recipe = recipe })
  478.     end
  479.     table.sort(sorted_recipes, function(a, b)
  480.         return (a.recipe.priority or 0) > (b.recipe.priority or 0)
  481.     end)
  482.     return sorted_recipes
  483. end
  484.  
  485. -- Updated loop to handle restarts
  486. print("Checking for possible recipes to craft...")
  487. while true do
  488.     -- Clean up the crafting station before starting
  489.     clean_crafting_station(crafting_station, output_inventory)
  490.  
  491.     local crafted_anything = false
  492.     local sorted_recipes = sort_recipes_by_priority(recipes)
  493.  
  494.     for _, entry in ipairs(sorted_recipes) do
  495.         local recipe_name = entry.name
  496.         local recipe = entry.recipe
  497.  
  498.         -- Skip the recipe if excluded items are present
  499.         if has_excluded_items(input_inventory, recipe.exclude) then
  500.             print("Skipping recipe '" .. recipe_name .. "' due to excluded items.")
  501.             goto continue
  502.         end
  503.  
  504.         -- Check if materials are available and craft the recipe
  505.         if check_stock(input_inventory, recipe) then
  506.             print("Materials found for recipe: " .. recipe_name)
  507.             craft_recipe(recipe)
  508.             crafted_anything = true
  509.             break -- Craft one recipe at a time
  510.         end
  511.  
  512.         ::continue::
  513.     end
  514.  
  515.     if not crafted_anything then
  516.         print("No materials available for any recipe. Waiting...")
  517.     end
  518.     os.sleep(10) -- Wait before checking again
  519. end
  520.  
Add Comment
Please, Sign In to add comment