Schmille

TurtleTree

Mar 29th, 2022 (edited)
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. SaplingSlot = 1
  2. BoneSlot = 2
  3. WoodSlot = 3
  4. FlintSlot = 16
  5. EdgeDistance = 4
  6.  
  7. -- SaplingSide = "right"
  8. -- BoneSide = "back"
  9. -- DropSide = "right"
  10. -- FlintSide = "left"
  11.  
  12. function EnsureSaplingPlaced()
  13.     if not turtle.detect() then
  14.         turtle.select(SaplingSlot)
  15.     end
  16.     turtle.place()
  17. end
  18.  
  19. function Fertilize()
  20.     turtle.select(BoneSlot)
  21.     turtle.place()
  22. end
  23.  
  24. function CheckGrown()
  25.     if turtle.detectUp() then
  26.         turtle.digUp()
  27.     end
  28.     turtle.up()
  29.     local detected = turtle.detect()
  30.     turtle.down()
  31.     return detected
  32. end
  33.  
  34. function IsEmpty(slot)
  35.     local last = turtle.getSelectedSlot()
  36.     turtle.select(slot)
  37.     local empty = turtle.getItemCount() <= 0
  38.     turtle.select(last)
  39. end
  40.  
  41. function EnsureHasSapling()
  42.     local hasBonemeal = not IsEmpty(BoneSlot)
  43.     turtle.select(SaplingSlot)
  44.     turtle.turnRight()
  45.     turtle.suck()
  46.     FlushExcessSaplings(hasBonemeal)
  47.     turtle.turnLeft()
  48. end
  49.  
  50. function FlushExcessSaplings(hasBonemeal)
  51.     for i=1,16,1 do
  52.         turtle.select(i)
  53.         if i ~= SaplingSlot and not (hasBonemeal and i == BoneSlot) and not IsFlintAndSteel(i) then
  54.             turtle.drop()
  55.         end
  56.     end
  57. end
  58.  
  59. function EnsureHasBonemeal()
  60.     turtle.select(BoneSlot)
  61.     turtle.turnLeft()
  62.     turtle.turnLeft()
  63.     turtle.suck()
  64.     FlushExcessBonemeal()
  65.     turtle.turnLeft()
  66.     turtle.turnLeft()
  67. end
  68.  
  69. function FlushExcessBonemeal()
  70.     for i=1,16,1 do
  71.         if i ~= SaplingSlot and i ~= BoneSlot and not IsFlintAndSteel(i) then
  72.             turtle.select(i)
  73.             turtle.drop()
  74.         end
  75.     end
  76. end
  77.  
  78. function IsFlintAndSteel(slot)
  79.     local data = turtle.getItemDetail(slot)
  80.     if data == nil then
  81.         return false
  82.     end
  83.  
  84.     local id = data.name
  85.     return id == "minecraft:flint_and_steel"
  86. end
  87.  
  88. function HasFlintAndSteel()
  89.     return not IsEmpty(FlintSlot) and IsFlintAndSteel(FlintSlot)
  90. end
  91.  
  92. function EnsureFlintAndSteel()
  93.     if not HasFlintAndSteel() then
  94.         turtle.turnLeft()
  95.         turtle.select(FlintSlot)
  96.         turtle.suck()
  97.         turtle.turnRight()
  98.     end
  99. end
  100.  
  101. function FellTree()
  102.     turtle.select(WoodSlot)
  103.     turtle.dig()
  104.     turtle.forward()
  105.  
  106.     while turtle.detectUp() do
  107.         turtle.digUp()
  108.         turtle.up()
  109.     end
  110.     os.sleep(2)
  111.  
  112.     local needsBurn = false
  113.     local last = turtle.getSelectedSlot()
  114.     turtle.select(FlintSlot)
  115.     while not turtle.detectDown() do
  116.         if needsBurn then
  117.             turtle.placeUp()
  118.         end
  119.  
  120.         needsBurn = turtle.detect()
  121.         turtle.down()
  122.     end
  123.     turtle.select(last)
  124.  
  125.     turtle.back()
  126. end
  127.  
  128. function DropProduce()
  129.     turtle.turnRight()
  130.     for i=1,16,1 do
  131.         if i ~= BoneSlot and i ~= SaplingSlot and i ~= FlintSlot then
  132.             turtle.select(i)
  133.             turtle.drop()
  134.         end
  135.     end
  136.     turtle.turnLeft()
  137. end
  138.  
  139. function GoToRefillPos()
  140.     for i=1,EdgeDistance,1 do
  141.         turtle.back()
  142.     end
  143. end
  144.  
  145. function GoToGrowPos()
  146.     for i=1,EdgeDistance,1 do
  147.         turtle.forward()
  148.     end
  149. end
  150.  
  151. while true do
  152.     print("-- Ensuring Saplings")
  153.     EnsureHasSapling()
  154.     print("-- Ensuring Bonemeal")
  155.     EnsureHasBonemeal()
  156.     print("-- Ensuring Flint and Steel")
  157.     EnsureFlintAndSteel()
  158.     print("-- Moving to GrowPos")
  159.     GoToGrowPos()
  160.     print("-- Placing Sapling")
  161.     EnsureSaplingPlaced()
  162.  
  163.     print("-- Start Fertilize-Loop")
  164.     while not CheckGrown() do
  165.         Fertilize()
  166.     end
  167.     print("-- Felling Tree")
  168.     FellTree()
  169.     print("-- Moving to RefillPos")
  170.     GoToRefillPos()
  171.     print("-- Dropping Produce")
  172.     print("-- Cycle done.")
  173.     print("")
  174.     DropProduce()
  175.  
  176.     os.sleep(1)
  177. end
Add Comment
Please, Sign In to add comment