Advertisement
rockbandcheeseman

Hprint

Jun 9th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. console = {}
  2. console.__index = console
  3. registertimer(100, "ConsoleTimer")
  4. math.inf = 1 / 0
  5.  
  6. function hprint(player, text, time, align, func)
  7.  
  8.     console[player] = console[player] or {}
  9.     local id = nextindex(player)
  10.     console[player][id] = {}
  11.     console[player][id].p = player
  12.     console[player][id].id = id
  13.     console[player][id].msg = text
  14.     console[player][id].t = time or 5
  15.     console[player][id].r = time or 5
  16.     console[player][id].f = func
  17.     console[player][id].a = align or "left"
  18.    
  19.     setmetatable(console[player][id], console)
  20.     return console[player][id]
  21. end
  22.  
  23. function nextindex(player)
  24.    
  25.     local max = 0
  26.     for k,v in pairs(console[player]) do
  27.         if k > max then
  28.             max = k
  29.         end
  30.     end
  31.    
  32.     return max + 1
  33. end
  34.  
  35. function getmessages(player)
  36.  
  37.     return console[player]
  38. end
  39.  
  40. function console:append(text, time_reset)
  41.  
  42.     local player = self.p
  43.     if getplayer(player) then
  44.         local id = self.id
  45.         if console[player] then
  46.             if console[player][id] then
  47.                 console[player][id].msg = text
  48.                 if time_reset == true or time_reset == nil then
  49.                     console[player][id].r = console[player][id].t
  50.                 end
  51.             end
  52.         end
  53.     end
  54. end
  55.  
  56. function console:message()
  57.  
  58.     return self.msg
  59. end
  60.  
  61. function console:player()
  62.  
  63.     return self.p
  64. end
  65.  
  66. function console:time()
  67.  
  68.     return self.t
  69. end
  70.  
  71. function console:remaining()
  72.  
  73.     return self.r
  74. end
  75.  
  76. function console:index()
  77.  
  78.     return self.id
  79. end
  80.  
  81. function console:func()
  82.  
  83.     return self.f
  84. end
  85.  
  86. function console:alignment()
  87.  
  88.     return self.a
  89. end
  90.  
  91. function console:delete()
  92.  
  93.     console[self.p][self.id] = nil
  94. end
  95.  
  96. function console:pause(duration)
  97.  
  98.     duration = duration or 5
  99.     console[self.p][self.id].pa = duration
  100. end
  101.  
  102. function console:paused()
  103.  
  104.     return self.pa
  105. end
  106.  
  107. function ConsoleTimer(id, count)
  108.  
  109.     for player,t in opairs(console) do
  110.         if type(t) == "table" and player ~= "__index" then
  111.             for id,t2 in opairs(t) do
  112.                 if console[player][id].pa then
  113.                     console[player][id].pa = console[player][id].pa - 0.1
  114.                     if console[player][id].pa <= 0 then
  115.                         console[player][id].pa = nil
  116.                     end
  117.                 else
  118.                     console[player][id].r = console[player][id].r - 0.1
  119.                     if console[player][id].r <= 0 then
  120.                         console[player][id] = nil
  121.                     end
  122.                 end
  123.                
  124.                 if console[player][id] then
  125.                     if console[player][id].f then
  126.                         if not console[player][id].f(player) then
  127.                             console[player][id] = nil
  128.                         end
  129.                     end
  130.                 end
  131.             end
  132.         end
  133.     end
  134.    
  135.     for player,t in opairs(console) do
  136.         if type(t) == "table" and player ~= "__index" then
  137.             if getplayer(player) then
  138.                 if table.len(t) > 0 then
  139.                     local paused = 0
  140.                     for id,t2 in pairs(t) do
  141.                         if console[player][id].pa then
  142.                             paused = paused + 1
  143.                         end
  144.                     end
  145.                    
  146.                     if paused < table.len(t) then
  147.                         local str = ""
  148.                         for i = 0,30 do
  149.                             str = str .. "\n "
  150.                         end
  151.                         hprintf(str, player)
  152.                         for id,t2 in opairs(t) do
  153.                             if console[player][id].a == "left" then
  154.                                 hprintf(console[player][id].msg, player)
  155.                             elseif console[player][id].a == "right" then
  156.                                 hprintf(consolerightalign(console[player][id].msg), player)
  157.                             end
  158.                         end
  159.                     end
  160.                 end
  161.             else
  162.                 console[player] = nil
  163.             end
  164.         end
  165.     end
  166.  
  167.     return true
  168. end
  169.  
  170. -- Console text length limit = 78
  171. function consolerightalign(text)
  172.  
  173.     local len = string.len(text)
  174.     for i = len + 1, 78 do
  175.         text = " " .. text
  176.     end
  177.    
  178.     return text
  179. end
  180.  
  181. function opairs(t)
  182.    
  183.     local keys = {}
  184.     for k,v in pairs(t) do
  185.         table.insert(keys, k)
  186.     end    
  187.     table.sort(keys,
  188.     function(a,b)
  189.         if type(a) == "number" and type(b) == "number" then
  190.             return a < b
  191.         end
  192.         an = string.lower(tostring(a))
  193.         bn = string.lower(tostring(b))
  194.         if an ~= bn then
  195.             return an < bn
  196.         else
  197.             return tostring(a) < tostring(b)
  198.         end
  199.     end)
  200.     local count = 1
  201.     return function()
  202.         if unpack(keys) then
  203.             local key = keys[count]
  204.             local value = t[key]
  205.             count = count + 1
  206.             return key,value
  207.         end
  208.     end
  209. end
  210.  
  211. function table.len(t)
  212.  
  213.     local count = 0
  214.     for k,v in pairs(t) do
  215.         count = count + 1
  216.     end
  217.    
  218.     return count
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement