Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Excavation Script with Ender Chest Support (Until Bedrock)
- -- Position and orientation tracking
- local x, y, z = 0, 0, 0
- local dir = 0 -- 0: north, 1: east, 2: south, 3: west
- local startX, startY, startZ = 0, 0, 0
- -- Ender Chest item name (adjust if different)
- local enderChestName = "enderstorage:ender_chest"
- -- Find the slot containing the Ender Chest
- local function findEnderChestSlot()
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == enderChestName then
- return i
- end
- end
- return nil
- end
- -- Check if inventory is full
- local function isInventoryFull()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- -- Unload inventory into Ender Chest
- local function unloadInventory()
- local slot = findEnderChestSlot()
- if not slot then
- print("Ender Chest not found in inventory!")
- return false
- end
- turtle.select(slot)
- if not turtle.placeUp() then
- print("Cannot place Ender Chest above. Ensure space is clear.")
- return false
- end
- for i = 1, 16 do
- if i ~= slot then
- turtle.select(i)
- turtle.dropUp()
- end
- end
- turtle.select(slot)
- turtle.digUp()
- turtle.select(1)
- return true
- end
- -- Movement functions with position tracking
- local function turnLeft()
- turtle.turnLeft()
- dir = (dir - 1) % 4
- end
- local function turnRight()
- turtle.turnRight()
- dir = (dir + 1) % 4
- end
- local function forward()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.5)
- end
- if dir == 0 then z = z - 1
- elseif dir == 1 then x = x + 1
- elseif dir == 2 then z = z + 1
- elseif dir == 3 then x = x - 1 end
- end
- local function down()
- local attempts = 0
- while not turtle.down() do
- if not turtle.digDown() then
- return false -- Unbreakable block below
- end
- sleep(0.5)
- attempts = attempts + 1
- if attempts > 5 then
- return false -- Tried too many times
- end
- end
- y = y - 1
- return true
- end
- local function up()
- while not turtle.up() do
- turtle.digUp()
- sleep(0.5)
- end
- y = y + 1
- end
- -- Face a specific direction
- local function face(targetDir)
- while dir ~= targetDir do
- turnRight()
- end
- end
- -- Return to the original starting position
- local function returnToStart()
- while y < startY do up() end
- face(3)
- while x > startX do forward() end
- face(1)
- while x < startX do forward() end
- face(0)
- while z > startZ do forward() end
- face(2)
- while z < startZ do forward() end
- face(0)
- end
- -- Dig one layer
- local function digLayer(width, length)
- for row = 1, length do
- for col = 1, width - 1 do
- turtle.digDown()
- forward()
- if isInventoryFull() then
- unloadInventory()
- end
- end
- if row < length then
- if row % 2 == 1 then
- turnRight()
- turtle.digDown()
- forward()
- turnRight()
- else
- turnLeft()
- turtle.digDown()
- forward()
- turnLeft()
- end
- end
- end
- -- Return to top-left corner of layer
- if length % 2 == 1 then
- face(2)
- for i = 1, width - 1 do forward() end
- turnRight()
- for i = 1, length - 1 do forward() end
- face(0)
- else
- face(2)
- for i = 1, length - 1 do forward() end
- turnLeft()
- for i = 1, width - 1 do forward() end
- face(0)
- end
- end
- -- Main excavation loop
- local function excavateUntilBedrock(width, length)
- while true do
- digLayer(width, length)
- if not down() then
- print("Unbreakable block below. Stopping excavation.")
- break
- end
- end
- returnToStart()
- end
- -- Main
- print("Enter excavation width:")
- local width = tonumber(read())
- print("Enter excavation length:")
- local length = tonumber(read())
- if not width or not length then
- print("Invalid input. Please enter numeric values.")
- return
- end
- print("Starting excavation...")
- startX, startY, startZ = x, y, z
- excavateUntilBedrock(width, length)
- print("Excavation complete. Returned to start.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement