Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- How to use
- -- Put a barrel connected with a wired modem, and cable
- -- Connect VANILLA chests to the system with cables and wired modems
- -- Have Fun! Script by Meletion on YT <3
- -- https://pastebin.com/Fv8PSDQa
- string.contains = function(self, str)
- return self:find(str) ~= nil
- end
- local outputBin = peripheral.find('minecraft:barrel')
- local chests = { peripheral.find('minecraft:chest') }
- -- Function to split a string into words
- local function splitString(inputStr, sep)
- local t = {}
- for str in string.gmatch(inputStr, "([^" .. sep .. "]+)") do
- table.insert(t, str)
- end
- return t
- end
- -- Function to process item names
- local function processItemName(itemName)
- if not itemName then
- return "Unknown Item"
- end
- -- Remove everything before the first colon and the colon itself
- local name = itemName:match(":(.*)")
- if not name then
- return itemName
- end
- -- Replace underscores with spaces and capitalize each word
- local words = splitString(name, "_")
- for i, word in ipairs(words) do
- words[i] = word:sub(1, 1):upper() .. word:sub(2)
- end
- name = table.concat(words, " ")
- return name
- end
- -- Function to find the emptiest chest
- local function findEmptiestChest()
- local emptiestChest = nil
- local minItems = math.huge
- for i, chest in ipairs(chests) do
- local itemCount = 0
- for _, item in pairs(chest.list()) do
- itemCount = itemCount + 1
- end
- if itemCount < minItems then
- minItems = itemCount
- emptiestChest = chest
- end
- end
- return emptiestChest
- end
- -- Function to check if a chest is not full
- local function isChestNotFull(chest)
- local emptySlots = 0
- for slot = 1, chest.size() do
- if not chest.getItemDetail(slot) then
- emptySlots = emptySlots + 1
- end
- end
- return emptySlots > 0
- end
- -- Function to find the next available chest
- local function findNextAvailableChest(currentChest)
- for i, chest in ipairs(chests) do
- if chest ~= currentChest and isChestNotFull(chest) then
- return chest
- end
- end
- return nil
- end
- -- Function to check if the barrel is not empty
- local function isBarrelNotEmpty()
- for slot, item in pairs(outputBin.list()) do
- if item then
- return true
- end
- end
- return false
- end
- -- Function to move items from bin to the emptiest chest
- local function moveItemsFromBinToEmptyChest()
- local emptiestChest = findEmptiestChest()
- if emptiestChest == nil then
- print("No chests available.")
- return
- end
- print("Moving items from bin to Chest " .. tostring(emptiestChest) .. ":")
- for slot, item in pairs(outputBin.list()) do
- if not item.name then
- print("Warning: item in slot " .. tostring(slot) .. " has no name.")
- end
- local itemsMoved = outputBin.pushItems(peripheral.getName(emptiestChest), slot)
- print("Moved " .. itemsMoved .. " of " .. processItemName(item.name) .. " from slot " .. tostring(slot))
- -- Check if the current chest is full, and find another chest if necessary
- while itemsMoved < item.count do
- local remaining = item.count - itemsMoved
- emptiestChest = findNextAvailableChest(emptiestChest)
- if not emptiestChest then
- print("All chests are full.")
- return
- end
- itemsMoved = itemsMoved + outputBin.pushItems(peripheral.getName(emptiestChest), slot, remaining)
- print("Moved additional " .. itemsMoved .. " of " .. processItemName(item.name) .. " to the next available chest")
- end
- end
- -- Check if the barrel is still not empty, if so, call the function again
- if isBarrelNotEmpty() then
- moveItemsFromBinToEmptyChest()
- end
- end
- -- Function to search items in chests
- local function searchItems(searchTerm)
- local foundItems = {}
- for i, chest in ipairs(chests) do
- for slot, item in pairs(chest.list()) do
- if item.name:contains(searchTerm) then
- table.insert(foundItems, { chest = chest, slot = slot, item = item })
- end
- end
- end
- return foundItems
- end
- -- Function to pick items and move to bin
- local function pickItemsToBin()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter Search Query:")
- local searchTerm = read()
- term.clear()
- term.setCursorPos(1, 1)
- print("--------- " .. processItemName(searchTerm) .. "(s) ---------")
- if searchTerm == "0" then
- print("Exiting...")
- return
- end
- local foundItems = searchItems(searchTerm)
- if #foundItems == 0 then
- print("No items found with the search term: " .. searchTerm)
- return
- end
- print("Select Item (or 0 to exit):")
- for i, entry in ipairs(foundItems) do
- print(i .. ". " .. processItemName(entry.item.name) .. " (" .. entry.item.count .. ")")
- end
- local itemIndex = tonumber(read())
- if itemIndex == 0 then
- print("Exiting...")
- return
- elseif itemIndex < 1 or itemIndex > #foundItems then
- print("Invalid selection.")
- return
- end
- local selectedItem = foundItems[itemIndex]
- print("Enter the amount to transfer (or 0 to exit):")
- local amount = tonumber(read())
- if amount == 0 then
- print("Exiting...")
- return
- elseif amount < 0 or amount > selectedItem.item.count then
- print("Invalid amount.")
- return
- end
- selectedItem.chest.pushItems(peripheral.getName(outputBin), selectedItem.slot, amount)
- print("Transferred " .. amount .. " of " .. processItemName(selectedItem.item.name) .. "(s) to the bin.")
- end
- -- Function to display options and get user input
- local function getUserOption()
- term.clear()
- term.setCursorPos(1, 1)
- print("Select an option:")
- print("1. Empty Bin.")
- print("2. Search.")
- local option = tonumber(read())
- return option
- end
- -- Main program
- while true do
- local option = getUserOption()
- if option == 1 then
- moveItemsFromBinToEmptyChest()
- elseif option == 2 then
- pickItemsToBin()
- else
- print("Invalid option selected.")
- end
- end
- shell.run("stuff.lua")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement