robocyclone

_Turtle Crafting Host 2.2.lua

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