Skip to content

Commit c4d3759

Browse files
committed
fixing bugs with arguments and cooldown
1 parent 36836a7 commit c4d3759

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

CustomCommands/Services/CooldownManager.cs

Lines changed: 17 additions & 5 deletions
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:

CustomCommands/Services/RegisterCommands.cs

Lines changed: 17 additions & 4 deletions
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,16 +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-
43+
44+
// Check if the command has arguments and if it does, check if the command exists and is the right one
4245
if (info.ArgCount > 1)
43-
command = PluginGlobals.CustomCommands.Find(x => x.Command.Contains(aliases[i])) ?? com;
46+
{
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;
53+
}
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;
4457

45-
if (command.Permission.PermissionList.Count > 0 && command.Permission != null)
58+
if (command!.Permission.PermissionList.Count > 0 && command.Permission != null)
4659
if (!PluginUtilities.RequiresPermissions(player, command.Permission))
4760
return;
4861

CustomCommands/test.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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
11+
* [X] check if toggle feature works

Examples/Colors.json

Lines changed: 2 additions & 2 deletions
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
]

Examples/MulitpleAliasesForOneCommand.json

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)