Initial commit
This commit is contained in:
parent
6a8a9d6f9e
commit
fc5318fc1f
3 changed files with 199 additions and 0 deletions
161
CS2-Tags.cs
Normal file
161
CS2-Tags.cs
Normal file
|
|
@ -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<Listeners.OnClientPutInServer>(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;
|
||||
}
|
||||
|
||||
}
|
||||
16
CS2-Tags.csproj
Normal file
16
CS2-Tags.csproj
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<RootNamespace>CS2_Tags</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.61" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
22
CS2-Tags.sln
Normal file
22
CS2-Tags.sln
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue