robocyclone

_Turtle Crafting Host 3.lua

Mar 12th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. term.clear()
  2. Energizer = peripheral.find("powah:energizing_orb")
  3. if not Energizer then
  4.     term.write("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:unobtainium_vibranium_alloy_ingot"] = {
  30.     ["allthemodium:piglich_heart"] = 1,
  31.     ["allthemodium:unobtainium_ingot"] = 1,
  32.     ["allthemodium:vibranium_ingot"] = 1
  33. }
  34. Recipes["allthemodium:unobtainium_allthemodium_alloy_ingot"] = {
  35.     ["allthemodium:piglich_heart"] = 1,
  36.     ["allthemodium:unobtainium_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.     term.write("\nFIND_ITEM: Finding " .. itemName .. ".")
  47.     for i = 1, 16 do
  48.         os.sleep(0.05)
  49.         term.write(".")
  50.         local info = turtle.getItemDetail(i)
  51.         if info and info.name == itemName then
  52.             term.write("\nFIND_ITEM: " .. info.count .. " of " .. itemName .. " found in slot " .. i .. "! ")
  53.             return i, info.count
  54.         end
  55.     end
  56.     term.write("\nFIND_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.     term.write("\nCHECK_CRAFT: Searching for ingredients for " .. recipeOutput .. ".")
  62.     local lowestCount = 64
  63.     for k, v in pairs(recipeTable) do
  64.         os.sleep(0.05)
  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.             term.write("\nCHECK_CRAFT: Missing " .. k .. " for recipe " .. recipeOutput .. ". ")
  73.             return false
  74.         end
  75.     end
  76.     term.write("\nCHECK_CRAFT: Can craft " .. recipeOutput .. " " .. lowestCount .. " times.")
  77.     return recipeTable, lowestCount, recipeOutput
  78. end
  79.  
  80. function CheckAvailCrafts() --run CheckCraft on all available recipes
  81.     term.write("\nCHECK_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.             term.write("\nCHECK_ALL: Crafting first recipe match found for " .. recipeOutput ..", " .. lowestCount .. " times. ")
  86.             return recipeTable, lowestCount, recipeOutput
  87.         end
  88.     end
  89.     term.write("\nCHECK_ALL: No recipes found.")
  90.     return false
  91. end
  92.  
  93. function CraftItem(recipeTable, amount, recipeOutput)
  94.     repeat
  95.         term.write("\nCRAFT_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.         term.write("\nCRAFT_ITEM: Recipe inserted.")
  104.         amount = amount - 1
  105.         term.write(" There are " .. amount .. " iterations of this recipe left.")
  106.         local eInv = true --Handles crafts that take longer than process run time to complete
  107.         term.write("\nCRAFT_ITEM: Waiting for craft to complete.")
  108.         repeat
  109.             eInv = Energizer.list()
  110.             os.sleep(0.75)
  111.             term.write(".")
  112.         until next(eInv) == nil
  113.         term.write("\nCRAFT_ITEM: Craft complete!")
  114.     until amount == 0
  115.     return true
  116. end
  117.  
  118. function Main()
  119.     term.write("\nMAIN: Checking inventory...")
  120.     local craftAvailable, count, r = CheckAvailCrafts()
  121.     if craftAvailable then
  122.         if count > 64 then
  123.             count = 64
  124.         end
  125.         term.write("\nMAIN: Craft found. Initiating craft for " .. r .. ", " .. count .. " time(s).")
  126.         CraftItem(craftAvailable, count, r)
  127.     else
  128.         term.write("\nMAIN: No craft available. Waiting to craft...")
  129.         os.pullEvent("turtle_inventory")
  130.         term.write("\nMAIN: Inventory changed. Initiating...")
  131.         Main()
  132.     end
  133.     Main()
  134. end
  135.  
  136. Main()
Add Comment
Please, Sign In to add comment