Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function find_item(name, count)
- for i=1, 16 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if item then
- if item.name == "minecraft:sapling" then
- if count == nil then
- return true
- end
- if count <= item.count then
- return true
- end
- end
- end
- end
- return false
- end
- function forward()
- if not turtle.forward() then
- a, b = turtle.inspect()
- if a then
- if b.name == "minecraft:leaves" then
- turtle.dig()
- turtle.forward()
- end
- end
- end
- end
- function up()
- if not turtle.up() then
- a, b = turtle.inspectUp()
- if a then
- if b.name == "minecraft:leaves" then
- turtle.digUp()
- turtle.up()
- end
- end
- end
- end
- function down()
- if not turtle.down() then
- a, b = turtle.inspectDown()
- if a then
- if b.name == "minecraft:leaves" then
- turtle.digDown()
- turtle.down()
- end
- end
- end
- end
- function harvest_trees()
- print("Harvesting trees")
- refuel()
- down()
- turtle.dig()
- forward()
- turtle.dig()
- turtle.turnLeft()
- local leftSide = false;
- a, b = turtle.inspect()
- print(b.name)
- if a then
- if b.name == "minecraft:log" then
- leftSide = true;
- end
- end
- print("leftSide: " .. tostring(leftSide))
- turtle.turnRight()
- local tree_height = 0
- while turtle.detectUp() do
- tree_height = tree_height + 1
- turtle.digUp()
- up()
- turtle.dig()
- refuel()
- end
- if leftSide then turtle.turnLeft() else turtle.turnRight() end
- turtle.dig()
- forward()
- if leftSide then turtle.turnRight() else turtle.turnLeft() end
- turtle.dig()
- for i=1, tree_height do
- refuel()
- turtle.digDown()
- down()
- turtle.dig()
- end
- if find_item("minecraft:sapling", 4) then
- print("Sapling found in inventory, replanting")
- refuel()
- up()
- turtle.placeDown()
- forward()
- turtle.placeDown()
- if leftSide then turtle.turnRight() else turtle.turnLeft() end
- forward()
- turtle.placeDown()
- if leftSide then turtle.turnRight() else turtle.turnLeft() end
- forward()
- turtle.placeDown()
- forward()
- down()
- else
- refuel()
- if leftSide then turtle.turnRight() else turtle.turnLeft() end
- forward()
- if leftSide then turtle.turnRight() else turtle.turnLeft() end
- forward()
- end
- up()
- end
- function check_grown()
- for i=1, 4 do
- turtle.turnRight()
- a, b = turtle.inspect()
- if a then
- if b.name == "minecraft:log" then
- return true
- end
- end
- end
- return false
- end
- function wait_for_grow()
- while not check_grown() do
- sleep(5)
- end
- print("Tree grown!")
- end
- function collect_saplings()
- print("Collecting saplings")
- local above = false
- refuel()
- above = log_avoidant_forward(above)
- above = log_avoidant_forward(above)
- turtle.turnRight()
- above = log_avoidant_forward(above)
- above = log_avoidant_forward(above)
- turtle.turnRight()
- for j=1, 2 do
- for i=1, 4 do
- above = log_avoidant_forward(above)
- end
- turtle.turnRight()
- above = log_avoidant_forward(above)
- turtle.turnRight()
- for i=1, 4 do
- above = log_avoidant_forward(above)
- end
- turtle.turnLeft()
- above = log_avoidant_forward(above)
- turtle.turnLeft()
- end
- for i=1, 4 do
- above = log_avoidant_forward(above)
- end
- turtle.turnLeft()
- above = log_avoidant_forward(above)
- above = log_avoidant_forward(above)
- turtle.turnLeft()
- above = log_avoidant_forward(above)
- above = log_avoidant_forward(above)
- end
- function organize_inventory()
- refuel()
- down()
- turtle.select(1)
- item = turtle.getItemDetail()
- if item ~= nil and item.name ~= "minecraft:log" then
- turtle.dropDown()
- end
- turtle.select(2)
- item = turtle.getItemDetail()
- if item ~= nil and item.name ~= "minecraft:sapling" then
- turtle.dropDown()
- end
- for i=3, 16 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if item ~= nil then
- if item.name == "minecraft:log" then
- turtle.transferTo(1)
- elseif item.name == "minecraft:sapling" then
- turtle.transferTo(2)
- end
- turtle.dropDown()
- end
- end
- up()
- end
- function log_avoidant_forward(above)
- refuel()
- turtle.suckDown()
- a, b = turtle.inspect()
- if a then
- if b.name == "minecraft:log" then
- if not above then
- up()
- end
- turtle.dig()
- forward()
- return true
- else
- turtle.dig()
- forward()
- if above then
- down()
- end
- return false
- end
- else
- forward()
- if above then
- down()
- end
- return false
- end
- end
- function refuel()
- if turtle.getFuelLevel() > 8 then
- return
- end
- for i=1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- break
- end
- end
- end
- refuel()
- if not turtle.detectDown() then
- down()
- end
- up()
- while true do
- wait_for_grow()
- while check_grown() do
- harvest_trees()
- end
- collect_saplings()
- organize_inventory()
- end
Add Comment
Please, Sign In to add comment