robocyclone

_Turtle Crafting Host 2.4.lua

Mar 12th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 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.         local info = turtle.getItemDetail(i)
  49.         if info and info.name == itemName then
  50.             print("FIND_ITEM: " .. info.count .. " of " .. itemName .. " found in slot " .. i .. "! ")
  51.             return i, info.count
  52.         end
  53.     end
  54.     print("FIND_ITEM: " .. itemName .. " not found. ")
  55.     return false
  56. end
  57.  
  58. function CheckCraft(recipeOutput, recipeTable) --will return item to craft, iterations, and recipe output, if recipe can be crafted
  59.     print("CHECK_CRAFT: Searching for ingredients for " .. recipeOutput". ")
  60.     local lowestCount = 64
  61.     for k, v in pairs(recipeTable) do
  62.         local slot, invCount = FindItemInInv(k)
  63.         if slot ~= false then --check how many we can potentially craft
  64.             local low = math.floor( invCount / v[1] )
  65.             if lowestCount > low then
  66.                 lowestCount = low
  67.             end
  68.         else
  69.             return false
  70.         end
  71.     end
  72.     return recipeTable, lowestCount, recipeOutput
  73. end
  74.  
  75. function CheckAvailCrafts() --run CheckCraft on all available recipes
  76.     print("CHECK_AVAILABILITY: Checking inventory...")
  77.     for k, v in pairs(Recipes) do --key; recipe output : value; recipe ingredient table
  78.         local recipeTable, lowestCount, recipeOutput = CheckCraft(k, v)
  79.         if recipeTable and lowestCount and recipeOutput then
  80.             return recipeTable, lowestCount, recipeOutput
  81.         end
  82.     end
  83.     return false
  84. end
  85.  
  86. function CraftItem(recipeTable, amount)
  87.     repeat
  88.         print("CRAFT_ITEM: Checking slots containing items required in recipe for " .. recipeTable .. ". ")
  89.         for k, v in pairs(recipeTable) do
  90.             local slot = FindItemInInv(k)
  91.             turtle.select(slot)
  92.             for i = 1, v do
  93.                 turtle.drop()
  94.             end
  95.         end
  96.         print("CRAFT_ITEM: Recipe inserted.")
  97.         amount = amount - 1
  98.         local eInv = true --Handles crafts that take longer than process run time to complete
  99.         repeat
  100.             eInv = Energizer.list()
  101.             os.sleep(0.5)
  102.             print("CRAFT_ITEM: Waiting for craft to complete...")
  103.         until next(eInv) == nil
  104.         print("CRAFT_ITEM: Craft complete!")
  105.     until amount == 0
  106.     return true
  107. end
  108.  
  109. function Main()
  110.     print("MAIN: Checking inventory...")
  111.     local craftAvailable, count, r = CheckAvailCrafts()
  112.     if craftAvailable then
  113.         if count > 64 then
  114.             count = 64
  115.         end
  116.         print("MAIN: Craft found. Initiating craft for " .. r .. ". ")
  117.         CraftItem(craftAvailable, count)
  118.     else
  119.         print("MAIN: No craft available. Waiting to craft...")
  120.         os.pullEvent("turtle_inventory")
  121.         print("MAIN: Inventory changed. Initiating...")
  122.         Main()
  123.     end
  124.     Main()
  125. end
  126.  
  127. Main()
Add Comment
Please, Sign In to add comment