Skip to content

Commit 5a48cc0

Browse files
authored
Merge 9d86315 into a903413
2 parents a903413 + 9d86315 commit 5a48cc0

12 files changed

+106
-73
lines changed

Diff for: .github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
run: dotnet build ./CustomCommands/CustomCommands.csproj --configuration Release --no-restore
2626

2727
- name: Publish
28-
run: dotnet publish --configuration Release --no-build --output ./out
28+
run: dotnet publish ./CustomCommands/CustomCommands.csproj --configuration Release --no-build --output ./out
2929

3030
- name: Zip artifacts
3131
run: |

Diff for: CustomCommands/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/.vs
22
/bin
3-
/obj
3+
/obj
4+
5+
TestingCommands.json

Diff for: CustomCommands/Interfaces/IPluginUtilities.cs

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ public interface IPluginUtilities
88
string[] SplitStringByCommaOrSemicolon(string str);
99
void ExecuteServerCommands(Commands cmd, CCSPlayerController player);
1010
bool RequiresPermissions(CCSPlayerController player, Permission permissions);
11-
string PadLeftColorTag(string input);
1211
}

Diff for: CustomCommands/Services/CooldownManager.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using CounterStrikeSharp.API.Core;
33
using CustomCommands.Model;
4+
using Microsoft.Extensions.Logging;
45

56
namespace CustomCommands.Interfaces;
67

@@ -38,8 +39,15 @@ public bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate
3839
if (index != -1)
3940
{
4041
string timeleft = PluginGlobals.CooldownTimer[index].CooldownTime.Subtract(DateTime.Now).Seconds.ToString();
41-
player.PrintToChat($"{PluginGlobals.Config.Prefix}{cmd.Cooldown.CooldownMessage.Replace("{TIME}", timeleft)
42-
?? $"This command is for {timeleft} seconds on cooldown"}");
42+
string message = "";
43+
44+
// Check if cmd.Cooldown is a Cooldown object
45+
if (cmd.Cooldown is Cooldown cooldown)
46+
message = cooldown.CooldownMessage.Replace("{TIME}", timeleft);
47+
else
48+
message = $"This command is for {timeleft} seconds on cooldown";
49+
50+
player.PrintToChat($"{PluginGlobals.Config.Prefix}{message}");
4351

4452
return true;
4553
}
@@ -61,12 +69,14 @@ public void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int c
6169
CommandID = commandID,
6270
CooldownTime = DateTime.Now.AddSeconds(cooldownTime)
6371
};
64-
72+
Console.WriteLine("Cooldown 2");
6573
if (isGlobal)
6674
{
75+
Console.WriteLine("Cooldown 3");
6776
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
6877
x.IsGlobal == true
6978
&& x.CommandID == commandID);
79+
7080
if (index != -1)
7181
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
7282
else
@@ -97,17 +107,19 @@ public void SetCooldown(CCSPlayerController player, Commands cmd)
97107
switch (jsonElement.ValueKind)
98108
{
99109
case JsonValueKind.Number:
100-
int cooldown = (int)cmd.Cooldown;
110+
111+
int cooldown = cmd.Cooldown.GetInt32();
101112
if (cooldown == 0)
102113
break;
103114

104115
AddToCooldownList(false, player.UserId ?? 0, cmd.ID, cooldown);
105116
break;
106117

107118
case JsonValueKind.Object:
108-
Cooldown cooldownObject = (Cooldown)cmd.Cooldown;
109119

120+
var cooldownObject = JsonSerializer.Deserialize<Cooldown>(cmd.Cooldown.GetRawText());
110121
AddToCooldownList(cooldownObject.IsGlobal, player.UserId ?? 0, cmd.ID, cooldownObject.CooldownTime);
122+
111123
break;
112124

113125
default:

Diff for: CustomCommands/Services/LoadJson.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ public List<Commands> GetCommandsFromJsonFiles(string path)
1818
{
1919
var comms = new List<Commands>();
2020

21+
string defaultconfigpath = Path.Combine(path, "Commands.json");
22+
2123
CheckForExampleFile(path);
2224

2325
var pathofcommands = Path.Combine(path, "Commands");
24-
var defaultconfigpath = Path.Combine(path, "Commands.json");
2526

2627
var files = new List<string>();
2728

@@ -34,7 +35,6 @@ public List<Commands> GetCommandsFromJsonFiles(string path)
3435
files.Add(defaultconfigpath);
3536
Logger.LogInformation("Found default config file.");
3637
}
37-
//
3838
else if (!File.Exists(defaultconfigpath) && files.Count == 0)
3939
{
4040
Logger.LogWarning("No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/<name>.json");
@@ -58,14 +58,20 @@ public List<Commands> GetCommandsFromJsonFiles(string path)
5858
// Check if the Command.json file exists. If not replace it with the example file
5959
public void CheckForExampleFile(string path)
6060
{
61+
if (Directory.Exists(Path.Combine(path, "Commands")))
62+
{
63+
var files = Directory.GetFiles(Path.Combine(path, "Commands"), "*.json", SearchOption.AllDirectories);
64+
if (files.Length > 0)
65+
return;
66+
}
67+
6168
var defaultconfigpath = Path.Combine(path, "Commands.json");
6269
var exampleconfigpath = Path.Combine(path, "Commands.example.json");
6370
if (!File.Exists(defaultconfigpath))
6471
{
6572
File.Copy(exampleconfigpath, defaultconfigpath);
6673
Logger.LogInformation("Created default config file.");
6774
}
68-
6975
}
7076
public bool IsValidJsonSyntax(string path)
7177
{

Diff for: CustomCommands/Services/PluginUtilities.cs

-37
Original file line numberDiff line numberDiff line change
@@ -98,41 +98,4 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio
9898
return true;
9999
}
100100
}
101-
/// <summary>
102-
/// Moves the color tag one space the the left if it's not already there.
103-
/// Because the game doesn't support color tags at the start of a string.
104-
/// </summary>
105-
/// <param name="input"></param>
106-
/// <returns></returns>
107-
public string PadLeftColorTag(string input)
108-
{
109-
string[] colorTagList = new string[] {
110-
"{DEFAULT}",
111-
"{WHITE}",
112-
"{DARKRED}",
113-
"{RED}",
114-
"{LIGHTRED}",
115-
"{GREEN}",
116-
"{LIME}",
117-
"{OLIVE}",
118-
"{ORANGE}",
119-
"{GOLD}",
120-
"{YELLOW}",
121-
"{BLUE}",
122-
"{DARKBLUE}",
123-
"{LIGHTPURPLE}",
124-
"{PURPLE}",
125-
"{SILVER}",
126-
"{BLUEGREY}",
127-
"{GREY}",
128-
};
129-
foreach (var colorTag in colorTagList)
130-
{
131-
if (input.StartsWith(colorTag))
132-
{
133-
return " " + input;
134-
}
135-
}
136-
return input;
137-
}
138101
}

Diff for: CustomCommands/Services/RegisterCommands.cs

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using CounterStrikeSharp.API.Core.Plugin;
23
using CustomCommands.Interfaces;
34
using CustomCommands.Model;
@@ -33,17 +34,28 @@ public void AddCommands(Commands com)
3334

3435
for (int i = 0; i < aliases.Length; i++)
3536
{
36-
plugin.AddCommand(aliases[i], com.Description, (player, info) =>
37+
string alias = aliases[i];
38+
plugin.AddCommand(alias, com.Description, (player, info) =>
3739
{
3840
if (player == null) return;
3941

4042
var command = com;
41-
if (info.ArgCount > 0)
43+
44+
// Check if the command has arguments and if it does, check if the command exists and is the right one
45+
if (info.ArgCount > 1)
4246
{
43-
command = PluginGlobals.CustomCommands.Find(x => x.Command.Contains(aliases[i])) ?? com;
47+
var findcommand = PluginGlobals.CustomCommands.Find(x => x.Command.Contains(alias + $" {info.ArgString}"));
48+
// Check if the command is the right one with the right arguments
49+
if (findcommand! != command)
50+
return;
51+
52+
command = findcommand;
4453
}
54+
// This will exit the command if the command has arguments but the client didn't provide any
55+
if (command!.Command.Contains(alias + " ") && info.ArgCount <= 1)
56+
return;
4557

46-
if (command.Permission.PermissionList.Count > 0 && command.Permission != null)
58+
if (command!.Permission.PermissionList.Count > 0 && command.Permission != null)
4759
if (!PluginUtilities.RequiresPermissions(player, command.Permission))
4860
return;
4961

@@ -70,12 +82,12 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
7082

7183
foreach (var alias in aliases)
7284
{
73-
if (commandNames.Contains(alias))
85+
if (commandNames.Contains(alias.ToLower()))
7486
{
7587
duplicateCommands.Add(com);
7688
continue;
7789
}
78-
commandNames.Add(alias);
90+
commandNames.Add(alias.ToLower());
7991
}
8092
}
8193

Diff for: CustomCommands/Services/ReplaceTagsFunction.cs

+40-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ public class ReplaceTagsFunctions : IReplaceTagsFunctions
1515
private readonly IPluginGlobals PluginGlobals;
1616
private readonly PluginContext PluginContext;
1717
private readonly ILogger<CustomCommands> Logger;
18-
private readonly IPluginUtilities PluginUtilities;
1918

2019
public ReplaceTagsFunctions(IPluginGlobals PluginGlobals, IPluginContext PluginContext,
21-
ILogger<CustomCommands> Logger, IPluginUtilities PluginUtilities)
20+
ILogger<CustomCommands> Logger)
2221
{
2322
this.PluginGlobals = PluginGlobals;
2423
this.PluginContext = (PluginContext as PluginContext)!;
2524
this.Logger = Logger;
26-
this.PluginUtilities = PluginUtilities;
2725
}
2826

2927
public string[] ReplaceTags(string[] input, CCSPlayerController player)
@@ -96,7 +94,7 @@ public string ReplaceMessageTags(string input, CCSPlayerController player)
9694
public string ReplaceColorTags(string input)
9795
{
9896
// PadLeft the color tag if it's not already there because the game doesn't support color tags at the start of a string.
99-
input = PluginUtilities.PadLeftColorTag(input);
97+
input = PadLeftColorTag(input);
10098

10199
Dictionary<string, string> replacements = new()
102100
{
@@ -164,4 +162,42 @@ public string[] WrappedLine(dynamic input)
164162

165163
return output.ToArray();
166164
}
165+
166+
/// <summary>
167+
/// Moves the color tag one space the the left if it's not already there.
168+
/// Because the game doesn't support color tags at the start of a string.
169+
/// </summary>
170+
/// <param name="input"></param>
171+
/// <returns></returns>
172+
private string PadLeftColorTag(string input)
173+
{
174+
string[] colorTagList = new string[] {
175+
"{DEFAULT}",
176+
"{WHITE}",
177+
"{DARKRED}",
178+
"{RED}",
179+
"{LIGHTRED}",
180+
"{GREEN}",
181+
"{LIME}",
182+
"{OLIVE}",
183+
"{ORANGE}",
184+
"{GOLD}",
185+
"{YELLOW}",
186+
"{BLUE}",
187+
"{DARKBLUE}",
188+
"{LIGHTPURPLE}",
189+
"{PURPLE}",
190+
"{SILVER}",
191+
"{BLUEGREY}",
192+
"{GREY}",
193+
};
194+
foreach (var colorTag in colorTagList)
195+
{
196+
if (input.StartsWith(colorTag))
197+
{
198+
return " " + input;
199+
}
200+
}
201+
return input;
202+
}
167203
}

Diff for: CustomCommands/test.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
## Test this:
22

3-
* [ ] Test tag support for server commands
4-
* [ ] check if USERID tag is the right one from the console
5-
* [ ] check if the string pattern check works
6-
* [ ] check cooldown funktion for normal int and than object
7-
* [ ] check if padleft color tag works now
8-
* [ ] If this works remove all spaced in the example files with color tags
9-
* [ ] check if command args works
3+
* [X] Test tag support for server commands
4+
* [X] check if USERID tag is the right one from the console
5+
* [X] check if the string pattern check works
6+
* [X] check cooldown funktion for normal int and than object
7+
* [X] check if padleft color tag works now
8+
* [X] If this works remove all spaced in the example files with color tags
9+
* [X] check if command args works
1010
* [ ] check if duplicated commands still wont work with this feature
11-
* [ ] check if toggle feature works
12-
13-
14-
15-
test
11+
* [X] check if toggle feature works

Diff for: Examples/Colors.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"Title": "Rainbow",
44
"Description": "rainbow command",
55
"Command": "rainbow",
6-
"Message": " {RED}R{ORANGE}A{YELLOW}I{GREEN}N{BLUE}B{DARKBLUE}O{PURPLE}W",
6+
"Message": "{RED}R{ORANGE}A{YELLOW}I{GREEN}N{BLUE}B{DARKBLUE}O{PURPLE}W",
77
"PrintTo": 0
88
},
99
{
1010
"Title": "All Colors",
1111
"Description": "This command displays all the colors that are available to use in the chat.",
1212
"Command": "colors",
13-
"Message": "{DEFAULT}1, {WHITE}2 \n {DARKRED}3, {RED}4, {LIGHTRED}5\n {GREEN}6, {LIME}7, {OLIVE}8\n {ORANGE}9, {GOLD}10, {YELLOW}11\n {BLUE}12, {DARKBLUE}13\n {LIGHTPURPLE}14 {PURPLE}15 \n, {GREY}18",
13+
"Message": "{DEFAULT}1, {WHITE}2 \n{DARKRED}3, {RED}4, {LIGHTRED}5\n{GREEN}6, {LIME}7, {OLIVE}8\n{ORANGE}9, {GOLD}10, {YELLOW}11\n{BLUE}12, {DARKBLUE}13\n{LIGHTPURPLE}14 {PURPLE}15 \n,{GREY}18",
1414
"PrintTo": 0
1515
}
1616
]

Diff for: Examples/MulitpleAliasesForOneCommand.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"Title": "Discord",
44
"Description": "Command for Discord link",
5-
"Command": "discord,dc,Discord", // User can type !discord or /discord, !dc or /dc and !Discord or /discord in chat
5+
"Command": "discord,dc", // User can type !discord or /discord, !dc or /dc and !Discord or /discord in chat
66
"Message": "{PREFIX}Discord: https://discord.gg/Z9JfJ9Y57C",
77
"PrintTo": 0
88
}

Diff for: Examples/Tags.json

+7
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,12 @@
7575
"Command": "playercount",
7676
"Message": "Current playercount: {PLAYERS}",
7777
"PrintTo": 0
78+
},
79+
{
80+
"Title": "UserID",
81+
"Description": "Displays Player #userid",
82+
"Command": "userid",
83+
"Message": "Current players userid: {USERID}",
84+
"PrintTo": 0
7885
}
7986
]

0 commit comments

Comments
 (0)