Skip to content

Commit 6c63b94

Browse files
authored
Merge pull request #9 from HerrMagiic/dev
Update 1.0.7
2 parents c10231c + a598f94 commit 6c63b94

10 files changed

+176
-54
lines changed

Diff for: CustomCommands/Commands.json renamed to CustomCommands/Commands.example.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
"Description": "Command for SteamGroup",
1212
"Command": "steam,steamgroup,group",
1313
"Message": "SteamGroup: <link>",
14-
"CenterElement": {
15-
"CenterMessage": "<div>Steam Group</div><br><div><font color='#00ff00'>https...</font></div>",
16-
"CenterMessageTime": 10
14+
"CenterMessage": {
15+
"Message": "<div>Steam Group</div><br><div><font color='#00ff00'>https...</font></div>",
16+
"Time": 10
1717
},
1818
"PrintTo": 7
1919
},
2020
{
2121
"Title": "Enable Surf",
2222
"Command": "surf",
23-
"Message": "Surf is now enabled",
23+
"Message": [
24+
"Surf is now:",
25+
"{GREEN}Enabled"
26+
],
2427
"PrintTo": 0,
2528
"Description": "Command for Surf gamemode",
2629
"ServerCommands": [

Diff for: CustomCommands/CustomCommands.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace CustomCommands;
1212
public partial class CustomCommands : BasePlugin, IPluginConfig<CustomCommandsConfig>
1313
{
1414
public override string ModuleName => "CustomCommands";
15-
public override string ModuleVersion => "1.0.6";
15+
public override string ModuleVersion => "1.0.7";
1616
public override string ModuleAuthor => "HerrMagic";
1717
public override string ModuleDescription => "Create your own commands per config";
1818

@@ -46,9 +46,13 @@ public override void Load(bool hotReload)
4646

4747
RegisterListeners();
4848

49-
if (comms != null)
49+
50+
if (comms != null)
51+
{
52+
comms = CheckForDuplicateCommands(comms);
5053
foreach (var com in comms)
5154
AddCommands(com);
55+
}
5256

5357
if (hotReload)
5458
InitializeLists();

Diff for: CustomCommands/CustomCommands.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
<!--<OutputPath>.</OutputPath>-->
88
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
99
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
10+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
1314
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.98" />
1415
</ItemGroup>
1516

17+
<ItemGroup>
18+
<None Update="Commands.example.json">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
1623
</Project>

Diff for: CustomCommands/Functions.cs

+92-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Nodes;
13
using CounterStrikeSharp.API;
24
using CounterStrikeSharp.API.Core;
35
using CounterStrikeSharp.API.Modules.Admin;
46
using CounterStrikeSharp.API.Modules.Cvars;
57
using CounterStrikeSharp.API.Modules.Entities;
6-
using CounterStrikeSharp.API.Modules.Entities.Constants;
78
using CounterStrikeSharp.API.Modules.Utils;
89
using CustomCommands.Model;
9-
10+
using Microsoft.Extensions.Logging;
1011
namespace CustomCommands;
1112
public partial class CustomCommands
1213
{
@@ -31,7 +32,49 @@ private void RegisterListeners()
3132

3233
});
3334
}
35+
private List<Commands> CheckForDuplicateCommands(List<Commands> comms)
36+
{
37+
List<Commands> duplicateCommands = new();
38+
List<Commands> commands = new();
39+
List<string> commandNames = new();
40+
41+
foreach (var com in comms)
42+
{
43+
string[] aliases = com.Command.Split(',');
44+
45+
foreach (var alias in aliases)
46+
{
47+
if (commandNames.Contains(alias))
48+
{
49+
duplicateCommands.Add(com);
50+
continue;
51+
}
52+
commandNames.Add(alias);
53+
}
54+
}
55+
56+
if (duplicateCommands.Count == 0)
57+
return comms;
3458

59+
Logger.LogError($"------------------------------------------------------------------------");
60+
Logger.LogError($"{Config.LogPrefix} Duplicate commands found, removing them from the list. Please check your config file for duplicate commands and remove them.");
61+
for (int i = 0; i < comms.Count; i++)
62+
{
63+
if(duplicateCommands.Contains(comms[i]))
64+
{
65+
Logger.LogError($"{Config.LogPrefix} Duplicate command found index {i+1}: ");
66+
Logger.LogError($"{Config.LogPrefix} - {comms[i].Title} ");
67+
Logger.LogError($"{Config.LogPrefix} - {comms[i].Description}");
68+
Logger.LogError($"{Config.LogPrefix} - {comms[i].Command}");
69+
continue;
70+
}
71+
72+
commands.Add(comms[i]);
73+
}
74+
Logger.LogError($"------------------------------------------------------------------------");
75+
76+
return commands;
77+
}
3578
private void AddCommands(Commands com)
3679
{
3780
string[] aliases = com.Command.Split(',');
@@ -61,14 +104,14 @@ private bool RequiresPermissions(CCSPlayerController player, Permission permissi
61104
if (AdminManager.PlayerHasPermissions(player, new string[]{permission}))
62105
return true;
63106
}
64-
PrintToChat(Receiver.Client, player, "You don't have the required permissions to execute this command");
107+
player.PrintToChat($"{PrefixCache}You don't have the required permissions to execute this command");
65108
return false;
66109
}
67110
else
68111
{
69112
if (!AdminManager.PlayerHasPermissions(player, permissions.PermissionList.ToArray()))
70113
{
71-
PrintToChat(Receiver.Client, player, "You don't have the required permissions to execute this command");
114+
player.PrintToChat($"{PrefixCache}You don't have the required permissions to execute this command");
72115
return false;
73116
}
74117
return true;
@@ -116,14 +159,56 @@ private void TriggerMessage(CCSPlayerController player, Commands cmd)
116159
break;
117160
}
118161
}
119-
private string[] WrappedLine(string input)
162+
private string[] WrappedLine(dynamic input)
120163
{
121-
return input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
164+
List<string> output = new List<string>();
165+
166+
if (input is JsonElement jsonElement)
167+
{
168+
switch (jsonElement.ValueKind)
169+
{
170+
case JsonValueKind.String:
171+
string result = jsonElement.GetString()!;
172+
return result?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None) ?? Array.Empty<string>();
173+
174+
case JsonValueKind.Array:
175+
foreach (var arrayElement in jsonElement.EnumerateArray())
176+
{
177+
string[] lines = arrayElement.GetString()?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None) ?? Array.Empty<string>();
178+
output.AddRange(lines);
179+
}
180+
break;
181+
182+
default:
183+
Logger.LogError($"{Config.LogPrefix} Message is not a string or array");
184+
return Array.Empty<string>();
185+
}
186+
}
187+
else
188+
{
189+
Logger.LogError($"{Config.LogPrefix} Invalid input type");
190+
return Array.Empty<string>();
191+
}
192+
193+
return output.ToArray();
194+
}
195+
196+
private string[] ReplaceTags(string[] input, CCSPlayerController player)
197+
{
198+
string[] output = new string[input.Length];
199+
200+
for (int i = 0; i < input.Length; i++)
201+
{
202+
output[i] = ReplaceMessageTags(input[i], player);
203+
output[i] = ReplaceColorTags(output[i]);
204+
}
205+
206+
return output;
122207
}
123208

124209
private string ReplaceMessageTags(string input, CCSPlayerController player)
125210
{
126-
SteamID steamId = new SteamID((ulong?)player.UserId!.Value ?? 0);
211+
SteamID steamId = new SteamID(player.SteamID);
127212

128213
Dictionary<string, string> replacements = new()
129214
{

Diff for: CustomCommands/Model/CommandsConfig.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Commands
55
public required string Title { get; set; } = "";
66
public string Description { get; set; } = "Description";
77
public required string Command { get; set; } = "testtesttest";
8-
public string Message { get; set; } = "";
8+
public dynamic Message { get; set; } = "";
99
public CenterElement CenterMessage { get; set; } = new();
1010
public required Sender PrintTo { get; set; } = Sender.ClientChat;
1111
public List<string> ServerCommands { get; set; } = new();
@@ -32,4 +32,3 @@ public enum Sender
3232
AllChatClientCenter = 6,
3333
AllChatAllCenter = 7
3434
}
35-

Diff for: CustomCommands/PrintFunctions.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ private void PrintToChatAndAllCenter(Receiver receiver, CCSPlayerController play
3737
PrintToAllCenter(cmd);
3838
}
3939

40-
private void PrintToChat(Receiver printToChat, CCSPlayerController player, string message)
40+
private void PrintToChat(Receiver printToChat, CCSPlayerController player, dynamic message)
4141
{
42-
string replaceTags = ReplaceMessageTags(message, player);
43-
string replaceColorTags = ReplaceColorTags(replaceTags);
44-
string[] msg = WrappedLine(replaceColorTags);
42+
string[] msg = WrappedLine(message);
43+
msg = ReplaceTags(msg, player);
4544

4645
switch (printToChat)
4746
{

Diff for: Examples/CenterMessageWithColor.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"Title": "Center Message with color",
44
"Description": "Center Message with color",
55
"Command": "colorcenter",
6-
"CenterElement": {
7-
"CenterMessage": "<font color='#00ff00'>Cool color here</font>",
8-
"CenterMessageTime": 5 // Seconds
6+
"CenterMessage": {
7+
"Message": "<font color='#00ff00'>Cool color here</font>",
8+
"Time": 5 // Seconds
99
},
1010
"PrintTo": 2
1111
},
@@ -14,9 +14,9 @@
1414
"Title": "With html",
1515
"Description": "With html",
1616
"Command": "htmlcenter",
17-
"CenterElement": {
18-
"CenterMessage": "<div>Steam Group</div><br><div><font color='#00ff00'>https...</font></div>",
19-
"CenterMessageTime": 5 // Seconds
17+
"CenterMessage": {
18+
"Message": "<div>Steam Group</div><br><div><font color='#00ff00'>https...</font></div>",
19+
"Time": 5 // Seconds
2020
},
2121
"PrintTo": 2
2222
}

Diff for: Examples/NewLines.json

+18
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,23 @@
1212
"Command": "numbers",
1313
"Message": "1\n2\n3\n4\n5\n6\n7\n8\n9\r\n10", // \n or \r is a new line
1414
"PrintTo": 0
15+
},
16+
{
17+
"Title": "Numbers",
18+
"Description": "From one to ten with arrays as new lines",
19+
"Command": "numbers2",
20+
"Message": [ // If you want to use arrays as new lines, do this
21+
"1",
22+
"2",
23+
"3",
24+
"4",
25+
"5",
26+
"6",
27+
"7",
28+
"8",
29+
"9",
30+
"10"
31+
],
32+
"PrintTo": 0
1533
}
1634
]

Diff for: Examples/PrintTo.json

+18-18
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
"Title": "Client Center",
1818
"Description": "Client Center message",
1919
"Command": "clientcenter",
20-
"CenterElement": {
21-
"CenterMessage": "Client Center",
22-
"CenterMessageTime": 5 // Seconds
20+
"CenterMessage": {
21+
"Message": "Client Center",
22+
"Time": 5 // Seconds
2323
},
2424
"PrintTo": 2
2525
},
2626
{
2727
"Title": "All Center",
2828
"Description": "All players Center message",
2929
"Command": "allcenter",
30-
"CenterElement": {
31-
"CenterMessage": "All Center",
32-
"CenterMessageTime": 7
30+
"CenterMessage": {
31+
"Message": "All Center",
32+
"Time": 7
3333
},
3434
"PrintTo": 3
3535
},
@@ -38,9 +38,9 @@
3838
"Description": "Client Chat & Client Center message",
3939
"Command": "CCCC",
4040
"Message": "Client Chat",
41-
"CenterElement": {
42-
"CenterMessage": "Client Center",
43-
"CenterMessageTime": 10
41+
"CenterMessage": {
42+
"Message": "Client Center",
43+
"Time": 10
4444
},
4545
"PrintTo": 4
4646
},
@@ -49,9 +49,9 @@
4949
"Description": "Client Chat & All Center",
5050
"Command": "CCAC",
5151
"Message": "Client Chat",
52-
"CenterElement": {
53-
"CenterMessage": "All Center",
54-
"CenterMessageTime": 3
52+
"CenterMessage": {
53+
"Message": "All Center",
54+
"Time": 3
5555
},
5656
"PrintTo": 5
5757
},
@@ -60,9 +60,9 @@
6060
"Description": "All Chat & Client Center",
6161
"Command": "ACCC",
6262
"Message": "All Chat",
63-
"CenterElement": {
64-
"CenterMessage": "Client Center",
65-
"CenterMessageTime": 4
63+
"CenterMessage": {
64+
"Message": "Client Center",
65+
"Time": 4
6666
},
6767
"PrintTo": 6
6868
},
@@ -71,9 +71,9 @@
7171
"Description": "All Chat & All Center",
7272
"Command": "ACAC",
7373
"Message": "All Chat",
74-
"CenterElement": {
75-
"CenterMessage": "All Center",
76-
"CenterMessageTime": 10
74+
"CenterMessage": {
75+
"Message": "All Center",
76+
"Time": 10
7777
},
7878
"PrintTo": 7
7979
}

0 commit comments

Comments
 (0)