Advertisement
PhamKun

Fully Auto Spruce Chopper

Aug 14th, 2022 (edited)
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. --util function
  2.     --check fuel and refuel
  3. local function checkF()
  4.     if turtle.getFuelLevel() == 0 then
  5.         turtle.select(1)
  6.         turtle.refuel(1)
  7.     end
  8. end
  9.  
  10.     --drop all item except last two slot
  11. local function dropItem()
  12.     for i = 3, 16, 1 do
  13.         turtle.select(i)
  14.         turtle.dropDown()
  15.     end
  16. end
  17.  
  18.     --suck Sapling
  19. local function suckItemSapling()
  20.     turtle.select(2)
  21.     local data = turtle.getItemDetail()
  22.     if data == nil then
  23.         turtle.turnRight()
  24.         turtle.suck(32)
  25.         turtle.turnLeft()
  26.     elseif not data.name == "minecraft:spruce_sapling" then
  27.         turtle.dropDown()
  28.         turtle.turnRight()
  29.         turtle.suck(32)
  30.         turtle.turnLeft()
  31.     elseif data.name == "minecraft:spruce_sapling" then
  32.         if data.count < 32 then
  33.             turtle.turnRight()
  34.             turtle.suck(32 - data.count)
  35.             turtle.turnLeft()
  36.         end
  37.     end
  38. end
  39.  
  40. local function suckItemCoal()
  41.     turtle.select(1)
  42.     local data = turtle.getItemDetail()
  43.     if data == nil then
  44.         turtle.suckUp()
  45.     elseif not data.name == "minecraft:charcoal" then
  46.         turtle.dropDown()
  47.         turtle.suckUp()
  48.     elseif data.name == "minecraft:charcoal" then
  49.         if data.count < 64 then
  50.             turtle.suckUp(64 - data.count)
  51.         end
  52.     end
  53. end
  54.    
  55. --tree related functions
  56.     --chop tree function
  57. local function chopUp()
  58.     local high = 1
  59.     while turtle.up() == false do
  60.         turtle.dig()
  61.         turtle.digUp()
  62.         checkF()
  63.         turtle.up()
  64.         high = high + 1
  65.     end
  66.     return high
  67. end
  68.  
  69. local function chopDown(a)
  70.     for i = 1, a, 1 do
  71.         turtle.dig()
  72.         turtle.digDown()
  73.         checkF()
  74.         turtle.down()
  75.     end
  76.     turtle.dig()
  77. end
  78.  
  79.     --check if tree grows
  80. local function checkSapling()
  81.     local isBlock, block = turtle.inspect()
  82.     if block.name == "minecraft:spruce_log" then
  83.         return true
  84.     end
  85.     return false
  86. end
  87.  
  88.     --move back 7blocks to make sure tree can grow
  89. local function moveSevenBlockBack()
  90.     for i = 1, 7 , 1 do
  91.         checkF()
  92.         if not turtle.back() then
  93.             turtle.turnRight()
  94.             turtle.turnRight()
  95.             turtle.dig()
  96.             turtle.turnRight()
  97.             turtle.turnRight()
  98.             turtle.back()
  99.         end
  100.     end
  101. end
  102.     --move forward to check tree
  103. local function moveSevenBlockForward()
  104.     for i = 1, 7 , 1 do
  105.         checkF()
  106.         if not turtle.forward() then
  107.             turtle.dig()
  108.             turtle.forward()
  109.         end
  110.     end
  111. end
  112.  
  113. --plant sapling
  114.     --at the end of this function, the turtle should return to original position a.k.a setX, setY, setZ
  115.     --spaling slot will be the 15th slot
  116. local function placeSapling()
  117.     turtle.select(2)
  118.     turtle.place()
  119.     turtle.turnLeft()
  120.     checkF()
  121.     turtle.select(2)
  122.     turtle.back()
  123.     turtle.place()
  124.     turtle.turnRight()
  125.     turtle.place()
  126.     checkF()
  127.     turtle.select(2)
  128.     turtle.back()
  129.     turtle.place()
  130. end
  131.  
  132.  
  133.     --functions use for init()
  134.     --clean up older log if there is any
  135. local function chopUpInit()
  136.     local high = 1
  137.     while high <= 32 do
  138.         turtle.dig()
  139.         turtle.digUp()
  140.         checkF()
  141.         turtle.up()
  142.         high = high + 1
  143.     end
  144.     return high
  145. end
  146.  
  147. --main
  148. checkF()
  149. turtle.dig()
  150. turtle.forward()
  151. local a = chopUpInit() - 1
  152. turtle.turnLeft()
  153. checkF()
  154. turtle.forward()
  155. turtle.turnRight()
  156. chopDown(a)
  157. placeSapling()
  158. moveSevenBlockBack()
  159. sleep(60)
  160. while true do
  161.     moveSevenBlockForward()
  162.     if checkSapling() then
  163.         turtle.dig()
  164.         checkF()
  165.         turtle.forward()
  166.         local high = chopUp()
  167.         turtle.turnLeft()
  168.         checkF()
  169.         turtle.forward()
  170.         turtle.turnRight()
  171.         chopDown(high)
  172.         placeSapling()
  173.         moveSevenBlockBack()
  174.         dropItem()
  175.         suckItemCoal()
  176.         suckItemSapling()
  177.     else
  178.         moveSevenBlockBack()
  179.         suckItemCoal()
  180.         suckItemSapling()
  181.     end
  182.     sleep(120)
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement