Advertisement
9551

Untitled

Jul 17th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.15 KB | None | 0 0
  1. local cache = {}
  2. local chest = peripheral.find(...)
  3.  
  4. -- Update the cache
  5. local function UpdateCache(chest)
  6.   local funcs = {}
  7.   local list = chest.list()
  8.  
  9.   -- Populate functions list
  10.   for i = 1, chest.size() do
  11.     if not list[i] then break end
  12.     funcs[i] = function() -- for each slot, store nbtHash and label to cache
  13.       cache[i] = {
  14.         nbtHash = list[i].nbtHash,
  15.         label = chest.getItemMeta(i).media.label -- note that this will throw an error if you insert a non-cassete to chest
  16.       }
  17.     end
  18.   end
  19.  
  20.   cache = {} -- clear the cache then update it
  21.   parallel.waitForAll(table.unpack(funcs))
  22. end
  23.  
  24. -- Check if the contents of chest are different than cached
  25. local function CheckDiff(chest)
  26.   local list = chest.list()
  27.   for i = 1, chest.size() do -- if slot[i] is not equal to cache[i] then
  28.     if list[i] and not cache[i] or cache[i] and not list[i] then return true end
  29.     if cache[i].nbtHash ~= list[i].nbtHash then
  30.       return true
  31.     end
  32.   end
  33.  
  34.   return false
  35. end
  36.  
  37. -- main loop, check for differences, update if so.
  38. while true do
  39.   if CheckDiff(chest) then
  40.     UpdateCache()
  41.   end
  42.  
  43.   os.sleep(1)
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement