2222Jonathan

JUtils

Dec 6th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. -- Print anerror message and a stack trace.
  2. function err(msg)
  3. if msg then textutils.pagedPrint(msg) end
  4. textutils.pagedPrint(debug.traceback())
  5. error()
  6. end
  7.  
  8. -- Check if item is at the selected slot, returns the detail of the item if found, else nil.
  9. function checkSelected(item)
  10. local data = turtle.getItemDetail()
  11. if data then
  12. if data.name == item then
  13. return data
  14. end
  15. end
  16. return nil
  17. end
  18.  
  19. -- Count number of occurenses of type item in the inventory and return it.
  20. function getItemCount(item)
  21. local count = 0
  22. for i=1, 16, 1 do
  23. turtle.select(i)
  24. local data = checkSelected(item)
  25. if data then
  26. count = count + data.count
  27. end
  28. end
  29. return count
  30. end
  31.  
  32. -- Count number of occurenses of type item in the inventory and check if it is the same or more than count. If it is less than count, pause and ask for refill.
  33. function hasNumItems(item, count)
  34. local c = getItemCount(item)
  35. print("Has only", c, "items of type", count, "need", count,", add items and press 'enter' when done.")
  36. while c < count do
  37. local action, key = os.pullEventRaw()
  38. if action == "key" then
  39. if key == keys.enter then
  40. c = getItemCount(item)
  41. if c >= count then
  42. print("Refilled!")
  43. return
  44. else
  45. print("That was not of type", item, ", try again and press 'enter' when done!")
  46. end
  47. end
  48. end
  49. end
  50. end
  51.  
  52. -- Check if an item exists in the inventory.
  53. function findItem(item)
  54. for i=1, 16, 1 do
  55. turtle.select(i)
  56. local data = checkSelected(item)
  57. if data then
  58. return data
  59. end
  60. end
  61. return nil
  62. end
  63.  
  64. -- Refule with fuelType if fuel level is less then minFuelLevel. If no fuel was found, pause and ask for refill.
  65. function refuel(fuelType, minFuelLevel, refuelAmount)
  66. local level = turtle.getFuelLevel()
  67. if level < minFuelLevel then
  68. local fuel = findItem(fuelType)
  69. local hasFuel = -1
  70. if turtle.refuel(refuelAmount) then hasFuel = 1 end
  71. while hasFuel <= 0 do
  72. if hasFuel == -1 then
  73. print("No fuel (coal) in inventory! Add fuel and press 'enter' key to continue.")
  74. hasFuel = 0
  75. end
  76. action, key = os.pullEventRaw()
  77. if action == "key" then
  78. if key == keys.enter then
  79. fuel = findItem(fuelType)
  80. if turtle.refuel(refuelAmount) then
  81. print("Refilled fuel!")
  82. hasFuel = 1
  83. else
  84. print("That was not fuel, try again and press 'enter' when done!")
  85. end
  86. end
  87. end
  88. end
  89. end
  90. end
  91.  
  92. -- Get the selected slot at the specified item. If no item was found, pause and ask for refill.
  93. function selectItem(item)
  94. -- Check if item is in inventory
  95. local data = checkSelected(item)
  96. if data then
  97. return data
  98. else
  99. data = findItem(item)
  100. if data then
  101. return data
  102. end
  103. end
  104.  
  105. -- Item was not found, ask for it.
  106. print(item, "was found, add", item, "and press 'enter' when done!")
  107. local hasItem = false
  108. while hasItem == false do
  109. local action, key = os.pullEventRaw()
  110. if action == "key" then
  111. if key == keys.enter then
  112. data = findItem(item)
  113. if data then
  114. hasItem = true
  115. else
  116. print("That was not ", item, ", try again and press 'enter' when done!")
  117. end
  118. end
  119. end
  120. end
  121.  
  122. return selectItem(item)
  123. end
Add Comment
Please, Sign In to add comment