Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Wrap peripherals
- local modem = peripheral.wrap("back") -- Adjust if needed
- local inputChest = peripheral.wrap("minecraft:chest_9") -- Your input chest
- local storageChests = {
- peripheral.wrap("minecraft:chest_5"),
- peripheral.wrap("minecraft:chest_6"),
- peripheral.wrap("minecraft:chest_7"),
- peripheral.wrap("minecraft:chest_8")
- }
- -- Print guide for commands
- local function printGuide()
- print("Commands:")
- print(" str - Prints this guide.")
- print(" str -i - Inserts input chest's contents into the system.")
- print(" str -e [string] - Extracts and lists all items with a name containing [string].")
- end
- function split(input, delimiter)
- local result = {}
- local start = 1
- local delimiterStart, delimiterEnd = string.find(input, delimiter, start)
- while delimiterStart do
- table.insert(result, string.sub(input, start, delimiterStart - 1))
- start = delimiterEnd + 1
- delimiterStart, delimiterEnd = string.find(input, delimiter, start)
- end
- table.insert(result, string.sub(input, start))
- return result
- end
- -- Insert items from input chest to storage chests
- local function insertItems()
- local items = inputChest.list()
- for slot, item in pairs(items) do
- local remaining = item.count
- for _, chest in pairs(storageChests) do
- if remaining <= 0 then break end
- local freeSpace = chest.getFreeSpace(item.name)
- local moveCount = math.min(remaining, freeSpace)
- if moveCount > 0 then
- inputChest.pushItems(chest.getName(), slot, moveCount)
- remaining = remaining - moveCount
- end
- end
- end
- print("Items inserted into the system.")
- end
- -- Extract and list items by name
- local function extractItems(searchString)
- local results = {}
- for _, chest in pairs(storageChests) do
- local items = chest.list()
- for slot, item in pairs(items) do
- if string.find(item.name, searchString) then
- table.insert(results, {name = item.name, count = item.count})
- end
- end
- end
- if #results == 0 then
- print("No items found containing: " .. searchString)
- else
- print("Items containing '" .. searchString .. "':")
- for _, result in pairs(results) do
- print("Item: " .. result.name .. " Count: " .. result.count)
- end
- end
- end
- -- Main command handler
- local function handleCommand(command, ...)
- if command == "str" then
- printGuide()
- elseif command == "str -i" then
- insertItems()
- elseif command == "str -e" then
- local searchString = table.concat({...}, " ")
- extractItems(searchString)
- else
- print("Unknown command. Use 'str' for help.")
- end
- end
- -- Main loop
- while true do
- print("Enter command:")
- local input = read()
- local args = split(input, " ")
- local command = args[1]
- table.remove(args, 1)
- handleCommand(command, table.unpack(args))
- end
- -- Utility function to split a string into a table of words
- function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t = {}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement