Advertisement
mathiaas

skyf_tree

Sep 1st, 2020 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. --minecraft:dirt
  2. --minecraft:chest
  3. --minecraft:log(meta 2)
  4. --minecraft:leaves(meta 2)
  5.  
  6. --minecraft:sapling(damage 2)
  7. --minecraft:log(damage 2)
  8. args = {...}
  9.  
  10. function getInventory()
  11.     local inventory = {}
  12.     for i=1,16 do
  13.         if (turtle.getItemDetail(i)) then
  14.             data = turtle.getItemDetail(i)
  15.             data["slot"] = i
  16.             table.insert(inventory, data)
  17.         end
  18.     end
  19.  
  20.     return inventory
  21. end
  22.  
  23. function digWalls()
  24.     for i=1,4 do
  25.         turtle.dig()
  26.         turtle.turnLeft()
  27.     end
  28. end
  29.  
  30. -- sort
  31. function sortInvent(item)
  32.     --sortInvent("minecraft:sapling")
  33.     local inventory = getInventory()
  34.  
  35.     for k,v in pairs(inventory) do
  36.         if (k ~= 1 and inventory[1].count < 64 and inventory[1].name == item) then
  37.             turtle.select(inventory[k].slot)
  38.             turtle.transferTo(inventory[1].slot)
  39.             inventory[1].count = inventory[1].count + inventory[k].count
  40.         end
  41.     end
  42. end
  43.  
  44.  
  45. -- depo
  46. -- keep first stack of saplings depo everything else
  47. function deposit()
  48.     local success, data = turtle.inspectDown()
  49.     if (not success or (success and data.name ~= "minecraft:chest")) then
  50.         return
  51.     end
  52.     sortInvent()
  53.     local inventory = getInventory()
  54.     local sapling_slot = nil
  55.  
  56.  
  57.  
  58.     for k,v in pairs(inventory) do
  59.         if (sapling_slot == nil and inventory[k].name == "minecraft:sapling") then sapling_slot = inventory[k].slot end
  60.         if (inventory[k].slot ~= sapling_slot) then
  61.             turtle.select(inventory[k].slot)
  62.             turtle.dropDown()
  63.         end
  64.     end
  65. end
  66.  
  67.  
  68. -- check space:
  69. function detect()
  70.     local success,data = turtle.inspect()
  71.     if (not success) then
  72.         -- plant if no log or sapling
  73.         plant()
  74.     elseif (success and data.name == "minecraft:log") then
  75.         felling()
  76.     end
  77. end
  78.  
  79.  
  80. -- felling:
  81. function felling()
  82.     -- dig, forward
  83.     turtle.dig()
  84.     turtle.forward()
  85.     -- loop dig up
  86.     while turtle.digUp() do
  87.         turtle.up()
  88.         -- check walls while digging up
  89.         digWalls()
  90.     end
  91.     turtle.back()
  92.     goDown()
  93.     plant()
  94. end
  95.  
  96. function goDown()
  97.     local direction = "l"
  98.     turtle.turnLeft()
  99.     while true do
  100.         local success, data = turtle.inspectDown()
  101.        
  102.         if (not success or (success and (data.name == "minecraft:log" or data.name == "minecraft:leaves"))) then
  103.             turtle.digDown()
  104.             turtle.down()
  105.             for i=1,2 do
  106.                 turtle.dig()
  107.                 if (direction == "l") then
  108.                     turtle.turnLeft()
  109.                 else
  110.                     turtle.turnRight()
  111.                 end
  112.             end
  113.             turtle.dig()
  114.             if (direction == "l") then
  115.                 direction = "r"
  116.             else
  117.                 direction = "l"
  118.             end
  119.         else
  120.             break
  121.         end
  122.     end
  123.  
  124.     if (direction == "l") then
  125.         turtle.turnRight()
  126.     else
  127.         turtle.turnLeft()
  128.     end
  129. end
  130.  
  131.  
  132.  
  133.  
  134. -- planting:
  135. function plant()
  136.     -- find sapling in invent
  137.     local inventory = getInventory()
  138.     -- select sapling
  139.     for k,v in pairs(inventory) do
  140.         if (inventory[k].name == "minecraft:sapling") then
  141.             turtle.select(inventory[k].slot)
  142.         end
  143.     end
  144.     -- plant in front
  145.     turtle.place()
  146. end
  147.  
  148.  
  149. -- traverse
  150. function traverse(distance, direction)
  151.     distance = distance - 1
  152.     while true do
  153.         -- dance if no coal
  154.         if  (turtle.getFuelLevel() < distance * 10) then
  155.             while true do
  156.                 turtle.turnRight()
  157.             end
  158.         end
  159.         for i=1,distance do
  160.             detect()
  161.             --choose turn direction based on start pos
  162.             if (direction == "r") then
  163.                 turtle.turnLeft()
  164.                 turtle.forward()
  165.                 turtle.turnRight()
  166.             else
  167.                 turtle.turnRight()
  168.                 turtle.forward()
  169.                 turtle.turnLeft()
  170.             end
  171.         end
  172.  
  173.         if (direction == "r") then
  174.             direction = "l"
  175.         else
  176.             direction = "r"
  177.         end
  178.         detect()
  179.         deposit()
  180.         sleep(20)
  181.  
  182.        
  183.     end
  184. end
  185.  
  186.  
  187. traverse(args[1],args[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement