Advertisement
rockbandcheeseman

Sendconsoletext Override 2.0.01

Aug 26th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.48 KB | None | 0 0
  1. -- Sendconsoletext Override 2.0.01
  2.  
  3. --[[
  4.     Documentation:
  5.    
  6.     I have overloaded sendconsoletext to allow you to send messages to players without them immediately disappearing.  This is the same function as "hprint" in previous versions of Phasor.  This version of the function works slightly differently than hprint did and includes many more features.
  7.    
  8.     First, a description of all of the functions you will be using and how they work (if any functions you see in the code aren't listed here, you shouldn't have a reason to use them; they are used by other functions to make my code shorter and more efficient):
  9.    
  10.     sendconsoletext(player, message, [time], [order], [align], [func])
  11.    
  12.         -- player: <int> Memory Id of player who will receive this message.
  13.         -- message: <string> Message the specified player will receive in their console.
  14.         -- time: <int> Amount of time a message will remain in a player's console in seconds (default = 5 seconds).
  15.         -- order: <int> Order messages are printed (lower number = higher on the screen) (default = next available lowest slot).
  16.         -- align: <string> Alignment of the text ("left" or "center") (default = "left")
  17.         -- func: <function or string> A function which should return true or false.  When false is returned, the message will stop printing regardless of how much time is left.  You may use the function's name as a string if you so choose.
  18.        
  19.         Returns:  Message object which can be used in functions within the console metatable.
  20.        
  21.         Example:
  22.        
  23.             -- This example will create a centered message that will send "Hello and welcome to Phasor 2.0!" for 10 seconds or until the player dies.  This message will print above any other message that has an order that is > 0.
  24.        
  25.             local message = sendconsoletext(0, "Hello and welcome to Phasor 2.0!", 10, 0, "center", isalive)
  26.            
  27.             function isalive(player)
  28.            
  29.                 local m_player = getplayer(player)
  30.                 if m_player then
  31.                     local objId = readdword(m_player, 0x34)
  32.                     local m_object = getobject(objId)
  33.                     if m_object then
  34.                         return true
  35.                     end
  36.                 end
  37.                
  38.                 return false
  39.             end
  40.            
  41.         Notes:
  42.            
  43.             -- The message order may not seem useful to you, but can be useful if you have a lot of different types of messages you'll be sending to the console, especially if you need to append any of them.
  44.             -- Up to 999 messages can occupy the same order.  Messages with the same order are printed in the order of which they were created.
  45.             -- I would suggest keeping a table of orders and reference the table whenever you send a message.  For example:
  46.                
  47.                 -- I will build on this example for the rest of the examples given in the following functions.
  48.                
  49.                 order = {}
  50.                 order.info = 0
  51.                 order.welcome = 1
  52.                 order.afkkick = 2
  53.                 order.claninfo = 3
  54.                 order.servertime = 4
  55.                 order.kills = 5
  56.                
  57.                 sendconsoletext(player, "Welcome to my server!", 7, order.welcome)
  58.                 sendconsoletext(player, "Visit our clan website to join!", 7, order.claninfo)
  59.                 sendconsoletext(player, "You will be kicked if you're AFK for more than 5 minutes!", 7, order.afkkick)
  60.                
  61.                 -- Would print:
  62.                     >> Welcome to my server!
  63.                     >> You will be kicked if you're AFK for more than 5 minutes!
  64.                     >> Visit our clan website to join!
  65.                    
  66.                 -- The benefit to this is if you for some reason want to print the amount of kills a player has when they get a kill, you can do this:
  67.                
  68.                 function OnPlayerKill(killer, victim, mode)
  69.                
  70.                     if mode == 4 then
  71.                         -- Check to see if the player is already receiving a message regarding their kill information.
  72.                         -- See the function 'getmessage' for information on what this line of code is doing.
  73.                         local message = getmessage(killer, order.kills)
  74.                         local m_player = getplayer(victim)
  75.                         local kills = readword(m_player, 0x9C)
  76.                         if message then
  77.                             -- Change what the message is printing to the new amount of kills the killer has (see console:append to figure out how to use this function)
  78.                             message:append("Kills: " .. kills, true)
  79.                         else
  80.                             sendconsoletext(killer, "Kills: " .. kills, 5, order.kills)
  81.                         end
  82.                     end
  83.                 end
  84.                
  85.                 -- The above is an example of how you can very easily insert messages wherever you please and change messages without worrying if you're changing the correct one or not.
  86.            
  87.     getmessage(player, order)
  88.    
  89.         -- player: <int> Memory Id of player whose message you're attempting to access.
  90.         -- order: <int> Order where this message will be found.  If this order contains a message block, returns the first message in the block (see getmessageblock for details)
  91.        
  92.         Returns:  Message object which can be used in functions within the console metatable.
  93.        
  94.         Example:
  95.            
  96.             local message = getmessage(player, order.welcome)
  97.             if message then
  98.                 -- (See console:append to know what this line of code is doing)
  99.                 message:append("Bienvenidos a mi servidor!", true)
  100.             end
  101.        
  102.         Notes:  
  103.        
  104.             -- Always check if this function returns nil before trying to use functions within the console metatable on it or you will get a script error.
  105.            
  106.     getmessages(player)
  107.    
  108.         -- player: <int> Memory Id of player whose messages you're attempting to access.
  109.        
  110.         Returns:  All of a player's message objects in a table.
  111.        
  112.         Example:
  113.        
  114.             local messages = getmessages(player)
  115.             for k,v in ipairs(messages) do
  116.                 -- (See console:pause for information on what this line of code is doing)
  117.                 v:pause(10)
  118.             end
  119.        
  120.     getmessageblock(player, order)
  121.    
  122.         -- player: <int> Memory Id of player whose message you're attempting to access.
  123.         -- order: <int> Order where this message block will be found.
  124.        
  125.         Returns:  A table of every message object within the message block of the order specified.
  126.        
  127.         Example:
  128.        
  129.             local list = {1, 2, 3, 4}
  130.             local messages = getmessageblock(player, order.info)
  131.             if messages then
  132.                 for k,v in ipairs(messages) do
  133.                     v:append(list[k], true)
  134.                 end
  135.             end
  136.        
  137.     console:getmessage()
  138.    
  139.         Returns:  The string the specified message object is sending.
  140.        
  141.         Example:
  142.        
  143.             local message = sendconsoletext(player, "Hello!", 5, order.welcome)
  144.             local str = message:getmessage()
  145.             print(str)
  146.            
  147.             >> Hello!
  148.            
  149.     console:append(new_message, [reset])
  150.    
  151.         -- new_message: <string> The new message the specified message object should be printing.
  152.         -- reset: <boolean or int> If reset == true, the message timer will be reset to its original time.  If false, the timer will not be reset.  If reset is a number, the message timer will be reset to the amount of time in seconds specified.  (default = false)
  153.        
  154.         Example:
  155.        
  156.             function OnPlayerJoin(player)
  157.            
  158.                 local jointime = os.time()
  159.                 sendconsoletext(player, "You have been in this server for " .. os.time() - jointime .. " seconds.", 5, order.servertime)
  160.                 registertimer(1000, "Timer", {player, jointime})
  161.             end
  162.            
  163.             function Timer(id, count, info)
  164.            
  165.                 local player = info[1]
  166.                 local jointime = info[2]
  167.                 local message = getmessage(player, order.servertime)
  168.                 if message then
  169.                     message:append("You have been in this server for " .. os.time() - jointime .. " seconds.", true)
  170.                 end
  171.                
  172.                 return true
  173.             end
  174.            
  175.     console:shift(order)
  176.    
  177.         -- order: <int> New order this message should occupy.
  178.        
  179.         Example:
  180.        
  181.             local message = getmessage(player, order.servertime)
  182.             if message then
  183.                 message:shift(order.welcome)
  184.             end
  185.            
  186.         Notes:
  187.        
  188.             -- Keep in mind this switches the order of the message object specified and the order specified; so every message currently in order.welcome will be printed where the order.servertime messages were printed.
  189.            
  190.     console:pause([time])
  191.    
  192.         -- time: <int> Amount of time in seconds the message will be paused (default = 5 seconds).
  193.        
  194.         Example:
  195.        
  196.             function OnServerCommand(admin, command)
  197.            
  198.                 local messages = getmessages(admin)
  199.                 for k,v in opairs(messages) do
  200.                     v:pause(10)
  201.                 end
  202.             end
  203.            
  204.         Notes:
  205.            
  206.             -- When a message is paused, it no longer prints and the message timer stops.  When the message unpauses, it is re-printed and the message timer continues.
  207.            
  208.     console:delete()
  209.    
  210.         Example:
  211.        
  212.             function OnPlayerKill(killer, victim, mode)
  213.            
  214.                 local message = getmessage(victim, order.kills)
  215.                 if message then
  216.                     message:delete()
  217.                 end
  218.             end
  219.            
  220.     -- Miscellaneous Notes:
  221.    
  222.         -- Use math.inf to specify infinity.
  223.         -- This function can be memory-intensive if abused, which will create lag.  In my experience, I can print about 10 messages to every player simultaneously without any problems, but I wouldn't go much higher than that.
  224.         -- This script includes the functions opairs and table.len; see the iter and table libraries supplied with Phasor 2.0 for information on how to use them.
  225. --]]
  226.  
  227. console = {}
  228. console.__index = console
  229. registertimer(100, "ConsoleTimer")
  230. phasor_sendconsoletext = sendconsoletext
  231. math.inf = 1 / 0
  232.  
  233. function sendconsoletext(player, message, time, order, align, func)
  234.  
  235.     console[player] = console[player] or {}
  236.    
  237.     local temp = {}
  238.     temp.player = player
  239.     temp.id = nextid(player, order)
  240.     temp.message = message or ""
  241.     temp.time = time or 5
  242.     temp.remain = temp.time
  243.     temp.align = align or "left"
  244.    
  245.     if type(func) == "function" then
  246.         temp.func = func
  247.     elseif type(func) == "string" then
  248.         temp.func = _G[func]
  249.     end
  250.    
  251.     console[player][temp.id] = temp
  252.     setmetatable(console[player][temp.id], console)
  253.     return console[player][temp.id]
  254. end
  255.  
  256. function nextid(player, order)
  257.  
  258.     if not order then
  259.         local x = 0
  260.         for k,v in pairs(console[player]) do
  261.             if k > x + 1 then
  262.                 return x + 1
  263.             end
  264.            
  265.             x = x + 1
  266.         end
  267.        
  268.         return x + 1
  269.     else
  270.         local original = order
  271.         while console[player][order] do
  272.             order = order + 0.001
  273.             if order == original + 0.999 then break end
  274.         end
  275.        
  276.         return order
  277.     end
  278. end
  279.  
  280. function getmessage(player, order)
  281.  
  282.     if console[player] then
  283.         if order then
  284.             return console[player][order]
  285.         end
  286.     end
  287. end
  288.  
  289. function getmessages(player)
  290.  
  291.     return console[player]
  292. end
  293.  
  294. function getmessageblock(player, order)
  295.  
  296.     local temp = {}
  297.    
  298.     for k,v in opairs(console[player]) do
  299.         if k >= order and k < order + 1 then
  300.             table.insert(temp, console[player][k])
  301.         end
  302.     end
  303.    
  304.     return temp
  305. end
  306.  
  307. function console:getmessage()
  308.  
  309.     return self.message
  310. end
  311.  
  312. function console:append(message, reset)
  313.  
  314.     if console[self.player] then
  315.         if console[self.player][self.id] then
  316.             if getplayer(self.player) then
  317.                 if reset then
  318.                     if reset == true then
  319.                         console[self.player][self.id].remain = console[self.player][self.id].time
  320.                     elseif tonumber(reset) then
  321.                         console[self.player][self.id].time = tonumber(reset)
  322.                         console[self.player][self.id].remain = tonumber(reset)
  323.                     end
  324.                 end
  325.                
  326.                 console[self.player][self.id].message = message or ""
  327.                 return true
  328.             end
  329.         end
  330.     end
  331. end
  332.  
  333. function console:shift(order)
  334.  
  335.     local temp = console[self.player][self.id]
  336.     console[self.player][self.id] = console[self.player][order]
  337.     console[self.player][order] = temp
  338. end
  339.  
  340. function console:pause(time)
  341.  
  342.     console[self.player][self.id].pausetime = time or 5
  343. end
  344.  
  345. function console:delete()
  346.  
  347.     console[self.player][self.id] = nil
  348. end
  349.  
  350. function ConsoleTimer(id, count)
  351.  
  352.     for i,_ in opairs(console) do
  353.         if tonumber(i) then
  354.             if getplayer(i) then
  355.                 for k,v in opairs(console[i]) do
  356.                     if console[i][k].pausetime then
  357.                         console[i][k].pausetime = console[i][k].pausetime - 0.1
  358.                         if console[i][k].pausetime <= 0 then
  359.                             console[i][k].pausetime = nil
  360.                         end
  361.                     else
  362.                         if console[i][k].func then
  363.                             if not console[i][k].func(i) then
  364.                                 console[i][k] = nil
  365.                             end
  366.                         end
  367.                        
  368.                         if console[i][k] then
  369.                             console[i][k].remain = console[i][k].remain - 0.1
  370.                             if console[i][k].remain <= 0 then
  371.                                 console[i][k] = nil
  372.                             end
  373.                         end
  374.                     end
  375.                 end
  376.                
  377.                 if table.len(console[i]) > 0 then
  378.                    
  379.                     local paused = 0
  380.                     for k,v in pairs(console[i]) do
  381.                         if console[i][k].pausetime then
  382.                             paused = paused + 1
  383.                         end
  384.                     end
  385.                    
  386.                     if paused < table.len(console[i]) then
  387.                         local str = ""
  388.                         for i = 0,30 do
  389.                             str = str .. " \n"
  390.                         end
  391.                        
  392.                         phasor_sendconsoletext(i, str)
  393.                        
  394.                         for k,v in opairs(console[i]) do
  395.                             if not console[i][k].pausetime then
  396.                                 if console[i][k].align == "right" or console[i][k].align == "center" then
  397.                                     phasor_sendconsoletext(i, consolecenter(string.sub(console[i][k].message, 1, 78)))
  398.                                 else
  399.                                     phasor_sendconsoletext(i, string.sub(console[i][k].message, 1, 78))
  400.                                 end
  401.                             end
  402.                         end
  403.                     end
  404.                 end
  405.             else
  406.                 console[i] = nil
  407.             end
  408.         end
  409.     end
  410.    
  411.     return true
  412. end
  413.  
  414. function consolecenter(text)
  415.  
  416.     if text then
  417.         local len = string.len(text)
  418.         for i = len + 1, 78 do
  419.             text = " " .. text
  420.         end
  421.        
  422.         return text
  423.     end
  424. end
  425.  
  426. function opairs(t)
  427.    
  428.     local keys = {}
  429.     for k,v in pairs(t) do
  430.         table.insert(keys, k)
  431.     end    
  432.     table.sort(keys,
  433.     function(a,b)
  434.         if type(a) == "number" and type(b) == "number" then
  435.             return a < b
  436.         end
  437.         an = string.lower(tostring(a))
  438.         bn = string.lower(tostring(b))
  439.         if an ~= bn then
  440.             return an < bn
  441.         else
  442.             return tostring(a) < tostring(b)
  443.         end
  444.     end)
  445.     local count = 1
  446.     return function()
  447.         if table.unpack(keys) then
  448.             local key = keys[count]
  449.             local value = t[key]
  450.             count = count + 1
  451.             return key,value
  452.         end
  453.     end
  454. end
  455.  
  456. function table.len(t)
  457.  
  458.     local count = 0
  459.     for k,v in pairs(t) do
  460.         count = count + 1
  461.     end
  462.    
  463.     return count
  464. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement