Xmann1

Untitled

Dec 22nd, 2020 (edited)
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. function find_item(name, count)
  2.     for i=1, 16 do
  3.         turtle.select(i)
  4.         item = turtle.getItemDetail()
  5.         if item then
  6.             if item.name == "minecraft:sapling" then
  7.                 if count == nil then
  8.                     return true
  9.                 end
  10.                 if count <= item.count then
  11.                     return true
  12.                 end
  13.             end
  14.         end
  15.     end
  16.     return false
  17. end
  18.  
  19. function forward()
  20.     if not turtle.forward() then
  21.         a, b = turtle.inspect()
  22.         if a then
  23.             if b.name == "minecraft:leaves" then
  24.                 turtle.dig()
  25.                 turtle.forward()
  26.             end
  27.         end
  28.     end
  29. end
  30.  
  31. function up()
  32.     if not turtle.up() then
  33.         a, b = turtle.inspectUp()
  34.         if a then
  35.             if b.name == "minecraft:leaves" then
  36.                 turtle.digUp()
  37.                 turtle.up()
  38.             end
  39.         end
  40.     end
  41. end
  42.  
  43. function down()
  44.     if not turtle.down() then
  45.         a, b = turtle.inspectDown()
  46.         if a then
  47.             if b.name == "minecraft:leaves" then
  48.                 turtle.digDown()
  49.                 turtle.down()
  50.             end
  51.         end
  52.     end
  53. end
  54.  
  55. function harvest_trees()
  56.     print("Harvesting trees")
  57.     refuel()
  58.     down()
  59.     turtle.dig()
  60.     forward()
  61.     turtle.dig()
  62.     turtle.turnLeft()
  63.     local leftSide = false;
  64.     a, b = turtle.inspect()
  65.     print(b.name)
  66.     if a then
  67.         if b.name == "minecraft:log" then
  68.             leftSide = true;
  69.         end
  70.     end
  71.     print("leftSide: " .. tostring(leftSide))
  72.     turtle.turnRight()
  73.  
  74.     local tree_height = 0
  75.     while turtle.detectUp() do
  76.         tree_height = tree_height + 1
  77.         turtle.digUp()
  78.         up()
  79.         turtle.dig()
  80.         refuel()
  81.     end
  82.     if leftSide then turtle.turnLeft() else turtle.turnRight() end
  83.     turtle.dig()
  84.     forward()
  85.     if leftSide then turtle.turnRight() else turtle.turnLeft() end
  86.     turtle.dig()
  87.     for i=1, tree_height do
  88.         refuel()
  89.         turtle.digDown()
  90.         down()
  91.         turtle.dig()
  92.     end
  93.     if find_item("minecraft:sapling", 4) then
  94.         print("Sapling found in inventory, replanting")
  95.         refuel()
  96.         up()
  97.         turtle.placeDown()
  98.         forward()
  99.         turtle.placeDown()
  100.         if leftSide then turtle.turnRight() else turtle.turnLeft() end
  101.         forward()
  102.         turtle.placeDown()
  103.         if leftSide then turtle.turnRight() else turtle.turnLeft() end
  104.         forward()
  105.         turtle.placeDown()
  106.         forward()
  107.         down()
  108.     else
  109.         refuel()
  110.         if leftSide then turtle.turnRight() else turtle.turnLeft() end
  111.         forward()
  112.         if leftSide then turtle.turnRight() else turtle.turnLeft() end
  113.         forward()
  114.     end
  115.     up()
  116. end
  117.  
  118. function check_grown()
  119.     for i=1, 4 do
  120.         turtle.turnRight()
  121.         a, b = turtle.inspect()
  122.         if a then
  123.             if b.name == "minecraft:log" then
  124.                 return true
  125.             end
  126.         end
  127.     end
  128.     return false
  129. end
  130.  
  131. function wait_for_grow()
  132.     while not check_grown() do
  133.         sleep(5)
  134.     end
  135.     print("Tree grown!")
  136. end
  137.  
  138. function collect_saplings()
  139.     print("Collecting saplings")
  140.     local above = false
  141.     refuel()
  142.     above = log_avoidant_forward(above)
  143.     above = log_avoidant_forward(above)
  144.     turtle.turnRight()
  145.     above = log_avoidant_forward(above)
  146.     above = log_avoidant_forward(above)
  147.     turtle.turnRight()
  148.  
  149.     for j=1, 2 do
  150.         for i=1, 4 do
  151.             above = log_avoidant_forward(above)
  152.         end
  153.         turtle.turnRight()
  154.         above = log_avoidant_forward(above)
  155.         turtle.turnRight()
  156.         for i=1, 4 do
  157.             above = log_avoidant_forward(above)
  158.         end
  159.  
  160.         turtle.turnLeft()
  161.         above = log_avoidant_forward(above)
  162.         turtle.turnLeft()
  163.     end
  164.     for i=1, 4 do
  165.         above = log_avoidant_forward(above)
  166.     end
  167.     turtle.turnLeft()
  168.     above = log_avoidant_forward(above)
  169.     above = log_avoidant_forward(above)
  170.     turtle.turnLeft()
  171.     above = log_avoidant_forward(above)
  172.     above = log_avoidant_forward(above)
  173. end
  174.  
  175. function organize_inventory()
  176.     refuel()
  177.     down()
  178.     turtle.select(1)
  179.     item = turtle.getItemDetail()
  180.     if item ~= nil and item.name ~= "minecraft:log" then
  181.         turtle.dropDown()
  182.     end
  183.     turtle.select(2)
  184.     item = turtle.getItemDetail()
  185.     if item ~= nil and item.name ~= "minecraft:sapling" then
  186.         turtle.dropDown()
  187.     end
  188.  
  189.     for i=3, 16 do
  190.         turtle.select(i)
  191.         item = turtle.getItemDetail()
  192.         if item ~= nil then
  193.             if item.name == "minecraft:log" then
  194.                 turtle.transferTo(1)
  195.             elseif item.name == "minecraft:sapling" then
  196.                 turtle.transferTo(2)
  197.             end
  198.             turtle.dropDown()
  199.         end
  200.     end
  201.  
  202.     up()
  203. end
  204.  
  205. function log_avoidant_forward(above)
  206.     refuel()
  207.     turtle.suckDown()
  208.     a, b = turtle.inspect()
  209.     if a then
  210.         if b.name == "minecraft:log" then
  211.             if not above then
  212.                 up()
  213.             end
  214.             turtle.dig()
  215.             forward()
  216.             return true
  217.         else
  218.             turtle.dig()
  219.             forward()
  220.             if above then
  221.                 down()
  222.             end
  223.             return false
  224.         end
  225.     else
  226.         forward()
  227.         if above then
  228.             down()
  229.         end
  230.         return false
  231.     end
  232. end
  233.  
  234. function refuel()
  235.     if turtle.getFuelLevel() > 8 then
  236.         return
  237.     end
  238.     for i=1, 16 do
  239.         turtle.select(i)
  240.         if turtle.refuel(1) then
  241.             break
  242.         end
  243.     end
  244. end
  245.  
  246. refuel()
  247. if not turtle.detectDown() then
  248.     down()
  249. end
  250.  
  251. up()
  252.  
  253. while true do
  254.     wait_for_grow()
  255.     while check_grown() do
  256.         harvest_trees()
  257.     end
  258.     collect_saplings()
  259.     organize_inventory()
  260. end
  261.  
Add Comment
Please, Sign In to add comment