Advertisement
cbotx

Drawer Monitor

Sep 17th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | Source Code | 0 0
  1. --[[
  2. Instructions:
  3. > label set [Factory Name]
  4. > pastebin get XQ0zrAwW [Program Name]
  5. > [Program Name]
  6. --]]
  7.  
  8. function findDrawer()
  9.   local compatible = {"storagedrawers:controller"} --only support controller
  10.   for i, item_id in ipairs(compatible) do
  11.     local drawer = peripheral.find(item_id)
  12.     if drawer then return drawer end
  13.   end
  14.   return nil
  15. end
  16.  
  17. function createMessageFromContainer(container)
  18.   local msg = os.getComputerLabel()
  19.   if container then
  20.     for slot, item in pairs(container.list()) do
  21.       msg = msg..("\n%s %d %d"):format(item.name, item.count, container.getItemLimit(slot))
  22.     end
  23.   end
  24.   return msg
  25. end
  26.  
  27. function main()
  28.   peripheral.find("modem", rednet.open)
  29.   while true do
  30.     while not rednet.isOpen() do
  31.       os.sleep(10)
  32.       peripheral.find("modem", rednet.open)
  33.     end
  34.     local drawer = findDrawer()
  35.     local msg = createMessageFromContainer(drawer)
  36.     rednet.broadcast(msg, "ITEM_UPDATE")
  37.     os.sleep(10)
  38.   end
  39. end
  40.  
  41. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement