Advertisement
gur111

stairs.lua

Jun 13th, 2025 (edited)
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. -- Function to parse command line arguments
  2. local function parseArgs(args)
  3.     local count = tonumber(args[1])
  4.     local acceptAny = false
  5.  
  6.     if args[2] == "--accept-any" then
  7.         acceptAny = true
  8.     end
  9.  
  10.     return count, acceptAny
  11. end
  12.  
  13. -- Function to find and select more stairs
  14. local function selectStairs(initialItemDetail, acceptAny)
  15.     for slot = 1, 16 do
  16.         local itemDetail = turtle.getItemDetail(slot)
  17.         if itemDetail then
  18.             if acceptAny or (itemDetail.name == initialItemDetail.name) then
  19.                 turtle.select(slot)
  20.                 return true
  21.             end
  22.         end
  23.     end
  24.     return false
  25. end
  26.  
  27. -- Function to place a stair block below the turtle
  28. local function placeStair(initialItemDetail, acceptAny)
  29.     while not turtle.placeDown() do
  30.         turtle.digDown()
  31.         if turtle.getItemCount() == 0 then
  32.             if not selectStairs(initialItemDetail, acceptAny) then
  33.                 print("Out of stairs!")
  34.                 return false
  35.             end
  36.         end
  37.     end
  38.     return true
  39. end
  40.  
  41. -- Function to move the turtle up safely
  42. local function moveUp()
  43.     while not turtle.up() do
  44.         turtle.digUp()
  45.     end
  46.     while turtle.detectUp() do
  47.         turtle.digUp()
  48.     end
  49. end
  50.  
  51. -- Function to move the turtle forward safely
  52. local function moveForward()
  53.     while not turtle.forward() do
  54.         turtle.dig()
  55.     end
  56. end
  57.  
  58. -- Main function to build stairs
  59. local function buildStairs(count, acceptAny)
  60.     local initialItemDetail = turtle.getItemDetail(1)
  61.     if not initialItemDetail then
  62.         print("No stairs in slot 1!")
  63.         return
  64.     end
  65.  
  66.     for i = 1, count do
  67.         if not placeStair(initialItemDetail, acceptAny) then
  68.             break
  69.         end
  70.         moveUp()
  71.         moveForward()
  72.     end
  73. end
  74.  
  75. -- Get command line arguments
  76. local tArgs = {...}
  77. local stairsCount, acceptAny = parseArgs(tArgs)
  78.  
  79. -- Check if the number of stairs is provided
  80. if not stairsCount then
  81.     print("Usage: buildStairs <number_of_stairs> [--accept-any]")
  82.     return
  83. end
  84.  
  85. -- Start building stairs
  86. buildStairs(stairsCount, acceptAny)
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement