sasaa86

Computercraft - treefarm

Feb 23rd, 2021 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. local bonemeal = true
  2. local bonemealSide = "right"
  3.  
  4.  
  5.  
  6. turtle.select(1)
  7. log_list = { "minecraft:birch_log", "minecraft:oak_log", "minecraft:spruce_log", "minecraft:jungle_log", "minecraft:acacia_log", "minecraft:dark_oak_log" }
  8. sapling_list = {"minecraft:birch_sapling", "minecraft:oak_sapling", "minecraft:spruce_sapling", "minecraft:jungle_sapling", "minecraft:acacia_sapling", "minecraft:dark_oak_sapling"}
  9. chest = peripheral.find("minecraft:chest")
  10. local stop = false
  11.  
  12.  
  13. function chopBlock()
  14.     succes, data = turtle.inspect()
  15.     for i=1,#log_list do
  16.         if (data.name == log_list[i]) then
  17.             if turtle.dig() then
  18.                 return true
  19.             end
  20.         end
  21.     end
  22. end
  23.  
  24. function chopBlockUp()
  25.     succes, data = turtle.inspectUp()
  26.     for i=1,#log_list do
  27.         if (data.name == log_list[i]) then
  28.             if turtle.digUp() then
  29.                 return true
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. function chopBlockUpDown()
  36.     succes, data = turtle.inspectDown()
  37.     for i=1,#log_list do
  38.         if (data.name == log_list[i]) then
  39.             if turtle.digDown() then
  40.                 return true
  41.             end
  42.         end
  43.     end
  44. end
  45.  
  46. function is_sapling(slot_number)
  47.     if slot_number == nil then
  48.         error("no slot number given") -- find out where function is called and set slot number
  49.     end
  50.     item = turtle.getItemDetail(slot_number)
  51.     if item ~= nil then
  52.         for i=1,#sapling_list do
  53.             if (item.name == sapling_list[i]) then
  54.                 -- print("is sapling")
  55.                 return true
  56.             else
  57.                 -- print("is not sapling")
  58.                 return false
  59.             end
  60.         end
  61.     else
  62.         return false
  63.     end
  64. end
  65.  
  66.  
  67. function place_sapling()
  68.     if is_sapling(1) then
  69.         if turtle.place() then
  70.             return true
  71.         end
  72.     else
  73.         return false
  74.     end
  75. end
  76.  
  77. function turn_around()
  78.     turtle.turnRight()
  79.     turtle.turnRight()
  80. end
  81.  
  82. function get_sapling()
  83.     local stop = false
  84.     if (chest ~= nil) then
  85.         if peripheral.isPresent("top") then
  86.             print("chest found on the top, not optimal!")
  87.         elseif peripheral.isPresent("front") then
  88.             print("Can't place a chest here this is where the sapling must be")
  89.         elseif peripheral.isPresent("right") then
  90.             print("chest found on the right")
  91.         elseif peripheral.isPresent("bottom") then
  92.             print("chest found on the bottom")
  93.         elseif peripheral.isPresent("back") then
  94.             print("chest found on the back")
  95.             turn_around()
  96.             for slot=2, 16 do
  97.                 turtle.select(slot)
  98.                 turtle.suck()
  99.                 item = turtle.getItemDetail(slot)
  100.                 for i=1,#sapling_list do
  101.                     if item ~= nil then
  102.                         if item.name == sapling_list[i] then
  103.                             if not turtle.transferTo(1) then
  104.                                 turtle.select(1)
  105.                                 turtle.drop()
  106.                                 turtle.select(slot)
  107.                                 turtle.transferTo(1)
  108.                             end
  109.                             stop = true
  110.                         end
  111.                     end
  112.                 end
  113.                 if stop then
  114.                     break
  115.                 end
  116.             end
  117.             for slot=2, 16 do
  118.                 turtle.select(slot)
  119.                 turtle.drop()
  120.             end
  121.             turn_around()
  122.         end
  123.     else
  124.         -- no chests found
  125.         if not is_sapling(1) then
  126.             turtle.drop()
  127.         end
  128.         while turtle.getItemCount(1) == 0 do
  129.             print("need saplings!")
  130.             -- if there's no tree find saplings
  131.             turtle.suck()
  132.             turtle.turnRight()
  133.             turtle.suck()
  134.             turtle.turnRight()
  135.             turtle.suck()
  136.             turtle.turnRight()
  137.             turtle.suck()
  138.             turtle.turnRight()
  139.             -- if there no items complain!
  140.             for slot=2, 16 do
  141.                 turtle.select(slot)
  142.                 if is_sapling(slot) then
  143.                     turtle.select(1)
  144.                     turtle.dropUp()
  145.                     turtle.select(slot)
  146.                     turtle.transferTo(1)
  147.                     break
  148.                 end
  149.             end
  150.             turtle.select(1)
  151.         end
  152.     end
  153.     turtle.select(1)
  154. end
  155.  
  156.  
  157. function is_sapling_infront()
  158.     succes, item = turtle.inspect()
  159.     if item ~= nil then
  160.         for i=1,#sapling_list do
  161.             if (item.name == sapling_list[i]) then
  162.                 return true
  163.             else
  164.                 return false
  165.             end
  166.         end
  167.     else
  168.         return false
  169.     end
  170. end
  171.  
  172.  
  173. print("computer ID: "..os.getComputerID())
  174.  
  175. while true do
  176.     if turtle.getFuelLevel() >= 20 then
  177.         if chopBlock() then
  178.             turtle.forward()
  179.             while chopBlockUp() do
  180.                 turtle.up()
  181.             end
  182.             while turtle.down() do
  183.                 -- nothing
  184.             end
  185.             turtle.back()
  186.         end
  187.         if not turtle.detect() then
  188.             if not place_sapling() then
  189.             -- turtle.drop()
  190.             get_sapling()
  191.             end
  192.         end
  193.         -- wait for tree to grow
  194.         if bonemeal then
  195.             if is_sapling_infront() then
  196.                 rs.setOutput(bonemealSide, true)
  197.                 os.sleep(0.1)
  198.                 rs.setOutput(bonemealSide, false)
  199.                 os.sleep(0.1)
  200.             end
  201.         end
  202.     else
  203.         print("Turtle fuel level low ["..turtle.getFuelLevel().."]")
  204.         os.sleep(5)
  205.     end
  206.     os.sleep(0.5)
  207. end
  208.  
Add Comment
Please, Sign In to add comment