Advertisement
CelticCoder

turtleCutTree

May 11th, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. -- NOTE FOR NORMAL MINECRAFT USE cutTree() INSTEAD OF THE FOR LOOP WITH turtle.dig()
  2.  
  3. os.loadAPI("turtleChest.lua")
  4.  
  5. function findStartPosition()
  6.     for i = 1, 4 do
  7.         success, item = turtle.inspect()
  8.         if item.name == "minecraft:chest" then
  9.             break
  10.         end
  11.         turtle.turnLeft()
  12.     end
  13.     turtle.turnLeft()
  14.     turtle.turnLeft()
  15. end
  16.  
  17. function checkList(list, thing)
  18.     for _, str in ipairs(list) do
  19.         if thing == str then
  20.             return true
  21.         end
  22.     end
  23.     return false
  24. end
  25.  
  26. function cutTree(logNames)
  27.     height = 0
  28.     turtle.dig()
  29.     turtle.forward()
  30.     success, item = turtle.inspectUp()
  31.     while checkList(logNames, item.name) do
  32.         turtle.digUp()
  33.         turtle.up()
  34.         height = height + 1
  35.         success, item = turtle.inspectUp()
  36.     end
  37.     for i = 0, height do
  38.         turtle.down()
  39.     end
  40.     turtle.back()
  41. end
  42.  
  43. function guessAndCheckAll()
  44.     turtle.turnRight()
  45.     turtleChest.insertAll()
  46. end
  47.  
  48. function guessAndCheckSome()
  49.     turtle.turnRight()
  50.     turtleChest.insertSome(2)
  51. end
  52.  
  53. function refuelSaplings()
  54.     turtle.select(1)
  55.     turtle.suckDown()
  56.     if turtle.getItemCount(1) == 0 then
  57.         sapling = false
  58.         return "out"
  59.     else
  60.         turtle.place()
  61.     end
  62. end
  63.  
  64. function placeSapling(saplingNames)
  65.     turtle.place()
  66.     success, item = turtle.inspect()
  67.     if item.name == nil then
  68.         return refuelSaplings()
  69.     end
  70.     --if turtle accidentally doesn't place a sapling
  71.     if not checkList(saplingNames, item.name) then
  72.         turtle.dig()
  73.         for i = 1, 4 do
  74.             guessAndCheckAll()
  75.         end
  76.         -- if Chest is full (since turtle isn't empty)
  77.         if turtleChest.countCheck(count) ~= 0 then
  78.             full = true
  79.             return "out"
  80.         else
  81.             return refuelSaplings()
  82.         end
  83.     end
  84. end
  85.  
  86. logNames = {
  87.     "minecraft:oak_log",
  88.     "minecraft:spruce_log",
  89.     "minecraft:birch_log",
  90.     "minecraft:jungle_log",
  91.     "minecraft:acacia_log",
  92.     "minecraft:dark_oak_log",
  93.     "minecraft:crimson_stem",
  94.     "minecraft:warped_stem",
  95. }
  96.  
  97. saplingNames = {
  98.     "minecraft:oak_sapling",
  99.     "minecraft:spruce_sapling",
  100.     "minecraft:birch_sapling",
  101.     "minecraft:jungle_sapling",
  102.     "minecraft:acacia_sapling",
  103.     "minecraft:dark_oak_sapling",
  104.     "minecraft:azalea",
  105.     "minecraft:flowering_azalea",
  106. }
  107.  
  108. fuel = turtle.getFuelLevel()
  109. success = true
  110. sapling = true
  111. full = false
  112. left = 0
  113.  
  114. findStartPosition()
  115.  
  116. turtle.select(1)
  117. if turtle.getItemCount(1) == 0 then
  118.     turtle.suckDown()
  119. end
  120.  
  121. turtle.turnLeft()
  122. placeSapling(saplingNames)
  123. turtle.turnRight()
  124. placeSapling(saplingNames)
  125. turtle.turnRight()
  126. placeSapling(saplingNames)
  127.  
  128. while fuel >= 100 and sapling == true and not full do
  129.     success, item = turtle.inspect()
  130.     if fuel >= 100 then
  131.         if item ~= nil and checkList(logNames, item.name) then
  132.             cutTree(logNames)
  133.             turtle.suckUp()
  134.             turtle.suck()
  135.             if placeSapling(saplingNames) == "out" then
  136.                 break
  137.             end
  138.             for i = 1, 4 do
  139.                 guessAndCheckSome()
  140.                 turtle.turnRight()
  141.             end
  142.         end
  143.         fuel = turtle.getFuelLevel()
  144.     end
  145.     turtle.turnLeft()
  146.     left = left + 1
  147.  
  148.     if left == 3 then
  149.         turtle.turnLeft()
  150.         left = 0
  151.     end
  152. end
  153. findStartPosition()
  154.  
  155. if fuel <= 100 then
  156.     print("Stopping: Fuel Level Low")
  157. elseif full == true then
  158.     print("Stopping: Chest Full")
  159. else
  160.     print("Stopping: Out of Saplings")
  161. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement