Advertisement
NeonSan

Turtle flat builder v1

May 6th, 2024 (edited)
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | None | 0 0
  1. local lastSlot = 1
  2. local fuelSlot = 16
  3. local function refuelIfNeeded()
  4.     if turtle.getFuelLevel() < 10 then
  5.         turtle.select(fuelSlot)
  6.         turtle.refuel(1)
  7.         return
  8.     end
  9. end
  10.  
  11. -- Function to check if there is a block to the left of the turtle
  12. local function checkLeft()
  13.     turtle.turnLeft()
  14.     local success = turtle.detect()
  15.     turtle.turnRight()
  16.     return success
  17. end
  18.  
  19. -- Function to place a block in the previous position of the turtle
  20. local function placeBlock()
  21.     turtle.select(lastSlot)
  22.     if turtle.getItemCount(lastSlot) > 0 then
  23.         turtle.place()
  24.     else
  25.         for slot = 1, 15 do
  26.             if turtle.getItemCount(slot) > 0 then
  27.                 lastSlot = slot
  28.                 turtle.select(slot)
  29.                 turtle.place()
  30.                 return
  31.             end
  32.         end
  33.     end
  34. end
  35.  
  36. -- Main function to move backwards and place a block
  37. local function moveBackwardsAndPlace()
  38.     refuelIfNeeded()
  39.     if checkLeft() then
  40.         if turtle.back() then
  41.             placeBlock()
  42.         else
  43.             turtle.turnLeft()
  44.         end
  45.     else
  46.         turtle.turnRight()
  47.         turtle.back()
  48.         placeBlock()
  49.     end
  50. end
  51.  
  52. local function inventoryNotEmpty()
  53.     local blockFound = false
  54.  
  55.     for slot = 1, 15 do
  56.         if turtle.getItemCount(slot) > 0 then
  57.             blockFound = true
  58.             break
  59.         end
  60.     end
  61.     return blockFound
  62. end
  63.  
  64. -- Main loop to repeat the movement and block placement
  65. while true do
  66.     if inventoryNotEmpty() then
  67.         moveBackwardsAndPlace()
  68.     else
  69.         turtle.suckUp(64)
  70.         os.sleep(1)
  71.     end
  72. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement