135 lines
2.7 KiB
Lua
135 lines
2.7 KiB
Lua
|
local dirt = "minecraft:dirt"
|
||
|
local sapling = "minecraft:oak_sapling"
|
||
|
local meal = "minecraft:bone_meal"
|
||
|
local log = "minecraft:oak_log"
|
||
|
local apple = "minecraft:apple"
|
||
|
local stick = "minecraft:stick"
|
||
|
local items = {
|
||
|
sapling, meal, log, apple, stick
|
||
|
}
|
||
|
|
||
|
local mealCount = 16
|
||
|
|
||
|
function selectItem(item)
|
||
|
for i = 1, 16 do
|
||
|
info = turtle.getItemDetail(i)
|
||
|
if info and info.name == item then
|
||
|
turtle.select(i)
|
||
|
return i
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
Farmer = {}
|
||
|
function Farmer:new(invName)
|
||
|
local t = setmetatable({}, { __index = Farmer })
|
||
|
t.invName = invName
|
||
|
return t
|
||
|
end
|
||
|
|
||
|
function Farmer:connectInv()
|
||
|
local m = peripheral.find("modem")
|
||
|
self.mName= m.getNameLocal()
|
||
|
self.inv = peripheral.wrap(self.invName)
|
||
|
end
|
||
|
|
||
|
function Farmer:unload(item)
|
||
|
for i = 1, 16 do
|
||
|
info = turtle.getItemDetail(i)
|
||
|
if info and info.name == item then
|
||
|
-- print(item, self.mName, i)
|
||
|
self.inv.pullItems(self.mName, i)
|
||
|
sleep(0.5)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function Farmer:yoink(item, count)
|
||
|
for i, info in pairs(self.inv.list()) do
|
||
|
if info.name == item then
|
||
|
if info.count >= count then
|
||
|
self.inv.pushItems(self.mName, i, count)
|
||
|
sleep(0.5)
|
||
|
return true
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return false
|
||
|
end
|
||
|
|
||
|
function Farmer:farmTree(noMeal)
|
||
|
assert(turtle.turnRight())
|
||
|
assert(turtle.turnRight())
|
||
|
assert(turtle.forward())
|
||
|
assert(turtle.forward())
|
||
|
assert(turtle.down())
|
||
|
|
||
|
assert(selectItem(dirt))
|
||
|
assert(turtle.place())
|
||
|
assert(turtle.up())
|
||
|
assert(selectItem(sapling))
|
||
|
assert(turtle.place())
|
||
|
|
||
|
if noMeal then
|
||
|
while true do
|
||
|
local present, info = turtle.inspect()
|
||
|
assert(present)
|
||
|
if info.name == log then
|
||
|
break
|
||
|
end
|
||
|
sleep(20)
|
||
|
end
|
||
|
else
|
||
|
assert(selectItem(meal))
|
||
|
local grown = false
|
||
|
for i = 1, mealCount do
|
||
|
turtle.place()
|
||
|
sleep(1)
|
||
|
local present, info = turtle.inspect()
|
||
|
assert(present)
|
||
|
if info.name == log then
|
||
|
grown = true
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
if not grown then
|
||
|
assert(false, "failed to grow tree")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
turtle.select(1)
|
||
|
assert(turtle.dig())
|
||
|
assert(turtle.down())
|
||
|
assert(turtle.dig())
|
||
|
assert(turtle.up())
|
||
|
sleep(5)
|
||
|
assert(turtle.down())
|
||
|
for i = 1, 8 do
|
||
|
sleep(0.5)
|
||
|
turtle.suck()
|
||
|
end
|
||
|
|
||
|
assert(turtle.up())
|
||
|
assert(turtle.turnRight())
|
||
|
assert(turtle.turnRight())
|
||
|
assert(turtle.forward())
|
||
|
assert(turtle.forward())
|
||
|
sleep(0.5)
|
||
|
end
|
||
|
|
||
|
function Farmer:xchgItems()
|
||
|
self:connectInv()
|
||
|
for _, i in pairs(items) do
|
||
|
self:unload(i)
|
||
|
end
|
||
|
assert(self:yoink(sapling, 1))
|
||
|
return self:yoink(meal, mealCount)
|
||
|
end
|
||
|
|
||
|
local f = Farmer:new("ae2:interface_0")
|
||
|
while true do
|
||
|
local noMeal = not f:xchgItems()
|
||
|
f:farmTree(noMeal)
|
||
|
end
|