CCCPaste

6db7559daaf55

-- Never-Stop 9x9 Quarry with Custom Starting Y
-- Mines down to Y=-53
-- Auto-refuels using chest or mined coal
-- Auto-empties inventory
-- Place chest behind turtle with fuel and storage

local quarrySize = 9
local targetY = -53
local fuelSlot = 16
local tossGarbage = true

-- -------------------------
-- Ask user for starting Y
-- -------------------------
print("Enter starting Y-level (number):")
local startY = tonumber(read())
if not startY then
    error("Invalid Y-level! Must be a number.")
end

local currentY = startY

-- -------------------------
-- Utility Functions
-- -------------------------
local function tryRefuel()
    turtle.select(fuelSlot)
    if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 100 then
        turtle.suck() -- chest behind
        if turtle.getItemCount(fuelSlot) > 0 then
            turtle.refuel()
            return
        end
        for s=1,16 do
            turtle.select(s)
            local detail = turtle.getItemDetail()
            if detail then
                local name = detail.name
                if name=="minecraft:coal" or name=="minecraft:charcoal" then
                    turtle.refuel()
                    return
                end
            end
        end
        print("Out of fuel! Waiting for chest or mined coal...")
        sleep(2)
    end
end

local function emptyInventory()
    for s=1,16 do
        turtle.select(s)
        turtle.drop()
    end
end

local function checkInventory()
    local fullSlots = 0
    for s=1,16 do
        if turtle.getItemCount(s) > 0 then
            if tossGarbage then
                local detail = turtle.getItemDetail(s)
                if detail then
                    local name = detail.name
                    if name=="minecraft:cobblestone" or name=="minecraft:stone" or name=="minecraft:dirt" or name=="minecraft:gravel" then
                        turtle.select(s)
                        turtle.drop()
                    end
                end
            end
            if turtle.getItemCount(s) > 0 then fullSlots = fullSlots + 1 end
        end
    end
    if fullSlots >= 16 then emptyInventory() end
end

-- -------------------------
-- Safe movement
-- -------------------------
local function safeForward()
    while true do
        if turtle.forward() then return end
        turtle.dig()
        sleep(0.1)
    end
end

local function safeDown()
    while true do
        if turtle.down() then
            currentY = currentY - 1
            return
        end
        turtle.digDown()
        sleep(0.1)
    end
end

-- -------------------------
-- Mine one 9x9 layer
-- -------------------------
local function mineLayer()
    local turn = 0
    for row=1,quarrySize do
        for col=1,quarrySize-1 do
            safeForward()
            checkInventory()
            tryRefuel()
        end
        if row < quarrySize then
            if turn == 0 then
                turtle.turnRight()
                safeForward()
                turtle.turnRight()
                turn = 1
            else
                turtle.turnLeft()
                safeForward()
                turtle.turnLeft()
                turn = 0
            end
            checkInventory()
            tryRefuel()
        end
    end
end

-- -------------------------
-- Return to chest and empty
-- -------------------------
local function returnToChest()
    turtle.turnLeft()
    turtle.turnLeft()
    for i=1,quarrySize-1 do safeForward() end
    turtle.turnLeft()
    turtle.turnLeft()
    emptyInventory()
end

-- -------------------------
-- Main Loop
-- -------------------------
while currentY > targetY do
    print("Mining layer at Y="..currentY)
    mineLayer()

    if currentY > targetY then
        for i=1,quarrySize-1 do safeDown() end
        turtle.turnLeft()
        turtle.turnLeft() -- face starting row
    end
end

returnToChest()
print("Quarry complete! Mined down to Y="..targetY)