harvest.lua

140 lines · 2.5 KB

Open raw

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/Michael-Reeves808/harvest.lua
1local TOTAL_SESSION_POTATOES = 0
2local SLOT_COUNT = 16
3
4function checkFuel()
5 turtle.select(1)
6
7 if(turtle.getFuelLevel() < 50) then
8 print("Attempting Refuel...")
9 for slot = 1, SLOT_COUNT, 1 do
10 turtle.select(slot)
11 if(turtle.refuel(1)) then
12 return true
13 end
14 end
15
16 return false
17 else
18 return true
19 end
20end
21
22function getItemIndex(itemName)
23 for slot = 1, SLOT_COUNT, 1 do
24 local item = turtle.getItemDetail(slot)
25 if(item ~= nil) then
26 if(item["name"] == itemName) then
27 return slot
28 end
29 end
30 end
31end
32
33function dropPotatoes(potatoIndex)
34 -- Drop normal potatoes
35 numPotatoes = turtle.getItemCount(potatoIndex) - 1
36
37 if numPotatoes > 0
38 then
39 turtle.select(potatoIndex)
40 turtle.dropDown(numPotatoes)
41 end
42
43 -- Drop poisin potatoes
44 potatoIndex = getItemIndex("minecraft:poisonous_potato")
45 if potatoIndex ~= nil
46 then
47 turtle.select(potatoIndex)
48 turtle.dropDown()
49 end
50
51end
52
53function harvestRow()
54 isBlock, data = turtle.inspect()
55
56 if(isBlock)
57 then
58 if (data['state']['age'] == 7)
59 then
60 potatoIndex = getItemIndex("minecraft:potato")
61 turtle.select(potatoIndex)
62 turtle.dig()
63 turtle.place()
64 succ()
65
66 dropPotatoes(potatoIndex)
67 TOTAL_SESSION_POTATOES = TOTAL_SESSION_POTATOES + numPotatoes
68 end
69 else
70 succ()
71 potatoIndex = getItemIndex("minecraft:potato")
72 turtle.select(potatoIndex)
73 canPlace = turtle.place()
74
75 if(not canPlace)
76 then
77 turtle.turnLeft()
78 end
79 end
80
81 succ()
82end
83
84function isGrown()
85 isBlock, data = turtle.inspect()
86
87 if(isBlock)
88 then
89 if (data['state']['age'] == 7)
90 then
91 return true
92 end
93 end
94 return false
95end
96
97function waitForGrowth()
98 while(not isGrown())
99 do
100 sleep(5)
101 end
102end
103
104function checkLeft()
105 turtle.turnLeft()
106 if (turtle.detect())
107 then
108 return true
109 end
110 turtle.forward()
111 turtle.turnRight()
112 return false
113end
114
115
116function succ()
117 for i = 1, 6, 1
118 do
119 turtle.suck()
120 end
121end
122
123
124print('Beginning Harvest...')
125
126while(1)
127do
128 harvestRow()
129
130 if (checkLeft())
131 then
132 print(TOTAL_SESSION_POTATOES .. ' potatoes harvested')
133 turtle.turnLeft()
134 checkFuel()
135 waitForGrowth()
136 end
137end
138
139
140