robocyclone

_Turtle Crafting Host 3.1.lua

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