Advertisement
TechManDylan

CustomExcavate

May 22nd, 2025 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. -- Turtle Excavation Script with Ender Chest Support (Until Bedrock)
  2.  
  3. -- Position and orientation tracking
  4. local x, y, z = 0, 0, 0
  5. local dir = 0 -- 0: north, 1: east, 2: south, 3: west
  6. local startX, startY, startZ = 0, 0, 0
  7.  
  8. -- Ender Chest item name (adjust if different)
  9. local enderChestName = "enderstorage:ender_chest"
  10.  
  11. -- Find the slot containing the Ender Chest
  12. local function findEnderChestSlot()
  13.     for i = 1, 16 do
  14.         local item = turtle.getItemDetail(i)
  15.         if item and item.name == enderChestName then
  16.             return i
  17.         end
  18.     end
  19.     return nil
  20. end
  21.  
  22. -- Check if inventory is full
  23. local function isInventoryFull()
  24.     for i = 1, 16 do
  25.         if turtle.getItemCount(i) == 0 then
  26.             return false
  27.         end
  28.     end
  29.     return true
  30. end
  31.  
  32. -- Unload inventory into Ender Chest
  33. local function unloadInventory()
  34.     local slot = findEnderChestSlot()
  35.     if not slot then
  36.         print("Ender Chest not found in inventory!")
  37.         return false
  38.     end
  39.     turtle.select(slot)
  40.     if not turtle.placeUp() then
  41.         print("Cannot place Ender Chest above. Ensure space is clear.")
  42.         return false
  43.     end
  44.     for i = 1, 16 do
  45.         if i ~= slot then
  46.             turtle.select(i)
  47.             turtle.dropUp()
  48.         end
  49.     end
  50.     turtle.select(slot)
  51.     turtle.digUp()
  52.     turtle.select(1)
  53.     return true
  54. end
  55.  
  56. -- Movement functions with position tracking
  57. local function turnLeft()
  58.     turtle.turnLeft()
  59.     dir = (dir - 1) % 4
  60. end
  61.  
  62. local function turnRight()
  63.     turtle.turnRight()
  64.     dir = (dir + 1) % 4
  65. end
  66.  
  67. local function forward()
  68.     while not turtle.forward() do
  69.         turtle.dig()
  70.         sleep(0.5)
  71.     end
  72.     if dir == 0 then z = z - 1
  73.     elseif dir == 1 then x = x + 1
  74.     elseif dir == 2 then z = z + 1
  75.     elseif dir == 3 then x = x - 1 end
  76. end
  77.  
  78. local function down()
  79.     local attempts = 0
  80.     while not turtle.down() do
  81.         if not turtle.digDown() then
  82.             return false -- Unbreakable block below
  83.         end
  84.         sleep(0.5)
  85.         attempts = attempts + 1
  86.         if attempts > 5 then
  87.             return false -- Tried too many times
  88.         end
  89.     end
  90.     y = y - 1
  91.     return true
  92. end
  93.  
  94. local function up()
  95.     while not turtle.up() do
  96.         turtle.digUp()
  97.         sleep(0.5)
  98.     end
  99.     y = y + 1
  100. end
  101.  
  102. -- Face a specific direction
  103. local function face(targetDir)
  104.     while dir ~= targetDir do
  105.         turnRight()
  106.     end
  107. end
  108.  
  109. -- Return to the original starting position
  110. local function returnToStart()
  111.     while y < startY do up() end
  112.     face(3)
  113.     while x > startX do forward() end
  114.     face(1)
  115.     while x < startX do forward() end
  116.     face(0)
  117.     while z > startZ do forward() end
  118.     face(2)
  119.     while z < startZ do forward() end
  120.     face(0)
  121. end
  122.  
  123. -- Dig one layer
  124. local function digLayer(width, length)
  125.     for row = 1, length do
  126.         for col = 1, width - 1 do
  127.             turtle.digDown()
  128.             forward()
  129.             if isInventoryFull() then
  130.                 unloadInventory()
  131.             end
  132.         end
  133.         if row < length then
  134.             if row % 2 == 1 then
  135.                 turnRight()
  136.                 turtle.digDown()
  137.                 forward()
  138.                 turnRight()
  139.             else
  140.                 turnLeft()
  141.                 turtle.digDown()
  142.                 forward()
  143.                 turnLeft()
  144.             end
  145.         end
  146.     end
  147.     -- Return to top-left corner of layer
  148.     if length % 2 == 1 then
  149.         face(2)
  150.         for i = 1, width - 1 do forward() end
  151.         turnRight()
  152.         for i = 1, length - 1 do forward() end
  153.         face(0)
  154.     else
  155.         face(2)
  156.         for i = 1, length - 1 do forward() end
  157.         turnLeft()
  158.         for i = 1, width - 1 do forward() end
  159.         face(0)
  160.     end
  161. end
  162.  
  163. -- Main excavation loop
  164. local function excavateUntilBedrock(width, length)
  165.     while true do
  166.         digLayer(width, length)
  167.         if not down() then
  168.             print("Unbreakable block below. Stopping excavation.")
  169.             break
  170.         end
  171.     end
  172.     returnToStart()
  173. end
  174.  
  175. -- Main
  176. print("Enter excavation width:")
  177. local width = tonumber(read())
  178. print("Enter excavation length:")
  179. local length = tonumber(read())
  180.  
  181. if not width or not length then
  182.     print("Invalid input. Please enter numeric values.")
  183.     return
  184. end
  185.  
  186. print("Starting excavation...")
  187. startX, startY, startZ = x, y, z
  188. excavateUntilBedrock(width, length)
  189. print("Excavation complete. Returned to start.")
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement