Skip to content

Commit d9b82fb

Browse files
authored
Merge pull request #41 from wrefgtzweve/add-formatting-hook
Add formatting hook
2 parents 198bb8b + b47ea15 commit d9b82fb

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

README.md

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ A simple and customizable chat box that can format text, display images and emoj
55
[![GLuaLint](https://github.com/StyledStrike/gmod-custom-chat/actions/workflows/glualint.yml/badge.svg)](https://github.com/FPtje/GLuaFixer)
66
[![Workshop Page](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-steam-workshop.jross.me%2F2799307109%2Fsubscriptions-text)](https://steamcommunity.com/sharedfiles/filedetails/?id=2799307109)
77

8-
### Features
8+
## Table of contents
9+
10+
- [Features](#features)
11+
- [Text formatting](#text-formatting)
12+
- [Fonts](#fonts)
13+
- [Whitelisted sites](#whitelisted-sites)
14+
- [Developer reference](#developer-reference)
15+
- [Hooks (quick links)](#hooks-quick-links)
16+
- [CanEmbedCustomChat](#canembedcustomchat)
17+
- [CanFormatCustomChat](#canformatcustomchat)
18+
- [OverrideCustomChatTags](#overridecustomchattags)
19+
- [OverrideCustomChatPlayerColor](#overridecustomchatplayercolor)
20+
- [CustomChatBlockInput](#customchatblockinput)
21+
- [CustomChatHideJoinMessage](#customchathidejoinmessage)
22+
- [Contributing](#contributing)
23+
24+
## Features
925

1026
* Customizable
1127
* Has built-in emojis
@@ -23,7 +39,7 @@ A simple and customizable chat box that can format text, display images and emoj
2339

2440
---
2541

26-
### Text Formatting Options
42+
## Text formatting
2743

2844
```
2945
||Spoilers here||
@@ -40,20 +56,29 @@ $$rainbow text here$$
4056
$255,0,0,0,100,255$(red-to-blue gradient text here)
4157
```
4258

43-
### Fonts
59+
## Fonts
4460

4561
You can change the font by typing **;fontname;** before the text.
4662
_(A list of fonts can be found on the workshop page.)_
4763

4864
```;comic; This will be displayed as Comic Sans```
4965

50-
### Whitelisted Sites
66+
## Whitelisted sites
5167

5268
By default, the chat box will only load pictures from trusted websites. You can open a pull request to add more, or send a request [here](https://steamcommunity.com/workshop/filedetails/discussion/2799307109/3272437487156558008/).
5369

54-
## For developers
70+
## Developer reference
5571

56-
### Hook: CanEmbedCustomChat
72+
### Hooks (quick links)
73+
74+
- [CanEmbedCustomChat](#canembedcustomchat)
75+
- [CanFormatCustomChat](#canformatcustomchat)
76+
- [OverrideCustomChatTags](#overridecustomchattags)
77+
- [OverrideCustomChatPlayerColor](#overridecustomchatplayercolor)
78+
- [CustomChatBlockInput](#customchatblockinput)
79+
- [CustomChatHideJoinMessage](#customchathidejoinmessage)
80+
81+
### CanEmbedCustomChat
5782

5883
You can prevent links from certain players from embedding, by using the `CanEmbedCustomChat` hook on the **client side**:
5984

@@ -72,7 +97,37 @@ hook.Add( "CanEmbedCustomChat", "chat_embed_access_example", function( ply, url,
7297
end )
7398
```
7499

75-
### Hook: OverrideCustomChatTags
100+
### CanFormatCustomChat
101+
102+
You can block specific text formatting types per-player on the client using the `CanFormatCustomChat` hook. Return `false` to prevent that formatting from being applied; the text will be shown as plain text instead.
103+
104+
```lua
105+
hook.Add( "CanFormatCustomChat", "format_access_example", function( ply, formatType, value )
106+
-- Return false to block this formatting for this player/message
107+
108+
-- formatType is one of:
109+
-- "url", "hyperlink", "gradient", "model", "font",
110+
-- "italic", "bold", "bold_italic", "color", "rainbow",
111+
-- "advert", "emoji", "spoiler", "code_line", "code"
112+
113+
-- Example: only allow admins to use gradients
114+
if formatType == "gradient" and not ply:IsAdmin() then
115+
return false
116+
end
117+
118+
-- Example: block spoilers for everyone
119+
if formatType == "spoiler" then
120+
return false
121+
end
122+
123+
-- Example: restrict links to super admins
124+
if (formatType == "url" or formatType == "hyperlink") and not ply:IsSuperAdmin() then
125+
return false
126+
end
127+
end )
128+
```
129+
130+
### OverrideCustomChatTags
76131

77132
You can add/override chat tags dynamically via code, using this hook on the **client side**:
78133

@@ -92,7 +147,7 @@ hook.Add( "OverrideCustomChatTags", "custom_tags_example", function( ply )
92147
end )
93148
```
94149

95-
### Hook: OverrideCustomChatPlayerColor
150+
### OverrideCustomChatPlayerColor
96151

97152
You can use this hook on the **client side** to override the colors that will be shown for player names.
98153

@@ -112,11 +167,11 @@ hook.Add( "OverrideCustomChatPlayerColor", "custom_player_color_example", functi
112167
end )
113168
```
114169

115-
### Hook: CustomChatBlockInput
170+
### CustomChatBlockInput
116171

117172
You can return `true` on this hook to block the "open chat" button(s). It runs on the **client side**.
118173

119-
### Hook: CustomChatHideJoinMessage
174+
### CustomChatHideJoinMessage
120175

121176
You can return `true` on this hook to dynamically prevent join/leave messages from showing up. It runs on the **client side**, and gives a `data` table as a argument, that contains the same keys given by the [player_connect_client](https://wiki.facepunch.com/gmod/gameevent/player_connect_client#members) hook.
122177

lua/custom_chat/client/parser.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@ function CustomChat.ParseString( str, outFunc )
112112
local value = Substring( str, r.s, r.e )
113113

114114
if value ~= "" then
115-
outFunc( r.type, value )
115+
local formatType = r.type
116+
117+
if CustomChat.lastReceivedMessage then
118+
local canFormat = hook.Run( "CanFormatCustomChat", CustomChat.lastReceivedMessage.speaker, r.type, value )
119+
if canFormat == false then
120+
formatType = "string"
121+
end
122+
end
123+
outFunc( formatType, value )
116124
end
117125
end
118126

lua/custom_chat/client/tags.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ end
3232
local function CustomChat_AddCustomTags( ply, text, isTeam, isDead )
3333
if not IsValid( ply ) or not ply:IsPlayer() then return end
3434

35+
CustomChat.lastReceivedMessage = CustomChat.lastReceivedMessage or { speaker = ply, text = text, channel = "global" }
36+
3537
local parts = Tags:GetParts( ply )
3638
local customParts, keepOriginal = hook.Run( "OverrideCustomChatTags", ply )
3739

@@ -44,7 +46,10 @@ local function CustomChat_AddCustomTags( ply, text, isTeam, isDead )
4446
end
4547
end
4648

47-
if not parts and not customParts then return end
49+
if not parts and not customParts then
50+
CustomChat.lastReceivedMessage = nil
51+
return
52+
end
4853

4954
local message = {}
5055

@@ -89,6 +94,7 @@ local function CustomChat_AddCustomTags( ply, text, isTeam, isDead )
8994
Insert( ": " .. text )
9095

9196
chat.AddText( unpack( message ) )
97+
CustomChat.lastReceivedMessage = nil
9298

9399
return true
94100
end

0 commit comments

Comments
 (0)