SHOW:
|
|
- or go back to the newest paste.
1 | --riewestLIB | |
2 | --Version "1.1.1" | |
3 | --MCVersion "1.10.2" | |
4 | --Writer: Riewest (AKA Riewest14) | |
5 | ||
6 | function test() | |
7 | --slotsToKeep.chestSlot = 15 | |
8 | --slotsToKeep.fuelSlot = 16 | |
9 | --emptyInvExcept("top") | |
10 | end | |
11 | ||
12 | ||
13 | --Slots to keep | |
14 | --set with setSlots() | |
15 | slotsToKeep = {} | |
16 | Settings = {} | |
17 | ||
18 | --initializes values for how you | |
19 | --want the functions to behave | |
20 | --keepSlots: table containing the slots to keep | |
21 | -- slot naming scheme ex fuelSlot | |
22 | --Slots keys should be named as such as well | |
23 | function addSlots(keepSlots) | |
24 | if not keepSlots then | |
25 | Settings.keepSlotsActive = false | |
26 | else | |
27 | Settings.keepSlotsActive = true | |
28 | end | |
29 | if Settings.keepSlotsActive then | |
30 | for k, v in pairs(keepSlots) do | |
31 | slotsToKeep[k] = v | |
32 | end | |
33 | end | |
34 | end | |
35 | ||
36 | ||
37 | function addSettings(newSettings) | |
38 | if newSettings then | |
39 | for k, v in pairs(newSettings) do | |
40 | Settings[k] = v | |
41 | end | |
42 | end | |
43 | end | |
44 | ||
45 | --------------- | |
46 | --ITEM HANDLING | |
47 | --------------- | |
48 | ||
49 | --Places item in slot passed | |
50 | --on the side passed | |
51 | function placeSide(slot, side) | |
52 | if not slot then | |
53 | return | |
54 | end | |
55 | if not side then | |
56 | side = "front" | |
57 | end | |
58 | local oldSlot = turtle.getSelectedSlot() | |
59 | turtle.select(slot) | |
60 | if side == "front" then | |
61 | turtle.place() | |
62 | elseif side == "top" then | |
63 | turtle.placeUp() | |
64 | elseif side == "bottom" then | |
65 | turtle.placeDown() | |
66 | else | |
67 | rotate(side) | |
68 | turtle.place() | |
69 | reverseRotate(side) | |
70 | end | |
71 | turtle.select(oldSlot) | |
72 | end | |
73 | ||
74 | ||
75 | --Drops all items containing the | |
76 | --substring passed in. Keeps the | |
77 | --slots passed in. | |
78 | --side: side to drop items | |
79 | function dropItem(name, side) | |
80 | if not name then | |
81 | return | |
82 | end | |
83 | if not side then | |
84 | side = "front" | |
85 | end | |
86 | local slotsToDrop = {} | |
87 | local i = 1 | |
88 | while i <= 16 do | |
89 | found, fslot = findSlot(name, i) | |
90 | if found then | |
91 | i = fslot + 1 | |
92 | table.insert(slotsToDrop, fslot) | |
93 | else | |
94 | i = i + 1 | |
95 | end | |
96 | end | |
97 | remove = {} | |
98 | if Settings.keepSlotsActive then | |
99 | for k, v in pairs(slotsToDrop) do | |
100 | for ik, jk in pairs(slotsToKeep) do | |
101 | if v == jk then | |
102 | table.insert(remove, k) | |
103 | end | |
104 | end | |
105 | end | |
106 | end | |
107 | for k, v in pairs(remove) do | |
108 | table.remove(slotsToDrop, v) | |
109 | end | |
110 | emptyInv(slotsToDrop, side) | |
111 | end | |
112 | ||
113 | --Attempts to get fuel from a | |
114 | --inventory on side passed and fill | |
115 | --the slot passed. Works best if | |
116 | --The item is still in slot or | |
117 | --Chest only has fuel items | |
118 | function getFuel(side) | |
119 | if not slotsToKeep.fuelSlot then | |
120 | print("Set Fuel Slot") | |
121 | return | |
122 | end | |
123 | if not side then | |
124 | side = "front" | |
125 | end | |
126 | oldSlot = turtle.getSelectedSlot() | |
127 | turtle.select(slotsToKeep.fuelSlot) | |
128 | side = string.lower(side) | |
129 | rotate(side) | |
130 | toGet = turtle.getItemSpace() | |
131 | if side == "front" then | |
132 | turtle.suck(toGet) | |
133 | elseif side == "top" then | |
134 | turtle.suckUp(toGet) | |
135 | elseif side == "bottom" then | |
136 | turtle.suckDown(toGet) | |
137 | else | |
138 | turtle.suck(toGet) | |
139 | end | |
140 | reverseRotate(side) | |
141 | turtle.select(oldSlot) | |
142 | end | |
143 | ||
144 | --Tries to put all of the items from | |
145 | --the all other slots into the | |
146 | --passed slot | |
147 | function sortItem(slot) | |
148 | if not slot then | |
149 | return | |
150 | end | |
151 | oldSlot = turtle.getSelectedSlot() | |
152 | turtle.select(slot) | |
153 | if turtle.getItemCount() > 0 then | |
154 | itemDetail = turtle.getItemDetail() | |
155 | for i = 1, 16 do | |
156 | turtle.select(i) | |
157 | if not (i == slot) and (turtle.getItemCount() > 0) then | |
158 | tempDetail = turtle.getItemDetail() | |
159 | if (itemDetail.name == tempDetail.name) then | |
160 | turtle.transferTo(slot) | |
161 | end | |
162 | end | |
163 | end | |
164 | end | |
165 | turtle.select(oldSlot) | |
166 | end | |
167 | ||
168 | --Empty's the entire inventory | |
169 | -- to the passed side | |
170 | function emptyAllInv(side) | |
171 | slots = {} | |
172 | for i = 1, 16 do | |
173 | table.insert(slots, i) | |
174 | end | |
175 | emptyInv(slots, side) | |
176 | end | |
177 | ||
178 | --Empty's the slots passed in through | |
179 | --the array. | |
180 | function emptyInv(slotsToEmpty, side) | |
181 | oldSlot = turtle.getSelectedSlot() | |
182 | if not side then | |
183 | side = "back" | |
184 | end | |
185 | if not slotsToEmpty then | |
186 | count = 16 | |
187 | else | |
188 | count = table.getn(slotsToEmpty) | |
189 | end | |
190 | rotate(side) | |
191 | for i = 1, count do | |
192 | dropSide(slotsToEmpty[i], side) | |
193 | end | |
194 | reverseRotate(side) | |
195 | turtle.select(oldSlot) | |
196 | end | |
197 | ||
198 | --Empty's slots except the passed | |
199 | --in values | |
200 | function emptyInvExcept(side) | |
201 | local slotsToEmpty = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} | |
202 | --table.sort(slotsToKeep) | |
203 | --for i = table.getn(slotsToKeep), 1, -1 do | |
204 | --table.remove(slotsToEmpty, slotsToKeep[i]) | |
205 | --end | |
206 | local rem = {} | |
207 | for k, v in pairs(slotsToKeep) do | |
208 | table.insert(rem, v) | |
209 | end | |
210 | table.sort(rem) | |
211 | for i = table.getn(rem), 1, -1 do | |
212 | table.remove(slotsToEmpty, rem[i]) | |
213 | end | |
214 | emptyInv(slotsToEmpty, side) | |
215 | end | |
216 | ||
217 | --Drops all the items in slotNum | |
218 | --to the indicated side | |
219 | function dropSide(slotNum, side) | |
220 | turtle.select(slotNum) | |
221 | side = string.lower(side) | |
222 | success = false | |
223 | if side == "front" then | |
224 | success = turtle.drop() | |
225 | elseif side == "top" then | |
226 | success = turtle.dropUp() | |
227 | elseif side == "bottom" then | |
228 | success = turtle.dropDown() | |
229 | else | |
230 | success = turtle.drop() | |
231 | end | |
232 | return success | |
233 | end | |
234 | ||
235 | --Method to tell the turtle to suck | |
236 | --in front of it and the left and right | |
237 | function tSuck(distinctItems) | |
238 | for i = 1, distinctItems do | |
239 | turtle.suck() | |
240 | end | |
241 | turtle.turnLeft() | |
242 | for i = 1, distinctItems do | |
243 | turtle.suck() | |
244 | end | |
245 | turnAround() | |
246 | for i = 1, distinctItems do | |
247 | turtle.suck() | |
248 | end | |
249 | turtle.turnLeft() | |
250 | end | |
251 | ||
252 | ||
253 | --Method to refuel the turtle | |
254 | --if its fuel level is below a | |
255 | --critical amount. Fuel slot is | |
256 | --passed in, as well as threshold | |
257 | function tRefuel(thresh) | |
258 | local fSlot = nil | |
259 | if not thresh then | |
260 | thresh = 100 | |
261 | end | |
262 | if not slotsToKeep.fuelSlot then | |
263 | fSlot = 16 | |
264 | else | |
265 | fSlot = slotsToKeep.fuelSlot | |
266 | end | |
267 | success = false | |
268 | if turtle.getFuelLevel() <= thresh then | |
269 | oldSlot = turtle.getSelectedSlot() | |
270 | turtle.select(fSlot) | |
271 | success = turtle.refuel(turtle.getItemCount() - 1) | |
272 | turtle.select(oldSlot) | |
273 | end | |
274 | return success | |
275 | end | |
276 | ||
277 | ------------------ | |
278 | --MOVEMENT METHODS | |
279 | ------------------ | |
280 | ||
281 | --Just runs turtle.forward() | |
282 | --until the specified amount toMov | |
283 | --is met. Digs block if unable to move | |
284 | function tMoveF(toMov) | |
285 | if not toMov then | |
286 | toMov = 1 | |
287 | end | |
288 | for i = 1, toMov do | |
289 | while not (turtle.forward()) do | |
290 | turtle.dig() | |
291 | end | |
292 | end | |
293 | end | |
294 | ||
295 | --Runst turtle.back() toMov times | |
296 | function tMoveB(toMov) | |
297 | if not toMov then | |
298 | toMov = 1 | |
299 | end | |
300 | for i = 1, toMov do | |
301 | while not turtle.back() do | |
302 | sleep(.1) | |
303 | end | |
304 | end | |
305 | end | |
306 | ||
307 | --Runs turtle.up() toMov times | |
308 | --and digs up if unable to move up. | |
309 | function tMoveU(toMov) | |
310 | if not toMov then | |
311 | toMov = 1 | |
312 | end | |
313 | for i = 1, toMov do | |
314 | while not (turtle.up()) do | |
315 | turtle.digUp() | |
316 | end | |
317 | end | |
318 | end | |
319 | ||
320 | --Runs turtle.down() toMov times | |
321 | -- and digs down if unable to move down | |
322 | function tMoveD(toMov) | |
323 | if not toMov then | |
324 | toMov = 1 | |
325 | end | |
326 | for i = 1, toMov do | |
327 | while not (turtle.down()) do | |
328 | turtle.digDown() | |
329 | end | |
330 | end | |
331 | end | |
332 | ||
333 | --Turns the turtle 180 degrees | |
334 | function turnAround() | |
335 | turtle.turnLeft() | |
336 | turtle.turnLeft() | |
337 | end | |
338 | ||
339 | --Turns the turtle to the passed side | |
340 | function rotate(side) | |
341 | if not side then | |
342 | return | |
343 | elseif side == "back" then | |
344 | turnAround() | |
345 | elseif side == "left" then | |
346 | turtle.turnLeft() | |
347 | elseif side == "right" then | |
348 | turtle.turnRight() | |
349 | else | |
350 | end | |
351 | end | |
352 | ||
353 | --Turns the turtle opposite the | |
354 | -- passed side | |
355 | function reverseRotate(side) | |
356 | if not side then | |
357 | return | |
358 | elseif side == "back" then | |
359 | turnAround() | |
360 | elseif side == "left" then | |
361 | turtle.turnRight() | |
362 | elseif side == "right" then | |
363 | turtle.turnLeft() | |
364 | else | |
365 | end | |
366 | end | |
367 | ||
368 | -------------- | |
369 | --DATA METHODS | |
370 | -------------- | |
371 | ||
372 | --Splits string into table on the delimiter : | |
373 | function split(str) | |
374 | local fields = {} | |
375 | for word in string.gmatch(str, '([^:]+)') do | |
376 | table.insert(fields, word) | |
377 | end | |
378 | return fields | |
379 | end | |
380 | ||
381 | --removes the value passed from the | |
382 | -- table for all instances | |
383 | function removeTableVal(tab, val) | |
384 | if not tab then | |
385 | return | |
386 | end | |
387 | for i = table.getn(tab), 1, -1 do | |
388 | if tab[i] == val then | |
389 | table.remove(tab, i) | |
390 | end | |
391 | end | |
392 | return tab | |
393 | end | |
394 | ||
395 | --returns the slots that can be emptied | |
396 | function getSlotsToEmpty() | |
397 | local slotsToEmpty = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} | |
398 | for k, v in pairs(slotsToKeep) do | |
399 | slotsToEmpty = removeTableVal(slotsToEmpty, v) | |
400 | end | |
401 | return slotsToEmpty | |
402 | end | |
403 | ||
404 | ||
405 | --Displays fuel level in the upper | |
406 | --right corner of the screen | |
407 | function displayFuel() | |
408 | fuelLevel = turtle.getFuelLevel() | |
409 | oldX, oldY = term.getCursorPos() | |
410 | displayY = 1 | |
411 | displayX = 34 | |
412 | term.setCursorPos(displayX, displayY) | |
413 | print("Fuel:") | |
414 | term.setCursorPos(displayX, displayY + 1) | |
415 | print(fuelLevel) | |
416 | term.setCursorPos(oldX, oldY) | |
417 | end | |
418 | ||
419 | ||
420 | --Function to find the slot number | |
421 | --Of the item containing passed in | |
422 | --String, Return bool, slot | |
423 | function findSlot(name, startSlot) | |
424 | if not startSlot then | |
425 | startSlot = 1 | |
426 | end | |
427 | found = false | |
428 | slot = 0 | |
429 | for i = startSlot, 16 do | |
430 | if turtle.getItemCount(i) > 0 then | |
431 | itemDetail = turtle.getItemDetail(i) | |
432 | if string.match(itemDetail.name, string.lower(name)) then | |
433 | slot = i | |
434 | found = true | |
435 | break | |
436 | end | |
437 | end | |
438 | end | |
439 | return found, slot | |
440 | end | |
441 | ||
442 | ||
443 | test() |