Neon1432

stairdigger

Oct 27th, 2025 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. local args = { ... }
  2. local levels = 5
  3. if #args ~= 0 then
  4.     if tonumber(args[1]) == nil then
  5.         error("input must be a number")
  6.     else
  7.         levels = tonumber(args[1])
  8.     end
  9. end
  10.  
  11. local COBBLE = "minecraft:cobblestone"
  12. local DEEPSLATE = "minecraft:cobbled_deepslate"
  13. local COLLBE_STAIRS = "minecraft:cobblestone_stairs"
  14. local DEEPSLATE_STAIRS = "minecraft:cobbled_deepslate_stairs"
  15. local TORCH = "minecraft:torch"
  16.  
  17. local preferredBuildingBlock = COBBLE
  18. local preferredStairBlock = COLLBE_STAIRS
  19.  
  20. local function selectItem(itemname)
  21.     local function isItem()
  22.         local details = turtle.getItemDetail()
  23.         if details ~= nil then
  24.             if details.name == itemname then
  25.                 return true
  26.             end
  27.         end
  28.         return false
  29.     end
  30.     if isItem() then return true end
  31.     for i = 1, 16, 1 do
  32.         turtle.select(i)
  33.         if isItem() then return true end
  34.     end
  35.     return false
  36. end
  37.  
  38.  
  39. ---@enum Directions
  40. local Directions = {
  41.     forward = 0,
  42.     up = 2,
  43.     down = 4,
  44. }
  45.  
  46. local function selectAlternateBlock(blockname)
  47.     if blockname == COBBLE then
  48.         if selectItem(DEEPSLATE) then
  49.             preferredBuildingBlock = DEEPSLATE
  50.         else
  51.             error(COBBLE .. " and " .. DEEPSLATE .. " not found in inventory")
  52.         end
  53.     elseif blockname == COLLBE_STAIRS then
  54.         if selectItem(DEEPSLATE_STAIRS) then
  55.             preferredStairBlock = DEEPSLATE_STAIRS
  56.         else
  57.             error(COLLBE_STAIRS .. " and " .. DEEPSLATE_STAIRS .. " not found in inventory")
  58.         end
  59.     else
  60.         error(blockname .. " not found in inventory")
  61.     end
  62. end
  63.  
  64. ---@param direction Directions
  65. local function placeBlock(direction, blockname)
  66.     if blockname == nil then
  67.         blockname = preferredBuildingBlock
  68.     end
  69.     if not selectItem(blockname) then
  70.         selectAlternateBlock(blockname)
  71.     end
  72.     if direction == Directions.forward then
  73.         turtle.place()
  74.     end
  75.     if direction == Directions.down then
  76.         turtle.placeDown()
  77.     end
  78.     if direction == Directions.up then
  79.         turtle.placeUp()
  80.     end
  81. end
  82.  
  83. local function surroundingsSafe()
  84.     if turtle.inspectDown() or turtle.inspectUp() then
  85.         return false
  86.     end
  87.     return true
  88. end
  89.  
  90. ---comment
  91. ---@return boolean
  92. ---@param placeTorch boolean
  93. local function digLevel(placeTorch)
  94.     preferredBuildingBlock = COBBLE
  95.     local function moveToNextLevel()
  96.         turtle.dig()
  97.         turtle.forward()
  98.         turtle.digDown()
  99.         turtle.down()
  100.         turtle.turnLeft()
  101.         placeBlock(Directions.forward)
  102.         turtle.turnLeft()
  103.         turtle.dig()
  104.         turtle.forward()
  105.         placeBlock(Directions.up, COLLBE_STAIRS)
  106.         turtle.back()
  107.         turtle.turnRight()
  108.     end
  109.     local function digOneUp()
  110.         while not turtle.up() do
  111.             turtle.digUp()
  112.         end
  113.         placeBlock(Directions.forward)
  114.     end
  115.     moveToNextLevel()
  116.     turtle.up()
  117.     placeBlock(Directions.forward)
  118.     digOneUp()
  119.     digOneUp()
  120.     digOneUp()
  121.     placeBlock(Directions.up)
  122.     turtle.turnRight()
  123.     turtle.turnRight()
  124.     placeBlock(Directions.forward)
  125.     turtle.down()
  126.     placeBlock(Directions.forward)
  127.     turtle.down()
  128.     placeBlock(Directions.forward)
  129.     if placeTorch then
  130.         placeBlock(Directions.up, TORCH)
  131.     end
  132.     turtle.down()
  133.     placeBlock(Directions.forward)
  134.     if not surroundingsSafe() then
  135.         turtle.turnLeft()
  136.         turtle.down()
  137.         return false
  138.     end
  139.     turtle.down()
  140.     placeBlock(Directions.forward)
  141.     turtle.turnLeft()
  142.     return true
  143. end
  144.  
  145. local safe = true
  146. local counter = 0
  147. local function levelsAndTorch()
  148.     for i = 1, 5, 1 do
  149.         if not safe then return false end
  150.         if counter >= levels then return false end
  151.         if i < 5 then
  152.             safe = digLevel(false)
  153.         else
  154.             safe = digLevel(true)
  155.         end
  156.         counter = counter + 1
  157.     end
  158.     return true
  159. end
  160.  
  161. local canResume = true
  162. while canResume do
  163.     canResume = levelsAndTorch()
  164. end
  165.  
Advertisement
Add Comment
Please, Sign In to add comment