Advertisement
machasins

autoCraft TNT

Aug 6th, 2022 (edited)
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. function suck(count, iter)
  2.     local result = false
  3.     local err = ""
  4.     if iter % 2 == 0 then
  5.         -- Suck gunpowder
  6.         result, err = turtle.suckUp(count)
  7.     else
  8.         -- Suck sand
  9.         result, err = turtle.suckDown(count)
  10.     end
  11.     if turtle.getItemCount(turtle.getSelectedSlot() + (iter % 3)) ~= count then
  12.         result = false
  13.         err = "Not enough resources."
  14.     end
  15.     if (iter + 1) % 3 == 0 then
  16.         turtle.select(turtle.getSelectedSlot() + 4)
  17.     end
  18.     if not result then error(err) end
  19. end
  20.  
  21. function clearInventory()
  22.     for i = 1, 16, 1
  23.     do
  24.         turtle.select(i)
  25.         turtle.drop(turtle.getItemCount(turtle.getSelectedSlot()))
  26.     end
  27.     turtle.select(1)
  28.     return true
  29. end
  30.  
  31. function toboolean(str)
  32.     return str == "true"
  33. end
  34.  
  35. function errorPrinter(err)
  36.     print("Error:", err)
  37. end
  38.  
  39. function waitForInput()
  40.     print("Press enter to continue...")
  41.     io.read()
  42. end
  43.  
  44. function setup()
  45.     print("Clearing inventory...")
  46.     clearInventory()
  47.  
  48.     if redstone.getInput("back") then
  49.         print("Starting crafting...")
  50.     else
  51.         print("Waiting for redstone input...")
  52.     end
  53. end
  54.  
  55. function mainLoop(itemType, craftAmount, waitForAmount, doCrafting)
  56.     for i = 0, 8, 1
  57.     do
  58.         callSuccess, err = pcall(suck, craftAmount, i)
  59.         if not callSuccess then
  60.             -- Failure
  61.             errorPrinter(err)
  62.             if waitForAmount then
  63.                 os.sleep(60)
  64.             else
  65.                 waitForInput()
  66.             end
  67.             error()
  68.         end
  69.     end
  70.  
  71.     turtle.select(1)
  72.     success = doCrafting and turtle.craft()
  73.     success = not doCrafting and clearInventory()
  74.     success = doCrafting and print("Crafted " .. craftAmount .. " " .. itemType)
  75.     turtle.drop(craftAmount)
  76. end
  77.  
  78. local args = {...}
  79. local itemType = args[1]
  80. local craftAmount = tonumber(args[2])
  81. local waitForAmount = toboolean(args[3]) or true
  82. local doCrafting = toboolean(args[4]) or true
  83.  
  84. local isCrafting = false
  85.  
  86. setup()
  87.  
  88. while true
  89. do
  90.     if redstone.getInput("back") then
  91.         if not isCrafting then
  92.             print("Starting crafting...")
  93.             isCrafting = true
  94.         end
  95.         if not pcall(mainLoop, itemType, craftAmount, waitForAmount, doCrafting) then
  96.             setup()
  97.         end
  98.     else
  99.         isCrafting = false
  100.         os.sleep(0.1)
  101.     end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement