Skip to content

Commit e3d881a

Browse files
committed
Implement SetCooldown method part 1
1 parent b8f4ad8 commit e3d881a

File tree

10 files changed

+114
-1
lines changed

10 files changed

+114
-1
lines changed

CustomCommands/CustomCommands.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CounterStrikeSharp.API.Core;
22
using CounterStrikeSharp.API.Core.Attributes;
33
using CustomCommands.Interfaces;
4+
using CustomCommands.Services;
45
using Microsoft.Extensions.Logging;
56

67
namespace CustomCommands;

CustomCommands/Interfaces/IPluginGlobals.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface IPluginGlobals
77
List<CenterClientElement> centerClientOn { get; set; }
88
CenterServerElement centerServerOn { get; set; }
99
CustomCommandsConfig Config { get; set; }
10+
List<CooldownTimer> CooldownTimer { get; set; }
1011
}

CustomCommands/Interfaces/IPluginUtilities.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public interface IPluginUtilities
99
string[] SplitStringByCommaOrSemicolon(string str);
1010
void ExecuteServerCommands(Commands cmd, CCSPlayerController player);
1111
bool RequiresPermissions(CCSPlayerController player, Permission permissions);
12+
bool OnCooldown(CCSPlayerController player, Commands cmd);
13+
void SetCooldown(CCSPlayerController player, Commands cmd);
1214
}

CustomCommands/Model/CommandsConfig.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22

33
public class Commands
44
{
5+
public Guid ID { get; set; } = Guid.NewGuid();
56
public string Title { get; set; } = "";
67
public string Description { get; set; } = "Description";
78
public string Command { get; set; } = "";
9+
public dynamic Cooldown { get; set; } = 0;
810
public dynamic Message { get; set; } = "";
911
public CenterElement CenterMessage { get; set; } = new();
1012
public Sender PrintTo { get; set; } = Sender.ClientChat;
1113
public List<string> ServerCommands { get; set; } = new();
1214
public Permission Permission { get; set; } = new();
15+
16+
}
17+
public class Cooldown
18+
{
19+
public int CooldownTime { get; set; } = 0;
20+
public bool IsGlobal { get; set; } = false;
21+
public string CooldownMessage { get; set; } = "";
1322
}
1423
public class Permission
1524
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CustomCommands.Model;
2+
3+
public class CooldownTimer
4+
{
5+
public bool IsGlobal { get; set; } = false;
6+
public int PlayerID { get; set; }
7+
public required Guid CommandID { get; set; }
8+
public required DateTime CooldownTime { get; set; }
9+
}

CustomCommands/Services/EventManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public EventManager(IPluginGlobals PluginGlobals, IPluginContext PluginContext,
2424
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo _)
2525
{
2626
PluginGlobals.centerClientOn.RemoveAll(p => p.ClientId == @event.Userid.UserId);
27+
PluginGlobals.CooldownTimer.RemoveAll(p => p.PlayerID == @event.Userid.UserId);
2728

2829
return HookResult.Continue;
2930
}

CustomCommands/Services/PluginGlobals.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public class PluginGlobals : IPluginGlobals
77
public List<CenterClientElement> centerClientOn { get; set; } = new();
88
public CenterServerElement centerServerOn { get; set; } = new();
99
public CustomCommandsConfig Config { get; set; } = new();
10+
public List<CooldownTimer> CooldownTimer { get; set; } = new();
1011
}

CustomCommands/Services/PluginUtilities.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using System.Text.RegularExpressions;
23
using CounterStrikeSharp.API;
34
using CounterStrikeSharp.API.Core;
@@ -56,4 +57,63 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio
5657
return true;
5758
}
5859
}
60+
public bool OnCooldown(CCSPlayerController player, Commands cmd)
61+
{
62+
63+
64+
}
65+
/// <summary>
66+
/// Sets the cooldown for the command
67+
/// </summary>
68+
/// <param name="player">Need to add the player if the Cooldown is only for a specific player</param>
69+
/// <param name="cmd"></param>
70+
public void SetCooldown(CCSPlayerController player, Commands cmd)
71+
{
72+
if (cmd.Cooldown is JsonElement jsonElement)
73+
{
74+
switch (jsonElement.ValueKind)
75+
{
76+
case JsonValueKind.Number:
77+
int cooldown = (int)cmd.Cooldown;
78+
79+
if (cooldown == 0)
80+
break;
81+
82+
var timer = new CooldownTimer() {
83+
IsGlobal = false,
84+
PlayerID = player.UserId ?? 0,
85+
CommandID = cmd.ID,
86+
CooldownTime = DateTime.Now.AddSeconds(cooldown)
87+
};
88+
PluginGlobals.CooldownTimer.Add(timer);
89+
90+
break;
91+
case JsonValueKind.Object:
92+
Cooldown cooldownObject = (Cooldown)cmd.Cooldown;
93+
94+
if (cooldownObject.IsGlobal)
95+
{
96+
var timerObj = new CooldownTimer() {
97+
IsGlobal = true,
98+
CommandID = cmd.ID,
99+
CooldownTime = DateTime.Now.AddSeconds(cooldownObject.CooldownTime)
100+
};
101+
PluginGlobals.CooldownTimer.Add(timerObj);
102+
} else {
103+
var timerObj = new CooldownTimer() {
104+
IsGlobal = false,
105+
PlayerID = player.UserId ?? 0,
106+
CommandID = cmd.ID,
107+
CooldownTime = DateTime.Now.AddSeconds(cooldownObject.CooldownTime)
108+
};
109+
PluginGlobals.CooldownTimer.Add(timerObj);
110+
}
111+
112+
break;
113+
114+
default:
115+
break;
116+
}
117+
}
118+
}
59119
}

CustomCommands/Services/RegisterCommands.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ public void AddCommands(Commands com)
3838
if (com.Permission.PermissionList.Count > 0 && com.Permission != null)
3939
if (!PluginUtilities.RequiresPermissions(player, com.Permission))
4040
return;
41-
41+
42+
if(PluginUtilities.OnCooldown(player, com))
43+
return;
44+
45+
PluginUtilities.SetCooldown(player, com);
46+
4247
MessageManager.SendMessage(player, com);
4348

4449
PluginUtilities.ExecuteServerCommands(com, player);

Examples/Cooldown.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
//Simple cooldown command
3+
{
4+
"Title": "SimpleCooldown",
5+
"Description": "Cooldown example command",
6+
"Command": "cooldown",
7+
"Cooldown": 10, // Cooldown in seconds
8+
"Message": "Cool cooldown message!",
9+
"PrintTo": 0
10+
},
11+
//Advanced cooldown command
12+
{
13+
"Title": "AdvCooldown",
14+
"Description": "Cooldown example command",
15+
"Command": "cooldown",
16+
"Cooldown": {
17+
"CooldownTime": 5,
18+
"IsGlobal": false, // If true, cooldown will be global for all users
19+
"CooldownMessage": "This command is on cooldown for {0} more seconds!"
20+
},
21+
"Message": "Cool cooldown message!",
22+
"PrintTo": 0
23+
}
24+
]

0 commit comments

Comments
 (0)