Advertisement
NB6G

CC: Tweaked Storage System

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