ComputerCraft/Tree_Farm/farmtree.lua

52 lines
1.1 KiB
Lua

--[[Tree Farming App by Al Sweigart
Plants tree and cuts it down.]]
os.loadAPI('hare.lua') -- Load the hare module
local blockExists, item
local logCount = 0
-- Check if choptree program exists
if not fs.exists('choptree.lua') then
error('You must install choptree app first')
end
while true do
-- Check inventory for saplings
if not hare.selectItem('minecraft:spruce_sapling') then
error('Out of saplings')
end
print('Planting...')
turtle.place() -- plant sapling
-- Loop until tree is grown
while true do
blockExists, item = turtle.inspect()
if item['name'] ~= 'minecraft:spruce_log' then
print('wait 5 seconds')
os.sleep(5)
else
break -- tree has grown
end
end
hare.selectEmptySlot()
shell.run('choptree.lua') -- run choptree
-- move to and face chest
turtle.back()
turtle.turnLeft()
turtle.turnLeft()
-- put logs into chest
while hare.selectItem('minecraft:spruce_log') do
logCount = logCount + turtle.getItemCount()
print('Total logs: ' .. logCount)
turtle.drop()
end
--face planting sport
turtle.turnLeft()
turtle.turnLeft()
end