Advertisement
Galakaston

Turtle Tunnel

Mar 9th, 2024 (edited)
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | Gaming | 0 0
  1. local function placeTorch()
  2.     for slot = 1, 16 do
  3.         local itemDetail = turtle.getItemDetail(slot)
  4.         if itemDetail and itemDetail.name == "minecraft:torch" then
  5.             turtle.select(slot)
  6.             if turtle.placeDown() then
  7.                 return true
  8.             end
  9.         end
  10.     end
  11.     return false
  12. end
  13.  
  14. local function checkAndDig()
  15.     while turtle.detect() do
  16.         turtle.dig()
  17.         sleep(0.5)
  18.     end
  19. end
  20.  
  21. local function mineTunnel(length)
  22.     -- Check if length is valid
  23.     if length < 1 then
  24.         print("Length must be greater than zero.")
  25.         return
  26.     end
  27.  
  28.     local torchCounter = 0
  29.  
  30.     -- Mine the tunnel
  31.     for i = 1, length do
  32.         checkAndDig()
  33.         turtle.forward()
  34.         turtle.digUp()
  35.         turtle.digDown()
  36.  
  37.         -- Increment torch counter
  38.         torchCounter = torchCounter + 1
  39.  
  40.         -- Place torch every 10 blocks
  41.         if torchCounter == 10 then
  42.             if not placeTorch() then
  43.                 print("No torches left to place!")
  44.             end
  45.             torchCounter = 0
  46.         end
  47.  
  48.         -- Mine the left side of the tunnel
  49.         turtle.turnLeft()
  50.         checkAndDig()
  51.         turtle.forward()
  52.         turtle.digUp()
  53.         turtle.digDown()
  54.         turtle.turnRight()
  55.  
  56.         -- Move to the right side of the tunnel
  57.         turtle.turnRight()
  58.         turtle.forward()
  59.         checkAndDig()
  60.         turtle.forward()
  61.         turtle.digUp()
  62.         turtle.digDown()
  63.         turtle.turnLeft()
  64.  
  65.         -- Return to original orientation
  66.         turtle.turnLeft()
  67.         turtle.forward()
  68.         turtle.turnRight()
  69.  
  70.         -- Move to the next row
  71.         if i < length then
  72.             turtle.forward()
  73.         end
  74.     end
  75. end
  76.  
  77. -- Main program
  78. local args = {...}
  79. local length = tonumber(args[1])
  80.  
  81. if not length then
  82.     print("Usage: tunnel <length>")
  83.     return
  84. end
  85.  
  86. mineTunnel(length)
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement