Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open "top"
- local inventories = {}
- for _, n in pairs(peripheral.getNames()) do
- local p = peripheral.wrap(n)
- if
- string.find(n, "chest") or
- string.find(n, "shulker") then
- inventories[n] = p
- end
- end
- local nameCache = {}
- function cache(item, chest, slot)
- local idx = item.name .. ":" .. item.damage
- if nameCache[idx] then
- return nameCache[idx]
- else
- nameCache[idx] = chest.getItemMeta(slot).displayName
- end
- end
- local index = {}
- function updateIndexFor(name)
- local inv = inventories[name]
- local data = inv.list()
- for slot, item in pairs(data) do
- data[slot].displayName = cache(item, inv, slot)
- os.queueEvent "dummy"
- os.pullEvent "dummy"
- end
- index[name] = data
- end
- function updateIndex()
- while true do
- for n in pairs(inventories) do
- updateIndexFor(n)
- sleep(0.1)
- end
- end
- end
- function find(predicate)
- for name, items in pairs(index) do
- for slot, item in pairs(items) do
- if predicate(item) then
- return name, slot, item
- end
- end
- end
- end
- function findSpace()
- for name, items in pairs(index) do
- if #items < inventories[name].size() then
- return name
- end
- end
- end
- function processRequest(msg)
- print(textutils.serialise(msg))
- if msg.cmd == "find" then
- return {find(function(item)
- return
- (not msg.meta or item.damage == msg.meta) and
- (not msg.name or item.name == msg.name) and
- (not msg.dname or string.find(item.displayName, msg.dname))
- end)}
- elseif msg.cmd == "space" then
- return findSpace()
- elseif msg.cmd == "list" then
- return index
- elseif msg.cmd == "name" then
- msg.meta = msg.meta or 0
- return msg.name and msg.meta and nameCache[msg.name .. ":" .. msg.meta]
- end
- end
- function processRequests()
- while true do
- local id, msg = rednet.receive "mw"
- if msg and msg.cmd then
- local ok, r = pcall(processRequest, msg)
- print(textutils.serialise(r))
- rednet.send(id, r, "mw")
- end
- end
- end
- parallel.waitForAll(updateIndex, processRequests)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement