Skip to content

Commit 2c8bcef

Browse files
committed
Update Color tags and update readme with wrong color tags
1 parent 28a27f4 commit 2c8bcef

File tree

5 files changed

+351
-3
lines changed

5 files changed

+351
-3
lines changed

.github/img/ColorsCS2.png

28.7 KB
Loading
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
using System.Text.Json;
2+
using CounterStrikeSharp.API;
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Modules.Admin;
5+
using CustomCommands.Model;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace CustomCommands;
9+
public partial class CustomCommands
10+
{
11+
private void RegisterListeners()
12+
{
13+
RegisterListener<Listeners.OnTick>(() =>
14+
{
15+
// Client Print To Center
16+
foreach (var player in centerClientOn)
17+
Utilities.GetPlayerFromUserid(player.ClientId).PrintToCenterHtml(player.Message, 1);
18+
19+
// Server Print To Center
20+
if (centerServerOn.IsRunning)
21+
{
22+
Utilities.GetPlayers().ForEach(controller =>
23+
{
24+
if (controller == null || !controller.IsValid) return;
25+
26+
string message = ReplaceMessageTags(centerServerOn.Message, controller);
27+
controller.PrintToCenterHtml(message, 1);
28+
});
29+
}
30+
});
31+
}
32+
33+
private List<Commands> CheckForDuplicateCommands(List<Commands> comms)
34+
{
35+
List<Commands> duplicateCommands = new();
36+
List<Commands> commands = new();
37+
List<string> commandNames = new();
38+
39+
foreach (var com in comms)
40+
{
41+
string[] aliases = com.Command.Split(',');
42+
43+
foreach (var alias in aliases)
44+
{
45+
if (commandNames.Contains(alias))
46+
{
47+
duplicateCommands.Add(com);
48+
continue;
49+
}
50+
commandNames.Add(alias);
51+
}
52+
}
53+
54+
if (duplicateCommands.Count == 0)
55+
return comms;
56+
57+
Logger.LogError($"------------------------------------------------------------------------");
58+
Logger.LogError($"{Config.LogPrefix} Duplicate commands found, removing them from the list. Please check your config file for duplicate commands and remove them.");
59+
for (int i = 0; i < comms.Count; i++)
60+
{
61+
if(duplicateCommands.Contains(comms[i]))
62+
{
63+
Logger.LogError($"{Config.LogPrefix} Duplicate command found index {i+1}: ");
64+
Logger.LogError($"{Config.LogPrefix} - {comms[i].Title} ");
65+
Logger.LogError($"{Config.LogPrefix} - {comms[i].Description}");
66+
Logger.LogError($"{Config.LogPrefix} - {comms[i].Command}");
67+
continue;
68+
}
69+
70+
commands.Add(comms[i]);
71+
}
72+
Logger.LogError($"------------------------------------------------------------------------");
73+
74+
return commands;
75+
}
76+
private void AddCommands(Commands com)
77+
{
78+
string[] aliases = com.Command.Split(',');
79+
80+
for (int i = 0; i < aliases.Length; i++)
81+
{
82+
AddCommand(aliases[i], com.Description, (player, info) =>
83+
{
84+
if (player == null) return;
85+
86+
if (com.Permission.PermissionList.Count > 0 && com.Permission != null)
87+
if (!RequiresPermissions(player, com.Permission))
88+
return;
89+
90+
SendMessage(player, com);
91+
92+
ExecuteServerCommands(com);
93+
});
94+
}
95+
}
96+
97+
private bool RequiresPermissions(CCSPlayerController player, Permission permissions)
98+
{
99+
if (!permissions.ReguiresAllPermissions)
100+
{
101+
foreach (var permission in permissions.PermissionList)
102+
{
103+
if (AdminManager.PlayerHasPermissions(player, new string[]{permission}))
104+
return true;
105+
}
106+
player.PrintToChat($"{Config.Prefix}You don't have the required permissions to execute this command");
107+
return false;
108+
}
109+
else
110+
{
111+
if (!AdminManager.PlayerHasPermissions(player, permissions.PermissionList.ToArray()))
112+
{
113+
player.PrintToChat($"{Config.Prefix}You don't have the required permissions to execute this command");
114+
return false;
115+
}
116+
return true;
117+
}
118+
}
119+
120+
private void ExecuteServerCommands(Commands cmd)
121+
{
122+
if (cmd.ServerCommands.Count == 0) return;
123+
124+
foreach (var serverCommand in cmd.ServerCommands)
125+
{
126+
Server.ExecuteCommand(serverCommand);
127+
}
128+
}
129+
130+
private void SendMessage(CCSPlayerController player, Commands cmd)
131+
{
132+
switch (cmd.PrintTo)
133+
{
134+
case Sender.ClientChat:
135+
PrintToChat(Receiver.Client, player, cmd.Message);
136+
break;
137+
case Sender.AllChat:
138+
PrintToChat(Receiver.Server, player, cmd.Message);
139+
break;
140+
case Sender.ClientCenter:
141+
PrintToCenterClient(player, cmd);
142+
break;
143+
case Sender.AllCenter:
144+
PrintToAllCenter(cmd);
145+
break;
146+
case Sender.ClientChatClientCenter:
147+
PrintToChatAndCenter(Receiver.Client, player, cmd);
148+
break;
149+
case Sender.ClientChatAllCenter:
150+
PrintToChatAndAllCenter(Receiver.Client, player, cmd);
151+
break;
152+
case Sender.AllChatClientCenter:
153+
PrintToChatAndCenter(Receiver.Server, player, cmd);
154+
break;
155+
case Sender.AllChatAllCenter:
156+
PrintToChatAndAllCenter(Receiver.Server, player, cmd);
157+
break;
158+
default:
159+
break;
160+
}
161+
}
162+
163+
private string[] WrappedLine(dynamic input)
164+
{
165+
List<string> output = new List<string>();
166+
167+
if (input is JsonElement jsonElement)
168+
{
169+
switch (jsonElement.ValueKind)
170+
{
171+
case JsonValueKind.String:
172+
string result = jsonElement.GetString()!;
173+
return result?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None) ?? Array.Empty<string>();
174+
175+
case JsonValueKind.Array:
176+
foreach (var arrayElement in jsonElement.EnumerateArray())
177+
{
178+
string[] lines = arrayElement.GetString()?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None) ?? Array.Empty<string>();
179+
output.AddRange(lines);
180+
}
181+
break;
182+
183+
default:
184+
Logger.LogError($"{Config.LogPrefix} Message is not a string or array");
185+
return Array.Empty<string>();
186+
}
187+
}
188+
else
189+
{
190+
Logger.LogError($"{Config.LogPrefix} Invalid input type");
191+
return Array.Empty<string>();
192+
}
193+
194+
return output.ToArray();
195+
}
196+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using CounterStrikeSharp.API;
2+
using CounterStrikeSharp.API.Core;
3+
using CustomCommands.Model;
4+
5+
namespace CustomCommands;
6+
public partial class CustomCommands
7+
{
8+
private void PrintToCenterClient(CCSPlayerController player, Commands cmd)
9+
{
10+
string message = ReplaceMessageTags(cmd.CenterMessage.Message, player);
11+
12+
var CenterClientElement = new CenterClientElement
13+
{
14+
ClientId = player.UserId!.Value,
15+
Message = message
16+
};
17+
centerClientOn.Add(CenterClientElement);
18+
AddTimer(cmd.CenterMessage.Time, () => centerClientOn.Remove(CenterClientElement));
19+
}
20+
21+
private void PrintToAllCenter(Commands cmd)
22+
{
23+
centerServerOn.Message = cmd.CenterMessage.Message;
24+
centerServerOn.IsRunning = true;
25+
26+
AddTimer(cmd.CenterMessage.Time, () =>
27+
{
28+
centerServerOn.IsRunning = false;
29+
});
30+
}
31+
32+
private void PrintToChatAndCenter(Receiver receiver, CCSPlayerController player, Commands cmd)
33+
{
34+
PrintToChat(receiver, player, cmd.Message);
35+
PrintToCenterClient(player, cmd);
36+
}
37+
38+
private void PrintToChatAndAllCenter(Receiver receiver, CCSPlayerController player, Commands cmd)
39+
{
40+
PrintToChat(receiver, player, cmd.Message);
41+
PrintToAllCenter(cmd);
42+
}
43+
44+
private void PrintToChat(Receiver printToChat, CCSPlayerController player, dynamic message)
45+
{
46+
string[] msg = WrappedLine(message);
47+
msg = ReplaceTags(msg, player);
48+
49+
switch (printToChat)
50+
{
51+
case Receiver.Client:
52+
PrintToChatClient(player, msg);
53+
break;
54+
case Receiver.Server:
55+
PrintToChatServer(msg);
56+
break;
57+
default:
58+
break;
59+
}
60+
}
61+
62+
private void PrintToChatClient(CCSPlayerController player, string[] msg)
63+
{
64+
foreach (var line in msg)
65+
player.PrintToChat(line);
66+
}
67+
68+
private void PrintToChatServer(string[] msg)
69+
{
70+
foreach (var line in msg)
71+
Server.PrintToChatAll(line);
72+
}
73+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using CounterStrikeSharp.API;
2+
using CounterStrikeSharp.API.Core;
3+
using CounterStrikeSharp.API.Modules.Cvars;
4+
using CounterStrikeSharp.API.Modules.Entities;
5+
using CounterStrikeSharp.API.Modules.Utils;
6+
7+
namespace CustomCommands;
8+
public partial class CustomCommands
9+
{
10+
private string[] ReplaceTags(string[] input, CCSPlayerController player)
11+
{
12+
string[] output = new string[input.Length];
13+
14+
for (int i = 0; i < input.Length; i++)
15+
{
16+
output[i] = ReplaceMessageTags(input[i], player);
17+
output[i] = ReplaceColorTags(output[i]);
18+
}
19+
20+
return output;
21+
}
22+
23+
private string ReplaceMessageTags(string input, CCSPlayerController player)
24+
{
25+
SteamID steamId = new SteamID(player.SteamID);
26+
27+
Dictionary<string, string> replacements = new()
28+
{
29+
{"{PREFIX}", Config.Prefix ?? "<PREFIX not found>"},
30+
{"{MAP}", NativeAPI.GetMapName() ?? "<MAP not found>"},
31+
{"{TIME}", DateTime.Now.ToString("HH:mm:ss") ?? "<TIME not found>"},
32+
{"{DATE}", DateTime.Now.ToString("dd.MM.yyyy") ?? "<DATE not found>"},
33+
{"{PLAYERNAME}", player.PlayerName ?? "<PLAYERNAME not found>"},
34+
{"{STEAMID2}", steamId.SteamId2 ?? "<STEAMID2 not found>"},
35+
{"{STEAMID3}", steamId.SteamId3 ?? "<STEAMID3 not found>"},
36+
{"{STEAMID32}", steamId.SteamId32.ToString() ?? "<STEAMID32 not found>"},
37+
{"{STEAMID64}", steamId.SteamId64.ToString() ?? "<STEAMID64 not found>"},
38+
{"{SERVERNAME}", ConVar.Find("hostname")!.StringValue ?? "<SERVERNAME not found>"},
39+
{"{IP}", ConVar.Find("ip")!.StringValue ?? "<IP not found>"},
40+
{"{PORT}", ConVar.Find("hostport")!.GetPrimitiveValue<int>().ToString() ?? "<PORT not found>"},
41+
{"{MAXPLAYERS}", Server.MaxPlayers.ToString() ?? "<MAXPLAYERS not found>"},
42+
{"{PLAYERS}",
43+
Utilities.GetPlayers().Count(u => u.PlayerPawn.Value != null && u.PlayerPawn.Value.IsValid).ToString() ?? "<PLAYERS not found>"}
44+
};
45+
46+
foreach (var pair in replacements)
47+
input = input.Replace(pair.Key, pair.Value);
48+
49+
return input;
50+
}
51+
52+
private string ReplaceColorTags(string input)
53+
{
54+
Dictionary<string, string> replacements = new()
55+
{
56+
{"{DEFAULT}", $"{ChatColors.Default}"},
57+
{"{WHITE}", $"{ChatColors.White}"},
58+
{"{DARKRED}", $"{ChatColors.Darkred}"},
59+
{"{RED}", $"{ChatColors.Red}"},
60+
{"{LIGHTRED}", $"{ChatColors.LightRed}"},
61+
{"{GREEN}", $"{ChatColors.Green}"},
62+
{"{LIME}", $"{ChatColors.Lime}"},
63+
{"{OLIVE}", $"{ChatColors.Olive}"},
64+
{"{ORANGE}", $"{ChatColors.Orange}"},
65+
{"{GOLD}", $"{ChatColors.Gold}"},
66+
{"{YELLOW}", $"{ChatColors.Yellow}"},
67+
{"{BLUE}", $"{ChatColors.Blue}"},
68+
{"{DARKBLUE}", $"{ChatColors.DarkBlue}"},
69+
{"{LIGHTPURPLE}", $"{ChatColors.LightPurple}"},
70+
{"{PURPLE}", $"{ChatColors.Purple}"},
71+
{"{SILVER}", $"{ChatColors.Silver}"},
72+
{"{BLUEGREY}", $"{ChatColors.BlueGrey}"},
73+
{"{GREY}", $"{ChatColors.Grey}"},
74+
};
75+
76+
foreach (var pair in replacements)
77+
input = input.Replace(pair.Key, pair.Value);
78+
79+
return input;
80+
}
81+
}

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,4 @@ Defines if the command requires specific permissions to execute:
111111

112112
### Colorlist
113113

114-
{DEFAULT}, {RED}, {LIGHTPURPLE}, {GREEN}, {LIME},
115-
{LIGHTGREEN}, {LIGHTRED}, {GRAY}, {LIGHTOLIVE},
116-
{OLIVE}, {LIGHTBLUE}, {BLUE}, {PURPLE}, {GRAYBLUE}
114+
![CS2Colors](.github\img\ColorsCS2.png)

0 commit comments

Comments
 (0)