Advertisement
Guest User

startup.lua

a guest
Apr 11th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. rednet.open "top"
  2.  
  3. local inventories = {}
  4. for _, n in pairs(peripheral.getNames()) do
  5.     local p = peripheral.wrap(n)
  6.     if
  7.         string.find(n, "chest") or
  8.         string.find(n, "shulker") then
  9.         inventories[n] = p
  10.     end
  11. end
  12.  
  13. local nameCache = {}
  14.  
  15. function cache(item, chest, slot)
  16.     local idx = item.name .. ":" .. item.damage
  17.    
  18.     if nameCache[idx] then
  19.         return nameCache[idx]
  20.     else
  21.         nameCache[idx] = chest.getItemMeta(slot).displayName
  22.     end
  23. end
  24.  
  25. local index = {}
  26. function updateIndexFor(name)
  27.     local inv = inventories[name]
  28.     local data = inv.list()
  29.    
  30.     for slot, item in pairs(data) do
  31.         data[slot].displayName = cache(item, inv, slot)
  32.         os.queueEvent "dummy"
  33.         os.pullEvent "dummy"
  34.     end
  35.    
  36.     index[name] = data
  37. end
  38.  
  39. function updateIndex()
  40.     while true do
  41.         for n in pairs(inventories) do
  42.             updateIndexFor(n)
  43.             sleep(0.1)
  44.         end
  45.     end
  46. end
  47.  
  48. function find(predicate)
  49.     for name, items in pairs(index) do
  50.         for slot, item in pairs(items) do
  51.             if predicate(item) then
  52.                 return name, slot, item
  53.             end
  54.         end
  55.     end
  56. end
  57.  
  58. function findSpace()
  59.     for name, items in pairs(index) do
  60.         if #items < inventories[name].size() then
  61.             return name
  62.         end
  63.     end
  64. end
  65.  
  66. function processRequest(msg)
  67.     print(textutils.serialise(msg))
  68.  
  69.     if msg.cmd == "find" then
  70.         return {find(function(item)
  71.             return
  72.                 (not msg.meta or item.damage == msg.meta) and
  73.                 (not msg.name or item.name == msg.name) and
  74.                 (not msg.dname or string.find(item.displayName, msg.dname))
  75.         end)}
  76.     elseif msg.cmd == "space" then
  77.         return findSpace()
  78.     elseif msg.cmd == "list" then
  79.         return index
  80.     elseif msg.cmd == "name" then
  81.         msg.meta = msg.meta or 0
  82.         return msg.name and msg.meta and nameCache[msg.name .. ":" .. msg.meta]
  83.     end
  84. end
  85.  
  86. function processRequests()
  87.     while true do
  88.         local id, msg = rednet.receive "mw"
  89.         if msg and msg.cmd then
  90.             local ok, r = pcall(processRequest, msg)
  91.             print(textutils.serialise(r))
  92.             rednet.send(id, r, "mw")
  93.         end
  94.     end
  95. end
  96.  
  97. parallel.waitForAll(updateIndex, processRequests)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement