Advertisement
NB6G

Untitled

Jul 10th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. string.contains = function(self, str)
  2. return self:find(str) ~= nil
  3. end -- if contains
  4.  
  5. local outputBin = peripheral.find('minecraft:barrel')
  6. local chests = { peripheral.find('minecraft:chest') }
  7.  
  8. -- Function to split a string into words
  9. local function splitString(inputStr, sep)
  10. local t = {}
  11. for str in string.gmatch(inputStr, "([^" .. sep .. "]+)") do
  12. table.insert(t, str)
  13. end
  14. return t
  15. end
  16.  
  17. -- Function to process item names
  18. local function processItemName(itemName)
  19. if not itemName then
  20. return "Unknown Item"
  21. end
  22.  
  23. -- Remove everything before the first colon and the colon itself
  24. local name = itemName:match(":(.*)")
  25. if not name then
  26. return itemName
  27. end
  28.  
  29. -- Replace underscores with spaces and capitalize each word
  30. local words = splitString(name, "_")
  31. for i, word in ipairs(words) do
  32. words[i] = word:sub(1, 1):upper() .. word:sub(2)
  33. end
  34. name = table.concat(words, " ")
  35.  
  36. return name
  37. end
  38.  
  39. -- Function to find the emptiest chest
  40. local function findEmptiestChest()
  41. local emptiestChest = nil
  42. local minItems = math.huge
  43.  
  44. for i, chest in ipairs(chests) do
  45. local itemCount = 0
  46. for _, item in pairs(chest.list()) do
  47. itemCount = itemCount + 1
  48. end
  49. if itemCount < minItems then
  50. minItems = itemCount
  51. emptiestChest = chest
  52. end
  53. end
  54.  
  55. return emptiestChest
  56. end
  57.  
  58. -- Function to check if a chest is not full
  59. local function isChestNotFull(chest)
  60. local emptySlots = 0
  61. for slot = 1, chest.size() do
  62. if not chest.getItemDetail(slot) then
  63. emptySlots = emptySlots + 1
  64. end
  65. end
  66. return emptySlots > 0
  67. end
  68.  
  69. -- Function to find the next available chest
  70. local function findNextAvailableChest(currentChest)
  71. for i, chest in ipairs(chests) do
  72. if chest ~= currentChest and isChestNotFull(chest) then
  73. return chest
  74. end
  75. end
  76. return nil
  77. end
  78.  
  79. -- Function to check if the barrel is not empty
  80. local function isBarrelNotEmpty()
  81. for slot, item in pairs(outputBin.list()) do
  82. if item then
  83. return true
  84. end
  85. end
  86. return false
  87. end
  88.  
  89. -- Function to move items from bin to the emptiest chest
  90. local function moveItemsFromBinToEmptyChest()
  91. local emptiestChest = findEmptiestChest()
  92. if emptiestChest == nil then
  93. print("No chests available.")
  94. return
  95. end
  96.  
  97. print("Moving items from bin to Chest " .. tostring(emptiestChest) .. ":")
  98. for slot, item in pairs(outputBin.list()) do
  99. if not item.name then
  100. print("Warning: item in slot " .. tostring(slot) .. " has no name.")
  101. end
  102. local itemsMoved = outputBin.pushItems(peripheral.getName(emptiestChest), slot)
  103. print("Moved " .. itemsMoved .. " of " .. processItemName(item.name) .. " from slot " .. tostring(slot))
  104.  
  105. -- Check if the current chest is full, and find another chest if necessary
  106. while itemsMoved < item.count do
  107. local remaining = item.count - itemsMoved
  108. emptiestChest = findNextAvailableChest(emptiestChest)
  109. if not emptiestChest then
  110. print("All chests are full.")
  111. return
  112. end
  113. itemsMoved = itemsMoved + outputBin.pushItems(peripheral.getName(emptiestChest), slot, remaining)
  114. print("Moved additional " .. itemsMoved .. " of " .. processItemName(item.name) .. " to the next available chest")
  115. end
  116. end
  117.  
  118. -- Check if the barrel is still not empty, if so, call the function again
  119. if isBarrelNotEmpty() then
  120. moveItemsFromBinToEmptyChest()
  121. end
  122. end
  123.  
  124. -- Function to search items in chests
  125. local function searchItems(searchTerm)
  126. local foundItems = {}
  127. for i, chest in ipairs(chests) do
  128. for slot, item in pairs(chest.list()) do
  129. if item.name:contains(searchTerm) then
  130. table.insert(foundItems, {chest = chest, slot = slot, item = item})
  131. end
  132. end
  133. end
  134. return foundItems
  135. end
  136.  
  137. -- Function to pick items and move to bin
  138. local function pickItemsToBin()
  139. term.clear()
  140. term.setCursorPos(1, 1)
  141. print("Enter Search Query:")
  142. local searchTerm = read()
  143. term.clear()
  144. term.setCursorPos(1, 1)
  145. print("--------- " .. processItemName(searchTerm) .. "(s) ---------")
  146. if searchTerm == "0" then
  147. print("Exiting...")
  148. return
  149. end
  150.  
  151. local foundItems = searchItems(searchTerm)
  152.  
  153. if #foundItems == 0 then
  154. print("No items found with the search term: " .. searchTerm)
  155. return
  156. end
  157.  
  158. print("Select Item (or 0 to exit):")
  159. for i, entry in ipairs(foundItems) do
  160. print(i .. ". " .. processItemName(entry.item.name) .. " (" .. entry.item.count .. ")")
  161. end
  162.  
  163. local itemIndex = tonumber(read())
  164. if itemIndex == 0 then
  165. print("Exiting...")
  166. return
  167. elseif itemIndex < 1 or itemIndex > #foundItems then
  168. print("Invalid selection.")
  169. return
  170. end
  171.  
  172. local selectedItem = foundItems[itemIndex]
  173. print("Enter the amount to transfer (or 0 to exit):")
  174. local amount = tonumber(read())
  175.  
  176. if amount == 0 then
  177. print("Exiting...")
  178. return
  179. elseif amount < 0 or amount > selectedItem.item.count then
  180. print("Invalid amount.")
  181. return
  182. end
  183.  
  184. selectedItem.chest.pushItems(peripheral.getName(outputBin), selectedItem.slot, amount)
  185. print("Transferred " .. amount .. " of " .. processItemName(selectedItem.item.name) .. "(s) to the bin.")
  186. end
  187.  
  188. -- Function to display options and get user input
  189. local function getUserOption()
  190. term.clear()
  191. term.setCursorPos(1, 1)
  192. print("Select an option:")
  193. print("1. Empty Bin.")
  194. print("2. Search.")
  195.  
  196. local option = tonumber(read())
  197. return option
  198. end
  199.  
  200. -- Main program
  201. while true do
  202. local option = getUserOption()
  203.  
  204. if option == 1 then
  205. moveItemsFromBinToEmptyChest()
  206. elseif option == 2 then
  207. pickItemsToBin()
  208. else
  209. print("Invalid option selected.")
  210. end
  211. end
  212. shell.run("stuff.lua")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement