Advertisement
Calame

treefarm.lua

Feb 8th, 2021 (edited)
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.38 KB | None | 0 0
  1. ---------------
  2. -- TREE FARM --
  3. ---------------
  4. TreeFarm = Station:new()
  5.  
  6. -- Tree Farm
  7. TreeFarm.tree_farm_length = 15
  8.  
  9.  
  10. function TreeFarm:cut_tree()
  11.     print( "Cutting a tree." )
  12.     turtle.dig()
  13.     turtle.forward()
  14.  
  15.     local height = 0
  16.  
  17.     while turtle.is_block_tag( "up", "minecraft:logs" ) do
  18.         turtle.digUp()
  19.         turtle.force_up( "minecraft:leaves" )
  20.         height = height + 1
  21.     end
  22.  
  23.     for i = 0, height - 1 do
  24.         turtle.force_down( "minecraft:leaves" )
  25.     end
  26.  
  27.     turtle.suck()
  28.     turtle.back()
  29.     turtle.suck()
  30. end
  31.  
  32.  
  33. function TreeFarm:plant_trees()
  34.     print( "Planting trees." )
  35.  
  36.     for i = 0, TreeFarm.tree_farm_length do
  37.         turtle.suck()
  38.         turtle.force_forward( "minecraft:leaves" )
  39.         turtle.turnLeft()
  40.         TreeFarm:inspect_tree()
  41.         turtle.turn180()
  42.         TreeFarm:inspect_tree()
  43.         turtle.turnLeft()
  44.     end
  45.  
  46.     turtle.suck()
  47.     turtle.turn180()
  48.  
  49.     for i = 0, TreeFarm.tree_farm_length do
  50.         turtle.suck()
  51.         turtle.force_forward( "minecraft:leaves" )
  52.     end
  53.  
  54.     turtle.turn180()
  55. end
  56.  
  57.  
  58. function TreeFarm:inspect_tree()
  59.     local result, data = turtle.inspect()
  60.  
  61.     if result then
  62.         if data.tags[ "minecraft:logs" ] then
  63.             TreeFarm:cut_tree()
  64.         end
  65.     end
  66.  
  67.     turtle.suck()
  68.  
  69.     if TreeFarm:has_sapling() then
  70.         turtle.select( 1 )
  71.         turtle.place()
  72.     end
  73. end
  74.  
  75.  
  76. function TreeFarm:manage_furnace()
  77.     local fuel_slot = turtle.get_valid_fuel_index()
  78.     local coal_amount = turtle.getItemCount( fuel_slot )
  79.  
  80.     if coal_amount > 32 then
  81.         return
  82.     end
  83.  
  84.     print( "Checking Furnace." )
  85.     --inserting fuel
  86.     turtle.force_up()
  87.     turtle.turnRight()
  88.     turtle.select( fuel_slot )
  89.     turtle.drop( math.ceil( coal_amount / 2 ) )
  90.  
  91.     -- inserting logs
  92.     local has_log = false
  93.     for n = 2, 16 do
  94.         local nCount = turtle.getItemCount( n )
  95.        
  96.         if nCount > 0 then
  97.             turtle.select( n )
  98.  
  99.             local item = turtle.getItemDetail()
  100.  
  101.             if string.find( item.name, "log" ) then
  102.                 has_log = true
  103.                 break
  104.             end
  105.         end
  106.     end
  107.  
  108.     if has_log then
  109.         turtle.force_up()
  110.         turtle.force_forward()
  111.         turtle.dropDown()
  112.         turtle.back()
  113.         turtle.down()
  114.     end
  115.  
  116.     turtle.down()
  117.     turtle.forward()
  118.     turtle.select( 16 )
  119.     turtle.suckUp()
  120.     turtle.back()
  121.     turtle.turnLeft()
  122. end
  123.  
  124.  
  125. function TreeFarm:drop_stuff()
  126.     print( "Unloading items." )
  127.     turtle.turn180()
  128.  
  129.     for n = 2, 16 do
  130.         local nCount = turtle.getItemCount( n )
  131.        
  132.         if nCount > 0 then
  133.             local item = turtle.getItemDetail( n )
  134.  
  135.             if not turtle.is_valid_fuel( item.name ) then
  136.                 if string.find( item.name, "sapling" ) then
  137.                     turtle.turnRight()
  138.                     turtle.select( n )
  139.                     turtle.drop()
  140.                     turtle.turnLeft()
  141.                 elseif string.find( item.name, "stick" ) then
  142.                     turtle.select( n )
  143.                     turtle.refuel()
  144.                 else
  145.                     turtle.select( n )
  146.                     local can_drop = turtle.drop()
  147.  
  148.                     if not can_drop then
  149.                         print( "Make some place in the chest!" )
  150.  
  151.                         while not turtle.drop() do
  152.                             os.sleep( 30 )
  153.                         end
  154.                     end
  155.                 end
  156.             end
  157.         end
  158.     end
  159.    
  160.     turtle.turn180()
  161. end
  162.  
  163.  
  164. function TreeFarm:has_sapling()
  165.     local item = turtle.getItemDetail( 1 )
  166.     local is_sapling = item and string.find( item.name, "sapling" )
  167.     return is_sapling
  168. end
  169.  
  170.  
  171. function TreeFarm:refil_sapling()
  172.     turtle.turnLeft()
  173.     turtle.select( 1 )
  174.  
  175.     local amount = 64 - turtle.getItemCount( 1 )
  176.     turtle.suck( amount )
  177.     turtle.turnRight()
  178. end
  179.  
  180.  
  181. function TreeFarm:check_sapling()
  182.     turtle.select( 1 )
  183.     local item = turtle.getItemDetail( 1 )
  184.    
  185.     if item and not string.find( item.name, "sapling" ) then
  186.         turtle.turn180()
  187.         turtle.drop()
  188.         turtle.turn180()
  189.     end
  190.  
  191.     TreeFarm:refil_sapling()
  192. end
  193.  
  194.  
  195. function TreeFarm:has_tree_farm_setup()
  196.     turtle.turnLeft()
  197.    
  198.     local success, data = turtle.inspect()
  199.  
  200.     if success and data.name == "minecraft:chest" then
  201.         turtle.turnRight()
  202.         return true
  203.     end
  204.  
  205.     print( "Give me 2 chests, a furnace, some saplings and fuel please. Thank you!" )
  206.     return false
  207. end
  208.  
  209.  
  210. function have_setup_materials()
  211.     local has_chests = false
  212.     local has_furnace = false
  213.     local has_fuel = false
  214.     local has_sapling = false
  215.  
  216.     for i = 1, 16 do
  217.         local item = turtle.getItemDetail( i )
  218.  
  219.         if item then
  220.             if item.name == "minecraft:chest" then
  221.                 if item.count == 2 then
  222.                     has_chests = true
  223.                 end
  224.             elseif item.name == "minecraft:furnace" then
  225.                 has_furnace = true
  226.             elseif string.find( item.name, "sapling" ) then
  227.                 has_sapling = true
  228.             elseif string.find( item.name, "coal" ) then
  229.                 has_fuel = true
  230.             end
  231.         end
  232.     end
  233.  
  234.     return has_furnace and has_fuel and has_sapling and has_chests
  235. end
  236.  
  237.  
  238. function TreeFarm:setup_tree_farm()
  239.     while not have_setup_materials() do
  240.         os.sleep( 1 )
  241.     end
  242.  
  243.     turtle.select( turtle.get_item_index( "coal" ) )
  244.     turtle.transferTo( 16 )
  245.     turtle.select( turtle.get_item_index( "minecraft:chest" ) )
  246.     turtle.place()
  247.     turtle.turnLeft()
  248.     turtle.place()
  249.     turtle.select( turtle.get_item_index( "minecraft:furnace" ) )
  250.     turtle.turnLeft()
  251.     turtle.up()
  252.     turtle.place()
  253.     turtle.down()
  254.     turtle.turnLeft()
  255.     turtle.select( turtle.get_item_index( "sapling" ) )
  256.     turtle.transferTo( 1 )
  257. end
  258.  
  259.  
  260. function TreeFarm:start_tree_farm( length )
  261.     TreeFarm.tree_farm_length = length or TreeFarm.tree_farm_length
  262.  
  263.     print( "- Starting TREE FARM -" )
  264.     if not TreeFarm:has_tree_farm_setup() then
  265.         TreeFarm:setup_tree_farm()
  266.     end
  267.  
  268.     while true do
  269.         TreeFarm:check_sapling()
  270.         TreeFarm:plant_trees()
  271.         TreeFarm:manage_furnace()
  272.         TreeFarm:drop_stuff()
  273.         os.sleep( 10 )
  274.     end
  275. end
  276.  
  277. return TreeFarm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement