Advertisement
HappySunChild

tree

Apr 9th, 2025 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. local TARGET_WOOD = arg[1] or "minecraft:spruce"
  2.  
  3. local MAX_HEIGHT = 50
  4. local FUEL_THRESHOLD = MAX_HEIGHT * 2 + 20
  5.  
  6. local LOG = TARGET_WOOD .. "_log"
  7. local SAPLING = TARGET_WOOD .. "_sapling"
  8.  
  9. local function forward()
  10.     while not turtle.forward() do
  11.         turtle.dig()
  12.     end
  13. end
  14.  
  15. local function hasItem(name)
  16.     for slot = 1, 16 do
  17.         local info = turtle.getItemDetail(slot)
  18.  
  19.         if info and info.name == name then
  20.             return true, info.count, slot
  21.         end
  22.     end
  23.  
  24.     return false, 0, 1
  25. end
  26.  
  27. local function checkEnvironment()
  28.     turtle.turnLeft()
  29.  
  30.     if not turtle.suck(4) then
  31.         turtle.turnRight()
  32.  
  33.         return false, "Missing sapling storage, or no saplings in storage!"
  34.     end
  35.  
  36.     if not hasItem(SAPLING) then
  37.         turtle.turnRight()
  38.  
  39.         return false, "Non-sapling items in sapling storage!"
  40.     end
  41.  
  42.     turtle.drop(4)
  43.  
  44.     turtle.turnRight()
  45.  
  46.     local exists, info = turtle.inspect()
  47.  
  48.     if not exists or not info.name:match(TARGET_WOOD) then
  49.         return false, "Missing tree!"
  50.     end
  51.  
  52.     return true
  53. end
  54.  
  55. -- cycle functions
  56.  
  57. local function waitForTree()
  58.     while true do
  59.         local exists, block = turtle.inspect()
  60.  
  61.         if exists and block.name == LOG then
  62.             break
  63.         end
  64.  
  65.         if not exists or block.name ~= SAPLING then -- we're not looking at a sapling? look around, maybe we're out of alignment
  66.             turtle.turnRight()
  67.         end
  68.  
  69.         sleep(1)
  70.     end
  71. end
  72.  
  73. local function chopTree()
  74.     forward()
  75.  
  76.     local y = 0
  77.  
  78.     while turtle.detectUp() do
  79.         turtle.dig()
  80.         turtle.turnRight()
  81.         turtle.dig()
  82.         turtle.turnLeft()
  83.  
  84.         turtle.digUp()
  85.         turtle.up()
  86.  
  87.         if y > MAX_HEIGHT then
  88.             break
  89.         end
  90.  
  91.         y = y + 1
  92.     end
  93.  
  94.     turtle.dig()
  95.  
  96.     turtle.turnRight()
  97.     forward()
  98.     turtle.turnLeft()
  99.     forward()
  100.  
  101.     for _ = 1, y do
  102.         turtle.digDown()
  103.         turtle.down()
  104.     end
  105.  
  106.     turtle.back()
  107.     turtle.turnRight()
  108.     turtle.back()
  109.     turtle.turnLeft()
  110.     turtle.back()
  111. end
  112.  
  113. local function checkFuel()
  114.     if turtle.getFuelLevel() > FUEL_THRESHOLD then
  115.         return
  116.     end
  117.  
  118.     for slot = 1, 16 do
  119.         local info = turtle.getItemDetail(slot)
  120.  
  121.         if info then
  122.             turtle.select(slot)
  123.  
  124.             local isCombustable = turtle.refuel(0)
  125.  
  126.             if isCombustable then
  127.                 turtle.refuel(16)
  128.             end
  129.         end
  130.     end
  131.  
  132.     checkFuel()
  133. end
  134.  
  135. local function dropoffItems()
  136.     turtle.turnLeft()
  137.     turtle.turnLeft()
  138.  
  139.     for slot = 1, 16 do
  140.         local info = turtle.getItemDetail(slot)
  141.  
  142.         if info then
  143.             turtle.select(slot)
  144.  
  145.             if info.name == LOG then
  146.                 turtle.drop(info.count)
  147.             elseif info.name == SAPLING then
  148.                 turtle.turnRight()
  149.                 turtle.drop(info.count)
  150.                 turtle.turnLeft()
  151.             else
  152.                 turtle.turnLeft()
  153.                 turtle.drop(info.count)
  154.                 turtle.turnRight()
  155.             end
  156.         end
  157.     end
  158.  
  159.     turtle.select(1)
  160.  
  161.     turtle.turnRight()
  162.     turtle.turnRight()
  163. end
  164.  
  165. local function replantTree()
  166.     local exists, count, slot = hasItem(SAPLING)
  167.  
  168.     if not exists or count < 4 then
  169.         turtle.turnLeft()
  170.  
  171.         if slot then
  172.             turtle.select(slot)
  173.             turtle.transferTo(1)
  174.             turtle.select(1)
  175.         end
  176.  
  177.         turtle.suck(4 - count)
  178.         turtle.turnRight()
  179.     end
  180.  
  181.     local _, newCount = hasItem(SAPLING)
  182.  
  183.     if newCount < 4 then
  184.         printError("Not enough saplings in storage!")
  185.  
  186.         sleep(30)
  187.  
  188.         return replantTree()
  189.     end
  190.  
  191.     turtle.forward()
  192.     turtle.forward()
  193.  
  194.     for _ = 1, 2 do
  195.         turtle.turnRight()
  196.         turtle.place()
  197.         turtle.turnLeft()
  198.         turtle.back()
  199.         turtle.place()
  200.     end
  201. end
  202.  
  203. term.clear()
  204. term.setCursorPos(1, 1)
  205.  
  206. print("checking environment")
  207.  
  208. local good, reason = checkEnvironment()
  209.  
  210. if not good then
  211.     printError(reason)
  212.  
  213.     return
  214. end
  215.  
  216. print("env good")
  217.  
  218. local cycles = 0
  219.  
  220. while true do
  221.     print("waiting for tree...")
  222.     waitForTree()
  223.  
  224.     print("chop cycle " .. cycles)
  225.     chopTree()
  226.  
  227.     print("checking fuel...")
  228.     checkFuel()
  229.  
  230.     dropoffItems()
  231.  
  232.     print("replanting...")
  233.     replantTree()
  234.  
  235.     cycles = cycles + 1
  236. end
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement