diff --git a/CS2-Tags.cs b/CS2-Tags.cs new file mode 100644 index 0000000..992d703 --- /dev/null +++ b/CS2-Tags.cs @@ -0,0 +1,161 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +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; +using static CounterStrikeSharp.API.Core.Listeners; + +namespace CS2_Tags; +public class CS2_Tags : BasePlugin +{ + 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.0"; + + public override void Load(bool hotReload) + { + CreateOrLoadJsonFile(ModuleDirectory + "/tags.json"); + + RegisterListener(OnClientPutInServer); + AddCommandListener("say", OnPlayerChat); + } + + private static void CreateOrLoadJsonFile(string filepath) + { + if (!File.Exists(filepath)) + { + var templateData = new JObject + { + ["tags"] = new JObject + { + ["@css/chat"] = new JObject + { + ["prefix"] = "{GREEN}[CHAT]", + ["nick_color"] = "{RED}", + ["message_color"] = "{GOLD}", + ["scoreboard"] = "[CHAT]" + }, + ["76561197961430531"] = new JObject + { + ["prefix"] = "{GREEN}[ADMIN]", + ["nick_color"] = "{RED}", + ["message_color"] = "{GOLD}", + ["scoreboard"] = "[ADMIN]" + }, + } + }; + + File.WriteAllText(filepath, templateData.ToString()); + var jsonData = File.ReadAllText(filepath); + JsonTags = JObject.Parse(jsonData); + } + else + { + var jsonData = File.ReadAllText(filepath); + JsonTags = JObject.Parse(jsonData); + } + } + + private void OnClientPutInServer(int playerSlot) + { + CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot); + + if (player == null || !player.IsValid) return; + + string steamid = new SteamID(player.SteamID).SteamId64.ToString(); + + if (JsonTags != null && JsonTags.TryGetValue("tags", out var tags) && tags is JObject tagsObject) + { + if (tagsObject.TryGetValue(steamid, out var playerTag) && playerTag is JObject) + { + player.Clan = playerTag["scoreboard"]?.ToString() ?? ""; + } + + foreach (var tagKey in tagsObject.Properties()) + { + 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) + { + player.Clan = permissionTag["prefix"]?.ToString() ?? ""; + } + } + } + } + } + } + + private HookResult OnPlayerChat(CCSPlayerController? player, CommandInfo info) + { + if (player == null || !player.IsValid) return HookResult.Continue; + string steamid = new SteamID(player.SteamID).SteamId64.ToString(); + + if (JsonTags != null && JsonTags.TryGetValue("tags", out var tags) && tags is JObject tagsObject) + { + if (tagsObject.TryGetValue(steamid, out var playerTag) && playerTag is JObject) + { + string prefix = playerTag["prefix"]?.ToString() ?? ""; + string nickColor = playerTag["nick_color"]?.ToString() ?? ""; + string messageColor = playerTag["message_color"]?.ToString() ?? ""; + + Server.PrintToChatAll(ReplaceTags($" {prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}")); + + return HookResult.Handled; + } + + foreach (var tagKey in tagsObject.Properties()) + { + 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() ?? ""; + string messageColor = permissionTag["message_color"]?.ToString() ?? ""; + Server.PrintToChatAll(ReplaceTags($" {prefix}{nickColor}{player.PlayerName}{ChatColors.Default}: {messageColor}{info.GetArg(1)}")); + + return HookResult.Handled; + } + } + } + } + } + + return HookResult.Continue; + } + + private string ReplaceTags(string message) + { + 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; + } + + return message; + } + +} diff --git a/CS2-Tags.csproj b/CS2-Tags.csproj new file mode 100644 index 0000000..0168292 --- /dev/null +++ b/CS2-Tags.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + CS2_Tags + enable + enable + true + + + + + + + + diff --git a/CS2-Tags.sln b/CS2-Tags.sln new file mode 100644 index 0000000..28a4de2 --- /dev/null +++ b/CS2-Tags.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CS2-Tags", "CS2-Tags.csproj", "{06A7649D-5CC9-4A37-9AB8-FEFB5DE69668}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {06A7649D-5CC9-4A37-9AB8-FEFB5DE69668}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {06A7649D-5CC9-4A37-9AB8-FEFB5DE69668}.Debug|Any CPU.Build.0 = Debug|Any CPU + {06A7649D-5CC9-4A37-9AB8-FEFB5DE69668}.Release|Any CPU.ActiveCfg = Release|Any CPU + {06A7649D-5CC9-4A37-9AB8-FEFB5DE69668}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal