Skip to content

Instantly share code, notes, and snippets.

@jarrydstan
Last active June 16, 2021 02:37
Show Gist options
  • Save jarrydstan/06a37db03c308de2cadef52801cf330e to your computer and use it in GitHub Desktop.
Save jarrydstan/06a37db03c308de2cadef52801cf330e to your computer and use it in GitHub Desktop.
Woodcutting bot for Turtle in Minecraft
local SLOT_COUNT = 16
local d = "north"
function getItemIndex(itemName)
for slot = 1, SLOT_COUNT, 1 do
local item = turtle.getItemDetail(slot)
if(item ~= nil) then
if(string.match(item.name, itemName)) then
return slot
end
end
end
end
function checkFuel()
if(turtle.getFuelLevel() < 50) then
coalIndex = getItemIndex("coal")
if (coalIndex == nil) then
print("No fuel detected, please supply more fuel")
return false
else
turtle.refuel()
print("Refueled")
return true
end
end
return true
end
function woodCutUp()
print("Cutting tree...")
i = 0
while turtle.detect() do
turtle.dig()
plant()
turtle.digUp()
turtle.up()
sleep(0.5)
i = i + 1
end
woodCutDown(i)
return true
end
function woodCutDown(height)
print("Cutting second half...")
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnRight()
turtle.dig()
turtle.digDown()
for v = 1,height,1 do
turtle.down()
turtle.dig()
turtle.digDown()
plant()
sleep(0.5)
end
return true
end
function plant()
index = getItemIndex("sapling")
if(index ~= nil) then
turtle.select(index)
if turtle.place() then
print("Sapling planted...")
end
return true
end
end
function returnToGround()
print("Returning to bottom...")
while (turtle.detect() == false) do
turtle.down()
end
end
function suck()
print("Attempting to collect all saplings around...")
turtle.suck()
turtle.turnLeft()
turtle.suck()
turtle.turnLeft()
turtle.suck()
turtle.turnLeft()
turtle.suck()
turtle.turnLeft()
turtle.suckDown()
end
function flipDirection( ... )
turtle.turnLeft()
turtle.turnLeft()
end
function depositWood()
print("Turning around...")
flipDirection()
print("Depositing")
for slot = 1, SLOT_COUNT, 1 do
local item = turtle.getItemDetail(slot)
if(item ~= nil) then
if not string.match(item["name"], "sapling") and not string.match(item["name"], "coal") then
turtle.select(slot)
turtle.drop()
end
end
end
print("Deposited. Turning around...")
flipDirection()
end
function start()
while true do
if(not checkFuel()) then
print("Turtle is out of fuel, Powering Down...")
return
end
turtle.suckDown()
if turtle.inspect() ~= nil then
x, table = turtle.inspect()
if x == true then
tag = table['tags']
name = table['name']
if tag['minecraft:logs'] == true then
print("Tree detected...")
woodCutUp()
elseif string.match(name,"dirt") or string.match(name,"grass") then
print("No sapling detected, planting...")
turtle.up()
plant()
else
sleep(10)
end
else
returnToGround()
suck()
depositWood()
print("Waiting for tree to grow...")
end
end
end
end
print("Bottom reached..")
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment