Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tree-Farm Turtle (based on existing public script)
- local sapling_name = "sapling"
- local min_fuel = 20
- -- Detect item in inventory by name
- local function findSlot(itemName)
- for i = 1,16 do
- local d = turtle.getItemDetail(i)
- if d and string.find(d.name, itemName) then
- return i
- end
- end
- return nil
- end
- -- Fuel-check and refuel
- local function refuelIfNeeded(fuelSlot)
- turtle.select(fuelSlot)
- while turtle.getFuelLevel() < min_fuel do
- if turtle.getItemCount(fuelSlot) == 0 then
- print("Need more fuel in slot " .. fuelSlot)
- return false
- end
- turtle.refuel(1)
- end
- return true
- end
- -- Climb and harvest
- local function harvestColumn()
- -- go up harvesting
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- -- go down back to base
- while turtle.down() do end
- end
- -- Plant sapling below when possible
- local function plantBelow(saplingSlot)
- turtle.select(sapplingSlot)
- if turtle.getItemCount(sapplingSlot) > 0 and not turtle.detectDown() then
- turtle.placeDown()
- end
- end
- -- Main loop
- local saplingSlot = findSlot(sapling_name)
- local fuelSlot = findSlot("coal") or findSlot("charcoal")
- if not saplingSlot then
- error("No sapling slot found.")
- end
- if not fuelSlot then
- error("No fuel slot found.")
- end
- while true do
- if not refuelIfNeeded(fuelSlot) then
- break
- end
- -- Plant, harvest, collect, then replant
- plantBelow(saplingSlot)
- harvestColumn()
- -- Collect dropped items around (down / up / forward)
- for i = 1,16 do
- turtle.select(i)
- turtle.suck()
- turtle.suckUp()
- turtle.suckDown()
- end
- -- Drop into a chest below
- for i = 1,16 do
- turtle.select(i)
- local count = turtle.getItemCount(i)
- if count > 0 and i ~= sapplingSlot and i ~= fuelSlot then
- turtle.dropDown(count)
- end
- end
- print("Cycle complete, refueling and replanting...")
- end
Advertisement
Add Comment
Please, Sign In to add comment