Suppenbiatch

Deep Storage Unit Monitor

Sep 22nd, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. --DSU Display
  2. function exists(path)
  3.         local file = assert(io.open(path, "r"))
  4.         if file ~= nil then
  5.                 file:close()
  6.                 return true
  7.         end
  8.        
  9.         return false
  10. end
  11.  
  12. function getTable(path)
  13.         if exists(path) then
  14.                 local file = io.open(path, "r")
  15.                 local lines = {}
  16.                 local i = 1
  17.                 local line = file:read("*l")
  18.                 while line ~= nil do
  19.                         lines[i] = line
  20.                         line = file:read("*l")
  21.                         i = i + 1
  22.                 end
  23.                 file:close()
  24.                 return lines
  25.         end
  26.         return {}
  27. end
  28.  
  29. function tablelength(T)
  30.     local count = 0
  31.     for _ in pairs(T) do count = count + 1 end
  32.     return count
  33. end
  34.  
  35. function formateNumber(n) --http://richard.warburton.it
  36.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  37.     return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
  38. end
  39.  
  40. -- Variables
  41. local dsus = {}
  42. local id = os.getComputerID()
  43. local config = getTable(id.."/config")
  44. local begin = tonumber(string.sub(config[1],string.find(config[1],":")+2))
  45. local stop = tonumber(string.sub(config[2],string.find(config[2],":")+2))
  46. local monitorvalue = tonumber(string.sub(config[3],string.find(config[3],":")+2))
  47. local monitor = peripheral.wrap("monitor_" .. monitorvalue)
  48. local x, y = monitor.getSize()
  49. local n = 1
  50.  
  51.  
  52. for i = begin, stop do
  53.     dsus[n] = peripheral.wrap("deep_storage_unit_" .. i)
  54.     n = n + 1
  55. end
  56.  
  57.  
  58. if tablelength(dsus) > y then
  59.     print("Monitor zu klein..")
  60.     sleep(5)
  61. end
  62.  
  63. monitor.clear()
  64.  
  65. while true do
  66.     local line = 1
  67.     local line2 = 1 + begin
  68.     for _ in pairs(dsus) do
  69.         if dsus[line].getStoredItems() ~= nil then
  70.             local qty = dsus[line].getStoredItems()["qty"]
  71.             local name = dsus[line].getStoredItems()["display_name"]
  72.             monitor.setCursorPos(1, line)
  73.             monitor.clearLine()
  74.             monitor.setTextColor(colors.orange)
  75.             monitor.write(formateNumber(qty) .. "x ")
  76.             monitor.setTextColor(colors.lime)
  77.             monitor.write(name .. "'s")
  78.         else
  79.             monitor.setCursorPos(1, line)
  80.             monitor.clearLine()
  81.             monitor.setTextColor(colors.red)
  82.             monitor.write("DSU ".. line2 .. " ist leer!")
  83.         end
  84.         line = line + 1
  85.         line2 = line2+ 1
  86.     end
  87.     sleep(0)
  88. end
Add Comment
Please, Sign In to add comment