View difference between Paste ID: r4FTDuSK and SfcB8b55
SHOW: | | - or go back to the newest paste.
1
--[[Wheat Farming program by Al Sweigart
2
Plants and harvests wheat.
3
Assumes a field is in front and
4
to the right of the turtle,
5
with a chest behind it.]]
6
7
os.loadAPI('hare')
8
9
-- handle command line arguments
10
local cliArgs = {...}
11
local length = tonumber(cliArgs[1])
12
local width = tonumber(cliArgs[2])
13
14
-- display "usage" info
15
if length == nil or width == nil or cliArgs[1] == '?' then
16
  print('Usage: farmwheat <length> <width>')
17
  return
18
end
19
20
print('Hold Ctrl-T to stop.')
21
22
-- check that chest is there
23
if not hare.findBlock('minecraft:chest') then
24
  error('Must start next to a chest.')
25
end
26
27
-- face field
28
turtle.turnLeft()
29
turtle.turnLeft()
30
31
32
-- checkWheatCrop() harvests mature wheat
33
-- and plants seeds
34
function checkWheatCrop()
35
  local result, block = turtle.inspectDown()
36
37
  if not result then
38
    turtle.digDown() -- till the soil
39
    plantWheatSeed()
40
  elseif block ~= nil and block['name'] == 'minecraft:wheat' and block['metadata'] == 7 then
41
    -- collect wheat and replant
42
    turtle.digDown()
43
    print('Collected wheat.')
44
    plantWheatSeed()
45
  end
46
end
47
48
49
-- plantWheatSeed() attempts to plant
50
-- a wheat seed below the turtle
51
function plantWheatSeed()
52
  if not hare.selectItem('minecraft:wheat_seeds') then
53
    print('Warning: Low on seeds.')
54
  else
55
    turtle.placeDown() -- plant a seed
56
    print('Planted seed.')
57
  end
58
end
59
60
61
-- storeWheat() puts all wheat into an
62
-- adjacent chest
63
function storeWheat()
64
  -- face the chest
65
  if not hare.findBlock('minecraft:chest') then
66
    error('Could not find chest.')
67
  end
68
69
  -- store wheat in chest
70
  while hare.selectItem('minecraft:wheat') do
71
    print('Dropping off ' .. turtle.getItemCount() .. ' wheat...')
72
    if not turtle.drop() then
73
      error('Wheat chest is full!')
74
    end
75
  end
76
77
  -- face field again
78
  turtle.turnLeft()
79
  turtle.turnLeft()
80
end
81
82
83
-- begin farming
84
while true do
85
    -- check fuel
86
  if turtle.getFuelLevel() < (length * width + length + width) then
87
    error('Turtle needs more fuel!')
88
  end
89
90
  -- farm wheat
91
  print('Sweeping field...')
92
  hare.sweepField(length, width, checkWheatCrop)
93
  storeWheat()
94
95
  print('Sleeping for 10 minutes...')
96
  os.sleep(600)
97
end