Skip to content

Commit 25eb4dc

Browse files
committed
Add hook to add/override chat tags
1 parent c3225d4 commit 25eb4dc

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ By default, the chat box will only load pictures from trusted websites. You can
5252

5353
### For developers
5454

55-
You can prevent links from certain players from embedding, by using the `CanEmbedCustomChat` hook on the **clientside**.
55+
You can prevent links from certain players from embedding, by using the `CanEmbedCustomChat` hook on the **client side**:
5656

5757
```lua
58-
hook.Add( "CanEmbedCustomChat", "override_chat_embed_access", function( ply, url, urlType )
58+
hook.Add( "CanEmbedCustomChat", "chat_embed_access_example", function( ply, url, urlType )
5959
-- return false to block embeds from "url"
6060

6161
-- "urlType" will be one of these strings:
@@ -69,6 +69,23 @@ hook.Add( "CanEmbedCustomChat", "override_chat_embed_access", function( ply, url
6969
end )
7070
```
7171

72+
You can add more or override chat tags via code, using this hook on the **client side**:
73+
74+
```lua
75+
hook.Add( "OverrideCustomChatTags", "custom_tags_example", function( ply )
76+
-- A sequential table with strings, colors or anything really
77+
local parts = {
78+
color_black, "(", Color( 0, 0, 255 ), "The " .. team.GetName( ply:Team() ), color_black, ") "
79+
}
80+
81+
-- Should we keep the original custom tags that
82+
-- were added on the "[Admin] Chat Tags" menu?
83+
local keepOriginalParts = true
84+
85+
return parts, keepOriginalParts
86+
end )
87+
```
88+
7289
## Contributing
7390

7491
Before you open a pull request, please read [this](https://github.com/StyledStrike/gmod-custom-chat/blob/master/.github/pull_request_template.md).

lua/custom_chat/client/tags.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ local function CustomChat_AddCustomTags( ply, text, isTeam, isDead )
3333
if not IsValid( ply ) or not ply:IsPlayer() then return end
3434

3535
local parts = Tags:GetParts( ply )
36-
if not parts then return end
36+
local customParts, keepOriginal = hook.Run( "OverrideCustomChatTags", ply )
37+
38+
if customParts then
39+
assert( type( customParts ) == "table" and table.IsSequential( customParts ),
40+
"OverrideCustomChatTags must be return sequential table!" )
41+
42+
if not keepOriginal then
43+
parts = nil
44+
end
45+
end
46+
47+
if not parts and not customParts then return end
3748

3849
local message = {}
3950

@@ -51,10 +62,16 @@ local function CustomChat_AddCustomTags( ply, text, isTeam, isDead )
5162
Insert( "*DEAD* " )
5263
end
5364

65+
if customParts and #customParts > 0 then
66+
for _, v in ipairs( customParts ) do
67+
Insert( v )
68+
end
69+
end
70+
5471
local messageColor = Color( 255, 255, 255 )
5572

56-
if #parts > 0 then
57-
for _, v in pairs( parts ) do
73+
if parts and #parts > 0 then
74+
for _, v in ipairs( parts ) do
5875
local color = Color( v[2], v[3], v[4] )
5976

6077
if v[1] == "MESSAGE_COL" then

0 commit comments

Comments
 (0)