-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-farm.lua
58 lines (50 loc) · 1.9 KB
/
basic-farm.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- CHANGE DEPENDING ON YOUR CROP
local SEED_NAME = nil -- "minecraft:wheat_seeds" -- Set this to nil to select any item with the word "seed" in its name
local GROWN_AGE = 7
--- Find any seed in the inventory, and return the slot number. Returns nil if no seeds are found.
---@return number|nil slot number of the seed, or nil if no seeds are found
local function find_any_seed()
for i = 1, 16 do
local item = turtle.getItemDetail(i)
if item and (SEED_NAME and item.name == SEED_NAME or not SEED_NAME and item.name:find("seed")) then
return i
end
end
return nil
end
--- Check if the inventory is full. We only need to check if the last slot contains items, as we only ever add items to the first slot available.
---@return boolean true if the inventory is full, false otherwise
local function inv_full()
return turtle.getItemCount(16) > 0
end
turtle.select(1) -- ensure we start with the first slot selected
-- Main loop: spin around and harvest crops
while true do
while inv_full() do
printError("Inventory is full! Waiting to be emptied...")
sleep(30)
end
repeat
print("Checking...")
local is_block, block = turtle.inspect()
if not is_block or block.state.age == GROWN_AGE then
print(is_block and "Harvesting..." or "Hoeing...")
-- ensure there is farmland (requires hoe!), or dig the finished crop
turtle.dig()
-- plant a seed if we have one
print("Planting...")
local seed_slot = find_any_seed()
if seed_slot then
turtle.select(seed_slot)
turtle.place()
print("Done.")
else
printError("No seeds found!")
end
end
-- Turn to the next crop
turtle.turnRight()
until is_block and block.state.age ~= GROWN_AGE -- continue spinning and digging until nothing is left to harvest, and there is a crop in front of us.
print("Waiting...")
sleep(30) -- wait a bit for the crops to grow back
end