Updated the file to support modules.lua and check fuel

The app now checks if it has enough fuel and if not, it asks for more.
Additionally it digs everythin in front of it, except for the last block.
This commit is contained in:
sascham 2020-12-20 13:31:55 +00:00
parent 58f282e0bf
commit 591b06aa19

View File

@ -1,10 +1,10 @@
--[[Tree Farming App by Al Sweigart --[[Tree Farming App by Al Sweigart
Plants tree and cuts it down.]] Plants tree and cuts it down.]]
os.loadAPI('hare.lua') -- Load the hare module os.loadAPI('modules.lua') -- Load the modules module
local NUM_OF_TREES = 4 local NUM_OF_TREES = 4
local TREE_SAPLING = 'spruce' local TREE_SAPLING = 'oak'
-- Check if choptree program exists -- Check if choptree program exists
if not fs.exists('choptree.lua') then if not fs.exists('choptree.lua') then
@ -14,11 +14,10 @@ end
while true do while true do
-- First of all, wait a few minutes -- First of all, wait a few minutes
os.sleep(300) os.sleep(300)
-- Check if turtle has enough fuel
if turtle.getFuelLevel() < (2 * (3 * NUM_OF_TREES)) then -- Sort all Items together
hare.selectItem('minecraft:' .. TREE_SAPLING .. 'log') modules.sort_items()
turtle.refuel(2)
end
-- Empty everything except for the saplings -- Empty everything except for the saplings
for i = 1, 16 do for i = 1, 16 do
@ -32,19 +31,25 @@ while true do
-- Check if we have saplings in our inventory -- Check if we have saplings in our inventory
if not hare.selectItem('minecraft:' .. TREE_SAPLING .. '_sapling') then if not modules.select_item('minecraft:' .. TREE_SAPLING .. '_sapling') then
error('Out of ' .. TREE_SAPLING .. ' saplings') error('Out of ' .. TREE_SAPLING .. ' saplings')
end end
-- Build a Loop, where we plant and cut trees -- Build a Loop, where we plant and cut trees
turtle.turnLeft()
turtle.turnLeft()
for i = 1, NUM_OF_TREES do for i = 1, NUM_OF_TREES do
turtle.back() for j = 1, 3 do
turtle.back() check_fuel()
turtle.back() if i ~= 3 * NUM_OF_TREES then
turtle.turnRight() turtle.dig()
end
turtle.forward()
end
turtle.turnLeft()
blockExists, item = turtle.inspect() blockExists, item = turtle.inspect()
if blockExists and item['name'] == 'minecraft:' .. TREE_SAPLING .. '_log' then if blockExists and item['name'] == 'minecraft:' .. TREE_SAPLING .. '_log' then
hare.selectEmtySlot() modules.selectEmptySlot()
shell.run('choptree.lua') -- Run Choptree shell.run('choptree.lua') -- Run Choptree
elseif blockExists and item['name'] ~= 'minecraft:' .. TREE_SAPLING .. '_sapling' then elseif blockExists and item['name'] ~= 'minecraft:' .. TREE_SAPLING .. '_sapling' then
print('Error! Wrong block') print('Error! Wrong block')
@ -56,16 +61,22 @@ while true do
turtle.back() turtle.back()
else else
turtle.back() turtle.back()
hare.selectItem('minecraft:' .. TREE_SAPLING .. '_sapling') modules.selectItem('minecraft:' .. TREE_SAPLING .. '_sapling')
turtle.place() turtle.place()
end end
end end
turtle.turnLeft() turtle.turnRight()
end end
-- Move forward to the chest again -- Move forward to the chest again
turtle.turnLeft()
turtle.turnLeft()
for i = 1, (3 * NUM_OF_TREES) do for i = 1, (3 * NUM_OF_TREES) do
turtle.forward() check_fuel()
if i ~= 3 * NUM_OF_TREES then
turtle.dig()
end
turtle.foward()
end end
end end