Advertisement
HANDZ

Draconic crafting automation

Aug 22nd, 2021 (edited)
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.41 KB | None | 0 0
  1. --[[
  2. interface in the front in blocking mode
  3. inventory that goes to injectors on the bottom
  4. inventory that goes to crafting core on the top
  5.  
  6. program recipes and done
  7. (you dont have to use the dummy item)
  8. --]]
  9.  
  10. local dummy = "minecraft:cobblestone"
  11. local recipes = {
  12.     -- draconic
  13.     -- awakend draconium
  14.     {
  15.         ingredients = {
  16.             {"draconicevolution:draconic_core", 6}, {"draconicevolution:draconium_block", 4}, {"draconicevolution:dragon_heart", 1}
  17.         },
  18.         core = {"draconicevolution:draconium_block", 4},
  19.         result = {"draconicevolution:draconic_block", 4}
  20.     },
  21.     -- awakend core
  22.     {
  23.         ingredients = {
  24.             {"minecraft:nether_star", 1}, {"draconicevolution:wyvern_core", 4}, {"draconicevolution:draconic_ingot", 5}
  25.         },
  26.         core = {"minecraft:nether_star", 1},
  27.         result = {"draconicevolution:awakened_core", 1}
  28.     },
  29.     -- chaotic core
  30.     {
  31.         ingredients = {
  32.             {"draconicevolution:chaos_shard", 1}, {"draconicevolution:awakened_core", 4}, {"draconicevolution:draconic_ingot", 4}
  33.         },
  34.         core = {"draconicevolution:chaos_shard", 1},
  35.         result = {"draconicevolution:chaotic_core", 1}
  36.     },
  37.     -- projecte
  38.     -- collectors
  39.     {
  40.         ingredients = {
  41.             {"minecraft:glass", 1}, {"minecraft:diamond_block", 1}, {"minecraft:glowstone", 6}, {"minecraft:furnace", 1}
  42.         },
  43.         core = {"minecraft:furnace", 1},
  44.         result = {"projecte:collector_mk1", 1}
  45.     },
  46.     {
  47.         ingredients = {
  48.             {"projecte:item.pe_matter", 1}, {"minecraft:glowstone", 7}, {"projecte:collector_mk1", 1}
  49.         },
  50.         core = {"projecte:collector_mk1", 1},
  51.         result = {"projecte:collector_mk2", 1}
  52.     },
  53.     {
  54.         ingredients = {
  55.             {"projecte:item.pe_matter", 1}, {"minecraft:glowstone", 7}, {"projecte:collector_mk2", 1}
  56.         },
  57.         core = {"projecte:collector_mk2", 1},
  58.         result = {"projecte:collector_mk3", 1}
  59.     },
  60.     -- relayes
  61.     {
  62.         ingredients = {
  63.             {"minecraft:glass", 1}, {"minecraft:diamond_block", 1}, {"minecraft:obsidian", 6}, {"minecraft:furnace", 1}
  64.         },
  65.         core = {"minecraft:furnace", 1},
  66.         result = {"projecte:relay_mk1", 1}
  67.     },
  68.     {
  69.         ingredients = {
  70.             {"projecte:item.pe_matter", 1}, {"minecraft:obsidian", 7}, {"projecte:relay_mk1", 1}
  71.         },
  72.         core = {"projecte:relay_mk1", 1},
  73.         result = {"projecte:relay_mk2", 1}
  74.     },
  75.     {
  76.         ingredients = {
  77.             {"projecte:item.pe_matter", 1}, {"minecraft:obsidian", 7}, {"projecte:relay_mk2", 1}
  78.         },
  79.         core = {"projecte:relay_mk2", 1},
  80.         result = {"projecte:relay_mk3", 1}
  81.     },
  82.     -- energy condenser
  83.     {
  84.         ingredients = {
  85.             {"projecte:item.pe_matter", 4}, {"projecte:item.pe_matter", 4}, {"ironchest:iron_chest", 1}, {"ironchest:iron_chest", 1}, {"projecte:alchemical_chest", 1}
  86.         },
  87.         core = {"projecte:alchemical_chest", 1},
  88.         result = {"projecte:condenser_mk1", 1}
  89.     }
  90. }
  91.  
  92. -----------------------------------------
  93.  
  94. -- sort recipes
  95. for i = 1, #recipes do
  96.     table.sort(recipes[i]["ingredients"], function (a, b)
  97.         return a[1] < b[1]
  98.     end)
  99. end
  100.  
  101. -- check if inventory is the same as recipe ingredients
  102. local function is_equal (a, b)
  103.     if #a ~= #b then
  104.         return false
  105.     end
  106.  
  107.     for i = 1, #a do
  108.         if a[i][1] ~= b[i][1] or a[i][2] ~= b[i][2] then
  109.             return false
  110.         end
  111.     end
  112.  
  113.     return true
  114. end
  115.  
  116. -- run all the time
  117. while true do
  118.     local using_dummy = false
  119.  
  120.     -- get items in inventory without dummy
  121.     local inv = {}
  122.     for i=1,16 do
  123.         local slot = turtle.getItemDetail(i)
  124.         if not slot then
  125.             break
  126.         end
  127.         if slot.name ~= dummy then
  128.             table.insert(inv, {slot.name, slot.count})
  129.         else
  130.             using_dummy = true
  131.         end
  132.     end
  133.  
  134.     if #inv > 0 then
  135.         -- sort inventory for checking
  136.         table.sort(inv, function (a, b)
  137.             return a[1] < b[1]
  138.         end)
  139.  
  140.         local to_craft = {}
  141.         -- check for valid recipe
  142.         for i = 1, #recipes do
  143.             if is_equal(recipes[i]["ingredients"], inv) then
  144.                 to_craft = recipes[i]
  145.                 break
  146.             end
  147.         end
  148.  
  149.         if to_craft["ingredients"] then
  150.             print("Crafting: " .. to_craft["result"][2] .. " x " .. to_craft["result"][1])
  151.  
  152.             -- send items to inventories
  153.             print("Sending items to inventories")
  154.             for i=1,16 do
  155.                 local slot = turtle.getItemDetail(i)
  156.                 if not slot then
  157.                     break
  158.                 end
  159.                 if slot.name ~= dummy then
  160.                     turtle.select(i)
  161.                     if slot.name == to_craft["core"][1] then
  162.                         turtle.dropUp(to_craft["core"][2])
  163.                         turtle.dropDown()
  164.                     else
  165.                         turtle.dropDown()
  166.                     end
  167.                 end
  168.             end
  169.  
  170.             -- wait for output
  171.             print("Waiting for output")
  172.             local done_crafting = false
  173.             while not done_crafting do
  174.                 for i=1,16 do
  175.                     local slot = turtle.getItemDetail(i)
  176.                     if not slot then
  177.                         break
  178.                     end
  179.                     if slot.name == to_craft["result"][1] and slot.count >= to_craft["result"][2] then
  180.                         done_crafting = true
  181.                         print("Sending output to inventory")
  182.                         turtle.select(i)
  183.                         turtle.drop(to_craft["result"][2])
  184.                         break
  185.                     end
  186.                 end
  187.                 sleep(0.1)
  188.             end
  189.  
  190.             -- remove dummy item (in blocking mode there should be only dummy item left)
  191.             if using_dummy then
  192.                 print("Removing dummy item")
  193.                 for i=1,16 do
  194.                     local slot = turtle.getItemDetail(i)
  195.                     if slot then
  196.                         turtle.select(i)
  197.                         turtle.drop()
  198.                     end
  199.                 end
  200.             end
  201.  
  202.             print("Finished crafting\n\n")
  203.         else
  204.             print("Recipe not found")
  205.         end
  206.     end
  207.     sleep(0.1)
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement