-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCS2-Tags.cs
497 lines (409 loc) · 16.3 KB
/
CS2-Tags.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
using Newtonsoft.Json.Linq;
using System.Reflection;
namespace CS2_Tags;
[MinimumApiVersion(159)]
public class CS2_Tags : BasePlugin
{
private HashSet<string> GaggedIds = new HashSet<string>();
public static JObject? JsonTags { get; private set; }
public override string ModuleName => "CS2-Tags";
public override string ModuleDescription => "Add player tags easily in cs2 game";
public override string ModuleAuthor => "daffyy";
public override string ModuleVersion => "1.0.4c";
public override void Load(bool hotReload)
{
CreateOrLoadJsonFile(ModuleDirectory + "/tags.json");
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
RegisterEventHandler<EventPlayerConnectFull>(OnPlayerConnectFull);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath);
AddCommandListener("say", OnPlayerChat);
AddCommandListener("say_team", OnPlayerChatTeam);
}
private void OnMapStart(string mapName)
{
GaggedIds.Clear();
}
private static void CreateOrLoadJsonFile(string filepath)
{
if (!File.Exists(filepath))
{
var templateData = new JObject
{
["tags"] = new JObject
{
["#css/admin"] = new JObject
{
["prefix"] = "{RED}[ADMIN]",
["nick_color"] = "{RED}",
["message_color"] = "{GOLD}",
["scoreboard"] = "[ADMIN]"
},
["@css/chat"] = new JObject
{
["prefix"] = "{GREEN}[CHAT]",
["nick_color"] = "{RED}",
["message_color"] = "{GOLD}",
["scoreboard"] = "[CHAT]"
},
["76561197961430531"] = new JObject
{
["prefix"] = "{RED}[ADMIN]",
["nick_color"] = "{RED}",
["message_color"] = "{GOLD}",
["scoreboard"] = "[ADMIN]"
},
["everyone"] = new JObject
{
["team_chat"] = false,
["prefix"] = "{Grey}[Player]",
["nick_color"] = "",
["message_color"] = "",
["scoreboard"] = "[Player]"
},
}
};
File.WriteAllText(filepath, templateData.ToString());
var jsonData = File.ReadAllText(filepath);
JsonTags = JObject.Parse(jsonData);
}
else
{
var jsonData = File.ReadAllText(filepath);
JsonTags = JObject.Parse(jsonData);
}
}
[ConsoleCommand("css_tags_reload")]
public void OnReloadConfig(CCSPlayerController? player, CommandInfo info)
{
if (player != null) return;
CreateOrLoadJsonFile(ModuleDirectory + "/tags.json");
Server.PrintToConsole("[CS2-Tags] Config reloaded!");
}
[ConsoleCommand("css_tag_mute")]
[CommandHelper(minArgs: 1, usage: "<SteamID>", whoCanExecute: CommandUsage.SERVER_ONLY)]
public void OnTagMuteCommand(CCSPlayerController? caller, CommandInfo command)
{
string? steamid = command.GetArg(1);
if (steamid.Length == 17)
{
if (!GaggedIds.Contains(steamid))
GaggedIds.Add(steamid);
}
}
[ConsoleCommand("css_tag_unmute")]
[CommandHelper(minArgs: 1, usage: "<SteamID>", whoCanExecute: CommandUsage.SERVER_ONLY)]
public void OnTagUnMuteCommand(CCSPlayerController? caller, CommandInfo command)
{
string? steamid = command.GetArg(1);
if (steamid.Length == 17)
{
if (GaggedIds.Contains(steamid))
GaggedIds.Remove(steamid);
}
}
private void OnClientAuthorized(int playerSlot, SteamID steamId)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
AddTimer(2.0f, () => SetPlayerClanTag(player));
}
private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return HookResult.Continue;
AddTimer(2.0f, () => SetPlayerClanTag(player));
return HookResult.Continue;
}
private void OnClientDisconnect(int playerSlot)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
GaggedIds.Remove(player.SteamID.ToString()!);
}
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot) return HookResult.Continue;
AddTimer(1.5f, () => SetPlayerClanTag(player));
return HookResult.Continue;
}
private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot) return HookResult.Continue;
AddTimer(1.5f, () => SetPlayerClanTag(player));
return HookResult.Continue;
}
private HookResult OnPlayerChat(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid || info.GetArg(1).Length == 0 || player.AuthorizedSteamID == null) return HookResult.Continue;
string steamid = player.AuthorizedSteamID.SteamId64.ToString();
if (player.SteamID.ToString() != "" && GaggedIds.Contains(player.SteamID.ToString())) return HookResult.Handled;
if (info.GetArg(1).StartsWith("!") || info.GetArg(1).StartsWith("@") || info.GetArg(1).StartsWith("/") || info.GetArg(1).StartsWith(".") || info.GetArg(1) == "rtv") return HookResult.Continue;
if (JsonTags != null && JsonTags.TryGetValue("tags", out var tags) && tags is JObject tagsObject)
{
string deadIcon = !player.PawnIsAlive ? $"{ChatColors.White}☠ {ChatColors.Default}" : "";
if (tagsObject.TryGetValue(steamid, out var playerTag) && playerTag is JObject)
{
string prefix = playerTag["prefix"]?.ToString() ?? "";
string nickColor = playerTag?["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = playerTag?["message_color"]?.ToString() ?? ChatColors.Default.ToString();
Server.PrintToChatAll(ReplaceTags($" {deadIcon}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}", player.TeamNum));
return HookResult.Handled;
}
foreach (var tagKey in tagsObject.Properties())
{
if (tagKey.Name.StartsWith("#"))
{
string group = tagKey.Name;
bool inGroup = AdminManager.PlayerInGroup(player, group);
if (inGroup)
{
if (tagsObject.TryGetValue(group, out var groupTag) && groupTag is JObject)
{
string prefix = groupTag["prefix"]?.ToString() ?? "";
string nickColor = groupTag?["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = groupTag?["message_color"]?.ToString() ?? ChatColors.Default.ToString();
Server.PrintToChatAll(ReplaceTags($" {deadIcon}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}", player.TeamNum));
return HookResult.Handled;
}
}
}
if (tagKey.Name.StartsWith("@"))
{
string permission = tagKey.Name;
bool hasPermission = AdminManager.PlayerHasPermissions(player, permission);
if (hasPermission)
{
if (tagsObject.TryGetValue(permission, out var permissionTag) && permissionTag is JObject)
{
string prefix = permissionTag["prefix"]?.ToString() ?? "";
string nickColor = permissionTag?["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = permissionTag?["message_color"]?.ToString() ?? ChatColors.Default.ToString();
Server.PrintToChatAll(ReplaceTags($" {deadIcon}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}", player.TeamNum));
return HookResult.Handled;
}
}
}
}
if (tagsObject.TryGetValue("everyone", out var everyoneTag) && everyoneTag is JObject && everyoneTag?["team_chat"]?.Value<bool>() == true)
{
string prefix = everyoneTag["prefix"]?.ToString() ?? "";
string nickColor = everyoneTag?["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = everyoneTag?["message_color"]?.ToString() ?? ChatColors.Default.ToString();
Server.PrintToChatAll(ReplaceTags($" {deadIcon}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}", player.TeamNum));
return HookResult.Handled;
}
}
return HookResult.Continue;
}
private HookResult OnPlayerChatTeam(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid || info.GetArg(1).Length == 0 || player.AuthorizedSteamID == null) return HookResult.Continue;
string steamid = player.AuthorizedSteamID.SteamId64.ToString();
if (player.SteamID.ToString() != "" && GaggedIds.Contains(player.SteamID.ToString())) return HookResult.Handled;
if (info.GetArg(1).StartsWith("@") && AdminManager.PlayerHasPermissions(player, "@css/chat"))
{
foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && !p.IsBot && !p.IsHLTV && AdminManager.PlayerHasPermissions(p, "@css/chat")))
{
p.PrintToChat($" {ChatColors.Lime}(ADMIN) {ChatColors.Default}{player.PlayerName}: {info.GetArg(1).Remove(0, 1)}");
}
return HookResult.Handled;
}
if (info.GetArg(1).StartsWith("!") || info.GetArg(1).StartsWith("@") || info.GetArg(1).StartsWith("/") || info.GetArg(1).StartsWith(".") || info.GetArg(1) == "rtv") return HookResult.Continue;
if (JsonTags != null && JsonTags.TryGetValue("tags", out var tags) && tags is JObject tagsObject)
{
string deadIcon = !player.PawnIsAlive ? $"{ChatColors.White}☠ {ChatColors.Default}" : "";
if (tagsObject.TryGetValue(steamid, out var playerTag) && playerTag is JObject)
{
string prefix = playerTag["prefix"]?.ToString() ?? "";
string nickColor = playerTag?["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = playerTag?["message_color"]?.ToString() ?? ChatColors.Default.ToString();
foreach (var p in Utilities.GetPlayers().Where(p => p.TeamNum == player.TeamNum && p.IsValid && !p.IsBot))
{
string messageToSend = $"{deadIcon}{TeamName(player.TeamNum)} {ChatColors.Default}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}";
p.PrintToChat($" {ReplaceTags(messageToSend, p.TeamNum)}");
}
return HookResult.Handled;
}
foreach (var tagKey in tagsObject.Properties())
{
if (tagKey.Name.StartsWith("#"))
{
string group = tagKey.Name;
bool inGroup = AdminManager.PlayerInGroup(player, group);
if (inGroup && tagsObject.TryGetValue(group, out var groupTag) && groupTag is JObject)
{
string prefix = groupTag["prefix"]?.ToString() ?? "";
string nickColor = groupTag["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = groupTag["message_color"]?.ToString() ?? ChatColors.Default.ToString();
foreach (var p in Utilities.GetPlayers().Where(p => p.TeamNum == player.TeamNum && p.IsValid && !p.IsBot))
{
string messageToSend = $"{deadIcon}{TeamName(player.TeamNum)} {ChatColors.Default}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}";
p.PrintToChat($" {ReplaceTags(messageToSend, p.TeamNum)}");
}
return HookResult.Handled;
}
}
if (tagKey.Name.StartsWith("@"))
{
string permission = tagKey.Name;
bool hasPermission = AdminManager.PlayerHasPermissions(player, permission);
if (hasPermission && tagsObject.TryGetValue(permission, out var permissionTag) && permissionTag is JObject)
{
string prefix = permissionTag["prefix"]?.ToString() ?? "";
string nickColor = permissionTag["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = permissionTag["message_color"]?.ToString() ?? ChatColors.Default.ToString();
foreach (var p in Utilities.GetPlayers().Where(p => p.TeamNum == player.TeamNum && p.IsValid && !p.IsBot))
{
string messageToSend = $"{deadIcon}{TeamName(player.TeamNum)} {ChatColors.Default}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}";
p.PrintToChat($" {ReplaceTags(messageToSend, p.TeamNum)}");
}
return HookResult.Handled;
}
}
}
if (tagsObject.TryGetValue("everyone", out var everyoneTag) && everyoneTag is JObject)
{
string prefix = everyoneTag["prefix"]?.ToString() ?? "";
string nickColor = everyoneTag["nick_color"]?.ToString() ?? ChatColors.Default.ToString();
string messageColor = everyoneTag["message_color"]?.ToString() ?? ChatColors.Default.ToString();
foreach (var p in Utilities.GetPlayers().Where(p => p.TeamNum == player.TeamNum && p.IsValid && !p.IsBot))
{
string messageToSend = $"{deadIcon}{TeamName(player.TeamNum)} {ChatColors.Default}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}";
p.PrintToChat($" {ReplaceTags(messageToSend, p.TeamNum)}");
}
//p.PrintToChat(ReplaceTags($" {TeamName(player.TeamNum)} {ChatColors.Default}{prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}", p.TeamNum));
return HookResult.Handled;
}
}
return HookResult.Continue;
}
private void SetPlayerClanTag(CCSPlayerController? player)
{
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || player.AuthorizedSteamID == null) return;
string steamid = player.SteamID!.ToString();
if (JsonTags != null && JsonTags.TryGetValue("tags", out var tags) && tags is JObject tagsObject)
{
if (tagsObject.TryGetValue(steamid, out var playerTag) && playerTag is JObject)
{
var scoreboardValue = playerTag["scoreboard"]?.ToString();
if (!string.IsNullOrEmpty(scoreboardValue))
{
player.Clan = scoreboardValue;
return;
}
}
foreach (var tagKey in tagsObject.Properties())
{
if (tagKey.Name.StartsWith("#"))
{
string group = tagKey.Name;
bool inGroup = AdminManager.PlayerInGroup(player, group);
if (inGroup)
{
if (tagsObject.TryGetValue(group, out var groupTag) && groupTag is JObject)
{
var scoreboardValue = groupTag["scoreboard"]?.ToString();
if (!string.IsNullOrEmpty(scoreboardValue))
{
player.Clan = scoreboardValue;
return;
}
}
}
}
if (tagKey.Name.StartsWith("@"))
{
string permission = tagKey.Name;
bool hasPermission = AdminManager.PlayerHasPermissions(player, permission);
if (hasPermission)
{
if (tagsObject.TryGetValue(permission, out var permissionTag) && permissionTag is JObject)
{
var scoreboardValue = permissionTag["scoreboard"]?.ToString();
if (!string.IsNullOrEmpty(scoreboardValue))
{
player.Clan = scoreboardValue;
return;
}
}
}
}
}
if (tagsObject.TryGetValue("everyone", out var everyone) && everyone is JObject everyoneObject)
{
var scoreboardValue = everyoneObject["scoreboard"]?.ToString();
if (!string.IsNullOrEmpty(scoreboardValue))
{
player.Clan = scoreboardValue;
}
}
}
}
private string TeamName(int teamNum)
{
string teamName = "";
switch (teamNum)
{
case 0:
teamName = $"(NONE)";
break;
case 1:
teamName = $"(SPEC)";
break;
case 2:
teamName = $"{ChatColors.Yellow}(T)";
break;
case 3:
teamName = $"{ChatColors.Blue}(CT)";
break;
}
return teamName;
}
private string TeamColor(int teamNum)
{
string teamColor;
switch (teamNum)
{
case 2:
teamColor = $"{ChatColors.Gold}";
break;
case 3:
teamColor = $"{ChatColors.Blue}";
break;
default:
teamColor = "";
break;
}
return teamColor;
}
private string ReplaceTags(string message, int teamNum = 0)
{
if (message.Contains('{'))
{
string modifiedValue = message;
foreach (FieldInfo field in typeof(ChatColors).GetFields())
{
string pattern = $"{{{field.Name}}}";
if (message.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
modifiedValue = modifiedValue.Replace(pattern, field.GetValue(null)!.ToString(), StringComparison.OrdinalIgnoreCase);
}
}
return modifiedValue.Replace("{TEAMCOLOR}", TeamColor(teamNum));
}
return message;
}
}