CCCPaste

stairdigger

local args = { ... }
if #args ~= 0 then
    local levels = 5
    if type(args[1]) == "number" then
        levels = args[1]
        print("arg set")
    end
end

local function selectItem(itemname)
    local function isItem()
        local details = turtle.getItemDetail()
        if details ~= nil then
            if details.name == itemname then
                return true
            end
        end
        return false
    end
    if isItem() then return true end
    for i = 1, 16, 1 do
        turtle.select(i)
        if isItem() then return true end
    end
    return false
end


---@enum Directions
local Directions = {
    forward = 0,
    up = 2,
    down = 4,
}

---@param direction Directions
local function placeBlock(direction)
    selectItem("minecraft:cobblestone")
    if direction == Directions.forward then
        if not turtle.inspect() then
            turtle.place()
        end
    end
    if direction == Directions.down then
        if not turtle.inspectDown() then
            turtle.placeDown()
        end
    end
    if direction == Directions.up then
        if not turtle.inspectUp() then
            turtle.placeUp()
        end
    end
end

local function surroundingsSafe()
    if turtle.inspectDown() or turtle.inspectUp() then
        return false
    end
    return true
end

---comment
---@return boolean
local function digLevel()
    turtle.dig()
    turtle.forward()
    turtle.digDown()
    turtle.down()
    placeBlock(Directions.down)
    turtle.up()
    turtle.digUp()
    turtle.up()
    turtle.digUp()
    turtle.up()
    turtle.digUp()
    turtle.up()
    placeBlock(Directions.up)
    turtle.down()
    turtle.down()
    turtle.down()
    if not surroundingsSafe() then
        return false
    end
    turtle.down()
    return true
end

local safe = true
while safe do
    safe = digLevel()

    levels = levels - 1
    if levels <= 0 then
        return true
    end
end