robocyclone

_Turtle Crafting Host 2.4.lua

Mar 12th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.67 KB | None | 0 0
  1. term.clear()
  2. Energizer = peripheral.find("powah:energizing_orb")
  3. if not Energizer then
  4.     print("Cannot find energizer.")
  5.     return false
  6. end
  7. Recipes = {}
  8. Recipes["powah:blazing_crystal_block"] = {
  9.     ["botania:blaze_block"] = 1
  10. }
  11. Recipes["powah:niotic_crystal_block"] = {
  12.     ["minecraft:diamond_block"] = 1
  13. }
  14. Recipes["powah:spirited_crystal_block"] = {
  15.     ["minecraft:emerald_block"] = 1
  16. }
  17. Recipes["powah:dry_ice"] = {
  18.     ["minecraft:blue_ice"] = 2
  19. }
  20. Recipes["powah:energized_steel_block"] = {
  21.     ["minecraft:gold_block"] = 1,
  22.     ["minecraft:iron_block"] = 1
  23. }
  24. Recipes["powah:nitro_crystal"] = {
  25.     ["minecraft:nether_star"] = 1,
  26.     ["minecraft:redstone_block"] = 2,
  27.     ["powah:blazing_crystal_block"] = 1
  28. }
  29. Recipes["allthemodium:unobtanium_vibranium_alloy_ingot"] = {
  30.     ["allthemodium:piglich_heart"] = 1,
  31.     ["allthemodium:unobtanium_ingot"] = 1,
  32.     ["allthemodium:vibranium_ingot"] = 1
  33. }
  34. Recipes["allthemodium:unobtanium_allthemodium_alloy_ingot"] = {
  35.     ["allthemodium:piglich_heart"] = 1,
  36.     ["allthemodium:unobtanium_ingot"] = 1,
  37.     ["allthemodium:allthemodium_ingot"] = 1
  38. }
  39. Recipes["allthemodium:vibranium_allthemodium_alloy_ingot"] = {
  40.     ["allthemodium:piglich_heart"] = 1,
  41.     ["allthemodium:vibranium_ingot"] = 1,
  42.     ["allthemodium:allthemodium_ingot"] = 1
  43. }
  44.  
  45. function FindItemInInv(itemName) --to fix double dropping items if requested craft requires >64 of one input ( and for readability :) )
  46.     print("FIND_ITEM: Finding " .. itemName .. "... ")
  47.     for i = 1, 16 do
  48.         os.sleep(1)
  49.         local info = turtle.getItemDetail(i)
  50.         if info and info.name == itemName then
  51.             print("FIND_ITEM: " .. info.count .. " of " .. itemName .. " found in slot " .. i .. "! ")
  52.             return i, info.count
  53.         end
  54.     end
  55.     os.sleep(1)
  56.     print("FIND_ITEM: " .. itemName .. " not found. ")
  57.     return false
  58. end
  59.  
  60. function CheckCraft(recipeOutput, recipeTable) --will return item to craft, iterations, and recipe output, if recipe can be crafted
  61.     print("CHECK_CRAFT: Searching for ingredients for " .. recipeOutput .. ". ")
  62.     local lowestCount = 64
  63.     for k, v in pairs(recipeTable) do
  64.         os.sleep(1)
  65.         local slot, invCount = FindItemInInv(k)
  66.         if slot ~= false then --check how many we can potentially craft
  67.             local low = math.floor( invCount / v )
  68.             if lowestCount > low then
  69.                 lowestCount = low
  70.             end
  71.         else
  72.             print("CHECK_CRAFT: Missing " .. k .. "for recipe " .. recipeOutput .. ". ")
  73.             return false
  74.         end
  75.     end
  76.     print("CHECK_CRAFT: Can craft " .. recipeOutput .. " " .. lowestCount .. " times.")
  77.     return recipeTable, lowestCount, recipeOutput
  78. end
  79.  
  80. function CheckAvailCrafts() --run CheckCraft on all available recipes
  81.     print("CHECK_ALL: Checking inventory...")
  82.     for k, v in pairs(Recipes) do --key; recipe output : value; recipe ingredient table
  83.         local recipeTable, lowestCount, recipeOutput = CheckCraft(k, v)
  84.         if recipeTable and lowestCount and recipeOutput then
  85.             print("CHECK_ALL: Crafting first recipe match found for " .. recipeOutput ..", " .. lowestCount .. " times. ")
  86.             return recipeTable, lowestCount, recipeOutput
  87.         end
  88.     end
  89.     print("CHECK_ALL: No recipes found.")
  90.     return false
  91. end
  92.  
  93. function CraftItem(recipeTable, amount, recipeOutput)
  94.     repeat
  95.         print("CRAFT_ITEM: Checking slots containing items required in recipe for " .. recipeOutput .. ". ")
  96.         for k, v in pairs(recipeTable) do
  97.             local slot = FindItemInInv(k)
  98.             turtle.select(slot)
  99.             for i = 1, v do
  100.                 turtle.drop()
  101.             end
  102.         end
  103.         print("CRAFT_ITEM: Recipe inserted.")
  104.         amount = amount - 1
  105.         local eInv = true --Handles crafts that take longer than process run time to complete
  106.         repeat
  107.             eInv = Energizer.list()
  108.             os.sleep(0.5)
  109.             print("CRAFT_ITEM: Waiting for craft to complete...")
  110.         until next(eInv) == nil
  111.         print("CRAFT_ITEM: Craft complete!")
  112.     until amount == 0
  113.     return true
  114. end
  115.  
  116. function Main()
  117.     print("MAIN: Checking inventory...")
  118.     local craftAvailable, count, r = CheckAvailCrafts()
  119.     if craftAvailable then
  120.         if count > 64 then
  121.             count = 64
  122.         end
  123.         print("MAIN: Craft found. Initiating craft for " .. r .. ". ")
  124.         CraftItem(craftAvailable, count, r)
  125.     else
  126.         print("MAIN: No craft available. Waiting to craft...")
  127.         os.pullEvent("turtle_inventory")
  128.         print("MAIN: Inventory changed. Initiating...")
  129.         Main()
  130.     end
  131.     Main()
  132. end
  133.  
  134. Main()
Add Comment
Please, Sign In to add comment