Tips:
- Most commands must be written on the SERVER 🟦 side of the game, except for commands that use CLIENT 🟧 side functionality.
- If you want to send the data after processing to the client, then use the net library. https://wiki.facepunch.com/gmod/Net_Library_Usage
- Since the addon uses net instead of umsg and rank-based access control for modifiers, we need to take this into account when designing commands, which makes them a bit more complicated to create, but greatly enhances functionality and security.
- Use wiki for help: https://wiki.facepunch.com/gmod
🟧 Client-side methods:
function chat.AddText(...) --https://wiki.facepunch.com/gmod/chat.AddText
function chat.Open(mode) --https://wiki.facepunch.com/gmod/chat.Open
function echat:Open(mode) --same as chat.Open
function echat:Close()
🟦 Server-side methods:
function echat:SendParsedMessageToPlayer(from, to, parsed_result) --send raw parsed data to player
function echat:SendMessageToPlayer(from, to, string_to_send) --send string to player
function echat:AddText(to, ...) --vararg version of SendMessageToPlayer
function PrintMessage(mtype, msg) --https://wiki.facepunch.com/gmod/Global.PrintMessage
🟧🟦 Shared methods:
function PLAYER_META:ChatPrint(text) --https://wiki.facepunch.com/gmod/Player:ChatPrint
function PLAYER_META:PrintMessage(mtype, msg) --https://wiki.facepunch.com/gmod/Player:PrintMessage
🟧🟦 Parsing methods:
function echat:ParseVararg(...)--Converts vararg to string. Supports Color, Player, String types
function echat:FinalParse(...) --Converts vararg to string like ParseVararg, but adds global modifier for message from config file (f.e. "text" will be "<clr:gray><adafontmono>12.20.10 <adafont><clr:white> text)
function echat:ParseCmd(txt, ply) --Modifiers parser. Used internally. Part of ParseText
function echat:ParseEmoji(inputText, ply) --Emoji parsed. Used internally. Part of ParseText
function echat:ParseText(txt, ply) --A main function that converts a string into a parsed data table. The second parameter is responsible for checking whether the modifier can be used for a certain player (based on the config). The second parameter is optional and if it is nil, the check will always be positive
The following functions make it easy to combine text that requires access control validation and text that does not require.
function echat:NewVariableParser() --creates new class of variable parser
function VARIABLE_PARSE_META:AddText(ply, text)
function VARIABLE_PARSE_META:AddVararg(ply, ...)
function VARIABLE_PARSE_META:AddNonParsedText(text)
function VARIABLE_PARSE_META:AddNonParsedVararg(...)
function VARIABLE_PARSE_META:Parse()
Example of using variable parser.
local parser = echat:NewVariableParser()
parser:AddVararg(nil, "<clr:green>[OOC] ", ply, color_white, ":") --no check for player (nil)
parser:AddVararg(ply, text) --protected (contains checks for modifiers by player usergroup)
local parsed_data = parser:Parse()
🟦 Server-side examples:
Coin flip command example
hook.Add( "PlayerSay", "CoinFlip", function( ply, text )
if ( string.lower( text ) ~= "/flip" ) then return end --only for /flip in chat
-- echat:FinalParse returns final string (with time and other (see config))
local text = echat:FinalParse(color_white, "Player: ", ply, color_white, " flipped the coin and got <clr:gold>" .. ( math.random( 2 ) == 1 and "heads" or "tails" ))
local parsed_data = echat:ParseText(text) --convert from string to parsed table
local from = nil --Message from server (no rank check)
-- send to all. Or you can use table: {ply, ply2, ply3, etc...} to send only for certain players
local to = player.GetAll()
--send message to player
--Args: (from: player, to: table of players, value)
echat:SendParsedMessageToPlayer(from, to, parsed_data)
return "" --prevent default message sending
end )
Own OOC command:
--example of OOC command
hook.Add( "PlayerSay", "OwnOOC", function( ply, text )
if (not string.StartsWith(string.lower( text ), "/ooc ") ) then return end --only for /ooc in chat
text = string.TrimLeft(text, "/ooc") --remove /ooc in player text
local parser = echat:NewVariableParser()
parser:AddVararg(nil, "<clr:green>[OOC] ", ply, color_white, ":") --no check for player (nil)
parser:AddVararg(ply, text) --protected (uses RBAC)
local parsed_data = parser:Parse()
local from = ply -- ply is sender
local to = player.GetAll() -- send to all. Or you can use table: {ply, ply2, ply3, etc...} to send only for certain players
--send message to player
--Args: (from: player, to: table of players, value)
echat:SendParsedMessageToPlayer(from, to, parsed_data)
return "" --prevent default message sending
end )
Own roll [min/max] command with distance check [Using distance from DarkRP config]
hook.Add( "PlayerSay", "echat.roll", function( ply, text )
if not DarkRP then return end --only on darkrp
if ( not string.StartsWith(string.lower( text ), "/roll") ) then return end --only for /roll in chat
local args = string.Explode(" ", text) -- split to args
--Calculate value
local min = 1
local max = 100
if args[2] and args[3] then
min = tonumber(args[2]) or 1
max = tonumber(args[3]) or 100
elseif args[2] then
max = tonumber(args[2]) or 100
end
local value = math.random(min, max)
local parser = echat:NewVariableParser()
parser:AddVararg(nil, "<clr:gold> Player ", ply, "<clr:gold> has rolled: <clr:white>"..value.." <clr:gold>("..min.."-"..max..")") --no check for player (nil)
local parsed_data = parser:Parse()
--Calculate recievers by distance
local ply_pos = ply:EyePos()
local max_distance = (GAMEMODE or GM).Config.talkDistance or 250 --chat distance
local max_distance_sqr = max_distance * max_distance
local all_players = player.GetAll()
local recievers = {}
for _, v in ipairs(all_players) do
-- if not IsValid(v) then continue end --skip non valid players
local distance = v:EyePos():DistToSqr(ply_pos)
if (distance <= max_distance_sqr) then
table.insert(recievers, v)
end
end
--send message to player
--Args: (from: player, recievers: table of players, value)
--from is nil because it's message from server
echat:SendParsedMessageToPlayer(nil, recievers, parsed_data)
return "" --prevent default message sending
end )