ComputerCraft/Tree_Farm/farmtree.lua

80 lines
2.1 KiB
Lua
Raw Normal View History

2020-11-06 15:05:04 +00:00
--[[Tree Farming App by Al Sweigart
Plants tree and cuts it down.]]
2020-11-06 11:00:34 +00:00
2020-11-06 15:05:04 +00:00
os.loadAPI('hare.lua') -- Load the hare module
2020-11-06 11:00:34 +00:00
2020-11-06 19:36:05 +00:00
local NUM_OF_TREES = 4
local TREE_SAPLING = 'spruce'
2020-11-06 11:00:34 +00:00
local blockExists, item
local logCount = 0
2020-11-06 15:05:04 +00:00
-- Check if choptree program exists
2020-11-06 11:00:34 +00:00
if not fs.exists('choptree.lua') then
2020-11-06 19:36:05 +00:00
error('You must install choptree app first')
2020-11-06 11:00:34 +00:00
end
while true do
2020-11-06 19:36:05 +00:00
-- First, empty the whole turtle
for i = 1, 16 do
turtle.select(i)
turtle.drop()
end
2020-11-06 11:00:34 +00:00
2020-11-06 19:36:05 +00:00
-- Check if turtle has enough fuel
if turtle.getFuelLevel() < (2 * (3 * NUM_OF_TREES)) then
hare.getItemFromChest('minecraft:' .. TREE_SAPLING .. '_log')
turtle.craft()
hare.selectItem('minecraft:' .. TREE_SAPLING .. 'plan')
turtle.refuel()
end
2020-11-06 11:00:34 +00:00
2020-11-06 19:36:05 +00:00
-- Take one stack of Saplings from the Chest
if not hare.getItemFromChest('minecraft:' .. TREE_SAPLING .. '_sapling') then
error('Out of saplingurtle.craft() -- craft stone brickss')
end
-- Build a Loop, where we plant and cut trees
for i = 1, NUM_OF_TREES do
turtle.back()
turtle.back()
turtle.back()
turtle.turnRight()
blockExists, item = turtle.inspect()
if blockExists and item['name'] == 'minecraft:' .. TREE_SAPLING .. 'log' then
shell.run('choptree.lua') -- Run Choptree
elseif block Exists and item['name'] ~= 'minecraft:' .. TREE_SAPLING .. 'sapling' then
turtle.drop()
2020-11-06 11:00:34 +00:00
2020-11-06 19:36:05 +00:00
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()
2020-11-06 11:00:34 +00:00
end