Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Sendconsoletext Override 2.0.01
- --[[
- Documentation:
- 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.
- 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):
- sendconsoletext(player, message, [time], [order], [align], [func])
- -- player: <int> Memory Id of player who will receive this message.
- -- message: <string> Message the specified player will receive in their console.
- -- time: <int> Amount of time a message will remain in a player's console in seconds (default = 5 seconds).
- -- order: <int> Order messages are printed (lower number = higher on the screen) (default = next available lowest slot).
- -- align: <string> Alignment of the text ("left" or "center") (default = "left")
- -- 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.
- Returns: Message object which can be used in functions within the console metatable.
- Example:
- -- 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.
- local message = sendconsoletext(0, "Hello and welcome to Phasor 2.0!", 10, 0, "center", isalive)
- function isalive(player)
- local m_player = getplayer(player)
- if m_player then
- local objId = readdword(m_player, 0x34)
- local m_object = getobject(objId)
- if m_object then
- return true
- end
- end
- return false
- end
- Notes:
- -- 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.
- -- Up to 999 messages can occupy the same order. Messages with the same order are printed in the order of which they were created.
- -- I would suggest keeping a table of orders and reference the table whenever you send a message. For example:
- -- I will build on this example for the rest of the examples given in the following functions.
- order = {}
- order.info = 0
- order.welcome = 1
- order.afkkick = 2
- order.claninfo = 3
- order.servertime = 4
- order.kills = 5
- sendconsoletext(player, "Welcome to my server!", 7, order.welcome)
- sendconsoletext(player, "Visit our clan website to join!", 7, order.claninfo)
- sendconsoletext(player, "You will be kicked if you're AFK for more than 5 minutes!", 7, order.afkkick)
- -- Would print:
- >> Welcome to my server!
- >> You will be kicked if you're AFK for more than 5 minutes!
- >> Visit our clan website to join!
- -- 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:
- function OnPlayerKill(killer, victim, mode)
- if mode == 4 then
- -- Check to see if the player is already receiving a message regarding their kill information.
- -- See the function 'getmessage' for information on what this line of code is doing.
- local message = getmessage(killer, order.kills)
- local m_player = getplayer(victim)
- local kills = readword(m_player, 0x9C)
- if message then
- -- 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)
- message:append("Kills: " .. kills, true)
- else
- sendconsoletext(killer, "Kills: " .. kills, 5, order.kills)
- end
- end
- end
- -- 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.
- getmessage(player, order)
- -- player: <int> Memory Id of player whose message you're attempting to access.
- -- 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)
- Returns: Message object which can be used in functions within the console metatable.
- Example:
- local message = getmessage(player, order.welcome)
- if message then
- -- (See console:append to know what this line of code is doing)
- message:append("Bienvenidos a mi servidor!", true)
- end
- Notes:
- -- 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.
- getmessages(player)
- -- player: <int> Memory Id of player whose messages you're attempting to access.
- Returns: All of a player's message objects in a table.
- Example:
- local messages = getmessages(player)
- for k,v in ipairs(messages) do
- -- (See console:pause for information on what this line of code is doing)
- v:pause(10)
- end
- getmessageblock(player, order)
- -- player: <int> Memory Id of player whose message you're attempting to access.
- -- order: <int> Order where this message block will be found.
- Returns: A table of every message object within the message block of the order specified.
- Example:
- local list = {1, 2, 3, 4}
- local messages = getmessageblock(player, order.info)
- if messages then
- for k,v in ipairs(messages) do
- v:append(list[k], true)
- end
- end
- console:getmessage()
- Returns: The string the specified message object is sending.
- Example:
- local message = sendconsoletext(player, "Hello!", 5, order.welcome)
- local str = message:getmessage()
- print(str)
- >> Hello!
- console:append(new_message, [reset])
- -- new_message: <string> The new message the specified message object should be printing.
- -- 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)
- Example:
- function OnPlayerJoin(player)
- local jointime = os.time()
- sendconsoletext(player, "You have been in this server for " .. os.time() - jointime .. " seconds.", 5, order.servertime)
- registertimer(1000, "Timer", {player, jointime})
- end
- function Timer(id, count, info)
- local player = info[1]
- local jointime = info[2]
- local message = getmessage(player, order.servertime)
- if message then
- message:append("You have been in this server for " .. os.time() - jointime .. " seconds.", true)
- end
- return true
- end
- console:shift(order)
- -- order: <int> New order this message should occupy.
- Example:
- local message = getmessage(player, order.servertime)
- if message then
- message:shift(order.welcome)
- end
- Notes:
- -- 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.
- console:pause([time])
- -- time: <int> Amount of time in seconds the message will be paused (default = 5 seconds).
- Example:
- function OnServerCommand(admin, command)
- local messages = getmessages(admin)
- for k,v in opairs(messages) do
- v:pause(10)
- end
- end
- Notes:
- -- 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.
- console:delete()
- Example:
- function OnPlayerKill(killer, victim, mode)
- local message = getmessage(victim, order.kills)
- if message then
- message:delete()
- end
- end
- -- Miscellaneous Notes:
- -- Use math.inf to specify infinity.
- -- 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.
- -- 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.
- --]]
- console = {}
- console.__index = console
- registertimer(100, "ConsoleTimer")
- phasor_sendconsoletext = sendconsoletext
- math.inf = 1 / 0
- function sendconsoletext(player, message, time, order, align, func)
- console[player] = console[player] or {}
- local temp = {}
- temp.player = player
- temp.id = nextid(player, order)
- temp.message = message or ""
- temp.time = time or 5
- temp.remain = temp.time
- temp.align = align or "left"
- if type(func) == "function" then
- temp.func = func
- elseif type(func) == "string" then
- temp.func = _G[func]
- end
- console[player][temp.id] = temp
- setmetatable(console[player][temp.id], console)
- return console[player][temp.id]
- end
- function nextid(player, order)
- if not order then
- local x = 0
- for k,v in pairs(console[player]) do
- if k > x + 1 then
- return x + 1
- end
- x = x + 1
- end
- return x + 1
- else
- local original = order
- while console[player][order] do
- order = order + 0.001
- if order == original + 0.999 then break end
- end
- return order
- end
- end
- function getmessage(player, order)
- if console[player] then
- if order then
- return console[player][order]
- end
- end
- end
- function getmessages(player)
- return console[player]
- end
- function getmessageblock(player, order)
- local temp = {}
- for k,v in opairs(console[player]) do
- if k >= order and k < order + 1 then
- table.insert(temp, console[player][k])
- end
- end
- return temp
- end
- function console:getmessage()
- return self.message
- end
- function console:append(message, reset)
- if console[self.player] then
- if console[self.player][self.id] then
- if getplayer(self.player) then
- if reset then
- if reset == true then
- console[self.player][self.id].remain = console[self.player][self.id].time
- elseif tonumber(reset) then
- console[self.player][self.id].time = tonumber(reset)
- console[self.player][self.id].remain = tonumber(reset)
- end
- end
- console[self.player][self.id].message = message or ""
- return true
- end
- end
- end
- end
- function console:shift(order)
- local temp = console[self.player][self.id]
- console[self.player][self.id] = console[self.player][order]
- console[self.player][order] = temp
- end
- function console:pause(time)
- console[self.player][self.id].pausetime = time or 5
- end
- function console:delete()
- console[self.player][self.id] = nil
- end
- function ConsoleTimer(id, count)
- for i,_ in opairs(console) do
- if tonumber(i) then
- if getplayer(i) then
- for k,v in opairs(console[i]) do
- if console[i][k].pausetime then
- console[i][k].pausetime = console[i][k].pausetime - 0.1
- if console[i][k].pausetime <= 0 then
- console[i][k].pausetime = nil
- end
- else
- if console[i][k].func then
- if not console[i][k].func(i) then
- console[i][k] = nil
- end
- end
- if console[i][k] then
- console[i][k].remain = console[i][k].remain - 0.1
- if console[i][k].remain <= 0 then
- console[i][k] = nil
- end
- end
- end
- end
- if table.len(console[i]) > 0 then
- local paused = 0
- for k,v in pairs(console[i]) do
- if console[i][k].pausetime then
- paused = paused + 1
- end
- end
- if paused < table.len(console[i]) then
- local str = ""
- for i = 0,30 do
- str = str .. " \n"
- end
- phasor_sendconsoletext(i, str)
- for k,v in opairs(console[i]) do
- if not console[i][k].pausetime then
- if console[i][k].align == "right" or console[i][k].align == "center" then
- phasor_sendconsoletext(i, consolecenter(string.sub(console[i][k].message, 1, 78)))
- else
- phasor_sendconsoletext(i, string.sub(console[i][k].message, 1, 78))
- end
- end
- end
- end
- end
- else
- console[i] = nil
- end
- end
- end
- return true
- end
- function consolecenter(text)
- if text then
- local len = string.len(text)
- for i = len + 1, 78 do
- text = " " .. text
- end
- return text
- end
- end
- function opairs(t)
- local keys = {}
- for k,v in pairs(t) do
- table.insert(keys, k)
- end
- table.sort(keys,
- function(a,b)
- if type(a) == "number" and type(b) == "number" then
- return a < b
- end
- an = string.lower(tostring(a))
- bn = string.lower(tostring(b))
- if an ~= bn then
- return an < bn
- else
- return tostring(a) < tostring(b)
- end
- end)
- local count = 1
- return function()
- if table.unpack(keys) then
- local key = keys[count]
- local value = t[key]
- count = count + 1
- return key,value
- end
- end
- end
- function table.len(t)
- local count = 0
- for k,v in pairs(t) do
- count = count + 1
- end
- return count
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement