TurtleGuy09

treefarm

Nov 17th, 2025 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. -- Tree-Farm Turtle (based on existing public script)
  2.  
  3. local sapling_name = "sapling"
  4. local min_fuel = 20
  5.  
  6. -- Detect item in inventory by name
  7. local function findSlot(itemName)
  8.     for i = 1,16 do
  9.         local d = turtle.getItemDetail(i)
  10.         if d and string.find(d.name, itemName) then
  11.             return i
  12.         end
  13.     end
  14.     return nil
  15. end
  16.  
  17. -- Fuel-check and refuel
  18. local function refuelIfNeeded(fuelSlot)
  19.     turtle.select(fuelSlot)
  20.     while turtle.getFuelLevel() < min_fuel do
  21.         if turtle.getItemCount(fuelSlot) == 0 then
  22.             print("Need more fuel in slot " .. fuelSlot)
  23.             return false
  24.         end
  25.         turtle.refuel(1)
  26.     end
  27.     return true
  28. end
  29.  
  30. -- Climb and harvest
  31. local function harvestColumn()
  32.     -- go up harvesting
  33.     while turtle.detectUp() do
  34.         turtle.digUp()
  35.         turtle.up()
  36.     end
  37.     -- go down back to base
  38.     while turtle.down() do end
  39. end
  40.  
  41. -- Plant sapling below when possible
  42. local function plantBelow(saplingSlot)
  43.     turtle.select(sapplingSlot)
  44.     if turtle.getItemCount(sapplingSlot) > 0 and not turtle.detectDown() then
  45.         turtle.placeDown()
  46.     end
  47. end
  48.  
  49. -- Main loop
  50. local saplingSlot = findSlot(sapling_name)
  51. local fuelSlot = findSlot("coal") or findSlot("charcoal")
  52.  
  53. if not saplingSlot then
  54.     error("No sapling slot found.")
  55. end
  56. if not fuelSlot then
  57.     error("No fuel slot found.")
  58. end
  59.  
  60. while true do
  61.     if not refuelIfNeeded(fuelSlot) then
  62.         break
  63.     end
  64.  
  65.     -- Plant, harvest, collect, then replant
  66.     plantBelow(saplingSlot)
  67.     harvestColumn()
  68.  
  69.     -- Collect dropped items around (down / up / forward)
  70.     for i = 1,16 do
  71.         turtle.select(i)
  72.         turtle.suck()
  73.         turtle.suckUp()
  74.         turtle.suckDown()
  75.     end
  76.  
  77.     -- Drop into a chest below
  78.     for i = 1,16 do
  79.         turtle.select(i)
  80.         local count = turtle.getItemCount(i)
  81.         if count > 0 and i ~= sapplingSlot and i ~= fuelSlot then
  82.             turtle.dropDown(count)
  83.         end
  84.     end
  85.  
  86.     print("Cycle complete, refueling and replanting...")
  87. end
  88.  
Advertisement
Add Comment
Please, Sign In to add comment