This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeFarm.lua
More file actions
441 lines (349 loc) · 10.5 KB
/
Copy pathTreeFarm.lua
File metadata and controls
441 lines (349 loc) · 10.5 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
-- //AUTO-GENERATED-CODE//
local APLibPath = settings.get('APLibPath')
assert( -- check if setup was done before, if not return with an error
type(APLibPath) == 'string',
'Couldn\'t open APLib through path: '..tostring(
APLibPath
)..'; probably you haven\'t completed Lib setup via \'LIBFILE setup\' or the setup failed'
)
assert( -- check if Lib is still there, if not return with an error
fs.exists(APLibPath),
'Couldn\'t open APLib through path: '..tostring(
APLibPath
)..'; remember that if you move the Lib\'s folder you must set it up again via \'LIBFILE setup\''
)
os.loadAPI(APLibPath) -- load Lib with CraftOS's built-in feature
APLibPath = fs.getName(APLibPath)
if APLibPath:sub(#APLibPath - 3) == '.lua' then APLibPath = APLibPath:sub(1, #APLibPath - 4); end
local APLib = _ENV[APLibPath]
APLib.APLibPath = APLibPath
APLibPath = nil
-- //MAIN-PROGRAM//
-- PARAMS
-- BASIC
local chestDistance = 2
local treeHeight = 6 -- OAK TREE HEIGHT
-- ADVANCED
local saplingID = 'minecraft:sapling' -- SAPLING ID
local logID = 'minecraft:log' -- OAK BLOCK ID
local turtleFacing = '' -- LEAVE IT EMPTY IF YOU DON'T KNOW WHAT YOU'RE DOING
local refuelSlot = 1
local saplingSlot = 2
local facingSlot = 3
local dropSlots = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
turtle.select(dropSlots[1])
-- MEMO
local meConsole = APLib.Memo.new(2, 2, 38, 12, {nil, nil, colors.gray})
meConsole:editable(false)
meConsole:limits(false)
-- FUNCTIONS
local function print(string)
meConsole:setCursorPos(1, #meConsole.lines)
meConsole:print(string)
meConsole:setCursorPos(1, #meConsole.lines - 1)
meConsole:draw()
end
local function invertFacing(facing)
if facing == 'north' then
facing = 'south'
elseif facing == 'south' then
facing = 'north'
elseif facing == 'east' then
facing = 'west'
elseif facing == 'west' then
facing = 'east'
end
return facing
end
local function refuel()
if not (turtle.getFuelLevel() > 0) then
local lastSlot = turtle.getSelectedSlot()
local fuelCount = turtle.getItemCount(refuelSlot)
if fuelCount > 1 then
turtle.select(refuelSlot)
if not turtle.refuel(fuelCount - 1) then
print('Fuel Level: '..tostring(turtle.getFuelLevel()))
write('Waiting for fuel... ')
read()
print()
refuel()
end
end
turtle.select(lastSlot)
end
end
local function dropDownAll()
local lastSlot = turtle.getSelectedSlot()
print('Dropping all items in dropSlots...')
for key, value in pairs(dropSlots) do
turtle.select(value)
turtle.dropDown()
end
turtle.select(lastSlot)
print('Item dropping ended!')
end
local function treeCapitate()
local currHeight = 0
print('Harvesting tree...')
for i=1, treeHeight do
turtle.dig()
if turtle.detectUp() then
turtle.digUp()
end
refuel()
turtle.up()
currHeight = i
local blockInspect = {turtle.inspect()}
if blockInspect[1] then
if not (blockInspect[2].name == logID) then
print('Tree harvest stopped at '..currHeight)
print(' blocks from the ground!')
break
end
else
break
end
end
print('Going back down...')
for i=1, currHeight do
if turtle.detectDown() then
turtle.digDown()
end
refuel()
turtle.down()
end
print('Tree harvest ended!')
end
local function treeStock()
local lastSlot = turtle.getSelectedSlot()
local saplingSlotDetail = turtle.getItemDetail(saplingSlot)
if not (saplingSlotDetail.count > 1) then
local saplingFound = false
-- // 1 //
print('Searching for saplings in inventory...')
for key, value in pairs(dropSlots) do
local valueDetail = turtle.getItemDetail(value)
if valueDetail then
if valueDetail.name == saplingID then
turtle.select(value)
turtle.transferTo(saplingSlot)
saplingFound = true
end
end
end
if saplingFound then
turtle.select(lastSlot)
print('Sapling found!')
return
end
-- // 2 //
for i=1, chestDistance - 1 do
refuel()
turtle.back()
end
local chest = peripheral.wrap('back')
while true do
print('Searching for saplings in chest...')
for i=1, chest.size() do
local item = chest.getItem(i)
if item then
local itemMeta = item.getMetadata()
if itemMeta.name == saplingID then
chest.pushItems(turtleFacing, i)
saplingFound = true
end
end
end
if saplingFound then
turtle.select(lastSlot)
refuel()
for i=1, chestDistance - 1 do
refuel()
turtle.forward()
end
print('Sapling found!')
return
end
end
end
end
local function treePlant()
local lastSlot = turtle.getSelectedSlot()
print('Replanting tree...')
treeStock()
turtle.select(saplingSlot)
if turtle.place() then
print('Tree Planted!')
else
print('Tree not Planted.')
end
turtle.select(lastSlot)
end
local function facing()
local lastSlot = turtle.getSelectedSlot()
turtle.select(facingSlot)
if turtle.detectUp() then
print('Destroying the block')
print(' above the turtle...')
turtle.digUp()
end
print('Placing chest...')
turtle.placeUp()
print('Figuring out where')
print(' the turtle is facing...')
local chest = peripheral.wrap('top')
local chestMeta = chest.getMetadata()
turtleFacing = invertFacing(chestMeta.state.facing)
print('Turtle is facing: '..turtleFacing)
print('Destroying chest!')
turtle.digUp()
turtle.select(lastSlot)
dropDownAll()
end
-- HEADERS
local hMain = APLib.Header.new(1, {'Main Menu'})
local hTask = APLib.Header.new(3, {'Task Bar'})
-- BUTTONS
local bAutoFarm = APLib.Button.new(34, 1, 39, 1, {'Auto', nil, nil, colors.green, colors.red})
local bActions = APLib.Button.new(1, 1, 9, 1, {'Tasks', nil, nil, colors.lightGray, colors.gray})
local mbHarvest = APLib.Button.new(0, 0, 0, 0, {'Harvest', nil, nil, colors.blue, colors.green})
local mbPlant = APLib.Button.new(0, 0, 0, 0, {'Plant', nil, nil, colors.blue, colors.green})
local mbDropAll = APLib.Button.new(0, 0, 0, 0, {'DropAll', nil, nil, colors.blue, colors.green})
local mbQuit = APLib.Button.new(0, 0, 0, 0, {'Quit', nil, nil, colors.green, colors.red})
-- MENUS
local mActions = APLib.Menu.new(1, 2, 9, 5, {colors.lightGray})
mActions:set({mbHarvest, mbPlant, mbDropAll, mbQuit}, true)
-- PERCENTAGEBAR
local pbState = APLib.PercentageBar.new(4, 4, 36, 6, 0, 0, 4, {true, nil, nil, colors.green, colors.black})
-- CALLBACKS
local function lockButtons(blacklist)
local mb = {mbHarvest, mbPlant, mbDropAll}
for key, value in pairs(mb) do
if value ~= blacklist then
value.colors.notPressedButtonColor = colors.red
value:draw()
end
end
end
local function unlockButtons(blacklist)
local mb = {mbHarvest, mbPlant, mbDropAll}
for key, value in pairs(mb) do
if value ~= blacklist then
value.colors.notPressedButtonColor = colors.green
value:draw()
end
end
end
bAutoFarm:setCallback(
APLib.event.button.onPress,
function (self, event)
if self.state then
mActions:hide(self.state)
else
mActions:hide(not bActions.state)
end
bActions:hide(self.state)
end
)
bActions:setCallback(
APLib.event.button.onPress,
function (self, event)
mActions:hide(not self.state)
end
)
mbHarvest:setCallback(
APLib.event.button.onPress,
function (self, event)
lockButtons(self)
self:draw()
treeCapitate()
self.state = not self.state
unlockButtons(self)
end
)
mbPlant:setCallback(
APLib.event.button.onPress,
function (self, event)
lockButtons(self)
self:draw()
treePlant()
self.state = not self.state
unlockButtons(self)
end
)
mbDropAll:setCallback(
APLib.event.button.onPress,
function (self, event)
lockButtons(self)
self:draw()
dropDownAll()
self.state = not self.state
unlockButtons(self)
end
)
mbQuit:setCallback(
APLib.event.button.onPress,
function (self, event)
if self.state then
self:draw()
sleep(0.5)
self.state = false
self:draw()
APLib.stopLoop()
APLib.resetLoopSettings()
end
end
)
APLib.drawOnLoopEvent()
APLib.addLoopGroup('main', {mActions, hMain, hTask, bAutoFarm, bActions, pbState, meConsole})
APLib.setLoopGroup('main')
APLib.setLoopCallback(
APLib.event.loop.onInit,
function ()
facing()
meConsole.pos.x1 = 2
meConsole.pos.y1 = 8
meConsole.pos.x2 = 38
meConsole.pos.y2 = 12
APLib.bClear()
end
)
APLib.setLoopCallback(
APLib.event.loop.onClock,
function (event)
if bAutoFarm.state then
local blockInspect = {turtle.inspect()}
if blockInspect[1] then
if blockInspect[2].name == logID then
pbState:setValue(1)
pbState:draw()
treeCapitate()
pbState:setValue(2)
pbState:draw()
dropDownAll()
pbState:setValue(3)
pbState:draw()
treePlant()
pbState:setValue(4)
pbState:draw()
dropDownAll()
pbState:setValue(0)
pbState:draw()
end
end
end
end
)
APLib.setLoopCallback(
APLib.event.loop.onEvent,
function (event)
if event[1] == 'mouse_scroll' then
meConsole:setCursorPos(1, meConsole.cursor.pos.line + event[2])
end
end
)
APLib.loop()
APLib.bClear()
-- //AUTO-GENERATED-CODE//
os.unloadAPI(APLib.APLibPath)
-- //--//