Riewest14

riewestLIB 1.1

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