Last active
September 22, 2020 02:36
-
-
Save foopis23/0be89b34da5778a45e17eb62681d56ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local width --amount of trees wide of the tree farm | |
local length --amount of tree length of the tree farm | |
local treeDistance --the distance of blocks between | |
local sleepTime --the amount of time between the turtle checking the tree farm for grown trees | |
local pos --current distance from the starting point | |
local direction --current direction turtle is facing | |
local function fuelCheck() | |
if turtle.getFuelLevel() == 0 then | |
for i=1,15,1 do | |
if turtle.getFuelLevel() > 0 then | |
break | |
end | |
turtle.select(i) | |
if turtle.getItemDetail().name ~= "minecraft:sapling" then | |
turtle.refuel(1) | |
end | |
end | |
end | |
if turtle.getFuelLevel() < 1 then | |
print("OH FUCK OH SHIT, WE OUT OF FUEL ABORT") | |
os.exit() | |
end | |
end | |
local function up() | |
fuelCheck() | |
if turtle.up() then | |
pos.y = pos.y + 1 | |
end | |
end | |
local function down() | |
fuelCheck() | |
if turtle.down() then | |
pos.y = pos.y - 1 | |
end | |
end | |
local function forward() | |
fuelCheck() | |
if turtle.forward() then | |
if direction == 0 then | |
pos.z = pos.z + 1 | |
elseif direction == 1 then | |
pos.x = pos.x + 1 | |
elseif direction == 2 then | |
pos.z = pos.z - 1 | |
elseif direction == 3 then | |
pos.x = pos.x - 1 | |
else | |
print("Oh shit what direction are we even facing???? OH FUCK OH GOD. ABORT") | |
os.exit() | |
end | |
end | |
end | |
local function left() | |
if turtle.turnLeft() then | |
direction = math.abs((direction - 1) % 4) | |
end | |
end | |
local function right() | |
if turtle.turnRight() then | |
direction = math.abs((direction + 1) % 4) | |
end | |
end | |
local function moveToNextTree() | |
local i=0 | |
while i <= treeDistance do | |
turtle.dig() | |
forward() | |
i = i + 1 | |
end | |
end | |
local function mineTree() | |
local success, data = turtle.inspectUp() | |
if success and data.name == "minecraft:log" then | |
turtle.digDown() | |
end | |
while success and data.name == "minecraft:log" do | |
turtle.digUp() | |
up() | |
success, data = turtle.inspectUp() | |
end | |
local y = pos.y | |
while pos.y > 0 do | |
down() | |
end | |
end | |
local function plantSapling() | |
local i = 1 | |
while i <= 16 do | |
turtle.select(i) | |
local itemDetails = turtle.getItemDetail() | |
if itemDetails ~= nil and itemDetails.name == "minecraft:sapling" then | |
turtle.placeDown() | |
break; | |
end | |
i = i + 1 | |
end | |
end | |
local function turnAround(turnRight) | |
moveToNextTree() | |
if turnRight then | |
right() | |
else | |
left() | |
end | |
moveToNextTree() | |
if turnRight then | |
right() | |
else | |
left() | |
end | |
return not turnRight | |
end | |
local function returnHome() | |
if pos.z == 0 then | |
left() | |
else | |
while pos.z > 0 do | |
forward() | |
end | |
right() | |
end | |
while pos.x > 0 do | |
forward() | |
end | |
right() | |
end | |
local function depositeWood() | |
while direction ~= 2 do | |
right() | |
end | |
local i = 1 | |
while i <= 16 do | |
turtle.select(i) | |
local itemDetails = turtle.getItemDetail() | |
if itemDetails ~= nil and itemDetails.name == "minecraft:log" then | |
turtle.drop() | |
end | |
i = i + 1 | |
end | |
end | |
local function restockSaplings() | |
while direction ~= 3 do | |
right() | |
end | |
local i = 1 | |
while i <= 16 do | |
turtle.select(i) | |
local itemDetails = turtle.getItemDetail() | |
if itemDetails ~= nil and itemDetails.name == "minecraft:sapling" then | |
turtle.drop() | |
end | |
i = i + 1 | |
end | |
turtle.select(1) | |
turtle.suck() | |
end | |
local function init() | |
width = 5; | |
length = 5; | |
treeDistance = 1; | |
sleepTime = 300 --Sleep Time is 5 minutes | |
pos = { | |
x=0, | |
y=0, | |
z=0, | |
} | |
direction = 0; | |
end | |
local function loop() | |
while true do | |
local turnRight = true; | |
restockSaplings() | |
while direction ~= 0 do | |
right() | |
end | |
local widDist=0 | |
while widDist < width do | |
local lenDist=0 | |
while lenDist < length do | |
moveToNextTree() | |
mineTree() | |
plantSapling() | |
lenDist = lenDist + 1 | |
end | |
turnRight = turnAround(turnRight) | |
widDist = widDist + 1 | |
end | |
returnHome() | |
depositeWood() | |
os.sleep(sleepTime) | |
end | |
end | |
init() | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment