Skip to content

Commit 0a630b1

Browse files
authored
Merge pull request #31 from HerrMagiic/feature/Cooldown
Feature/cooldown
2 parents 7720a3c + ba735fd commit 0a630b1

File tree

12 files changed

+166
-2
lines changed

12 files changed

+166
-2
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ public interface IPluginUtilities
99
string[] SplitStringByCommaOrSemicolon(string str);
1010
void ExecuteServerCommands(Commands cmd, CCSPlayerController player);
1111
bool RequiresPermissions(CCSPlayerController player, Permission permissions);
12+
bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd);
13+
void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int cooldownTime);
14+
void SetCooldown(CCSPlayerController player, Commands cmd);
1215
}

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: 105 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,108 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio
5657
return true;
5758
}
5859
}
60+
61+
/// <summary>
62+
/// Checks if the command is on cooldown
63+
/// </summary>
64+
/// <param name="player"></param>
65+
/// <param name="cmd"></param>
66+
/// <returns></returns>
67+
public bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd)
68+
{
69+
// Check global cooldown
70+
if (IsCommandOnCooldownWithCondition(x => x.IsGlobal == true && x.CommandID == cmd.ID, player, cmd))
71+
return true;
72+
73+
// Check player cooldown
74+
if (IsCommandOnCooldownWithCondition(x => x.PlayerID == player.UserId && x.CommandID == cmd.ID, player, cmd))
75+
return true;
76+
77+
return false;
78+
}
79+
80+
private bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate, CCSPlayerController player, Commands cmd)
81+
{
82+
int index = PluginGlobals.CooldownTimer.FindIndex(x => predicate(x) && x.CooldownTime > DateTime.Now);
83+
84+
if (index != -1)
85+
{
86+
string timeleft = PluginGlobals.CooldownTimer[index].CooldownTime.Subtract(DateTime.Now).Seconds.ToString();
87+
player.PrintToChat($"{PluginGlobals.Config.Prefix}{cmd.Cooldown.CooldownMessage.Replace("{TIME}", timeleft)
88+
?? $"This command is for {timeleft} seconds on cooldown"}");
89+
90+
return true;
91+
}
92+
93+
return false;
94+
}
95+
96+
/// <summary>
97+
/// Adds the command to the cooldown list
98+
/// </summary>
99+
/// <param name="isGlobal"></param>
100+
/// <param name="playerID"></param>
101+
/// <param name="commandID"></param>
102+
/// <param name="cooldownTime"></param>
103+
public void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int cooldownTime)
104+
{
105+
var timer = new CooldownTimer() {
106+
IsGlobal = isGlobal,
107+
CommandID = commandID,
108+
CooldownTime = DateTime.Now.AddSeconds(cooldownTime)
109+
};
110+
111+
if (isGlobal)
112+
{
113+
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
114+
x.IsGlobal == true
115+
&& x.CommandID == commandID);
116+
if (index != -1)
117+
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
118+
else
119+
PluginGlobals.CooldownTimer.Add(timer);
120+
}
121+
else
122+
{
123+
timer.PlayerID = playerID;
124+
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
125+
x.PlayerID == playerID
126+
&& x.CommandID == commandID);
127+
if (index != -1)
128+
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
129+
else
130+
PluginGlobals.CooldownTimer.Add(timer);
131+
}
132+
}
133+
134+
/// <summary>
135+
/// Sets the cooldown for the command
136+
/// </summary>
137+
/// <param name="player">Need to add the player if the Cooldown is only for a specific player</param>
138+
/// <param name="cmd"></param>
139+
public void SetCooldown(CCSPlayerController player, Commands cmd)
140+
{
141+
if (cmd.Cooldown is JsonElement jsonElement)
142+
{
143+
switch (jsonElement.ValueKind)
144+
{
145+
case JsonValueKind.Number:
146+
int cooldown = (int)cmd.Cooldown;
147+
if (cooldown == 0)
148+
break;
149+
150+
AddToCooldownList(false, player.UserId ?? 0, cmd.ID, cooldown);
151+
break;
152+
153+
case JsonValueKind.Object:
154+
Cooldown cooldownObject = (Cooldown)cmd.Cooldown;
155+
156+
AddToCooldownList(cooldownObject.IsGlobal, player.UserId ?? 0, cmd.ID, cooldownObject.CooldownTime);
157+
break;
158+
159+
default:
160+
break;
161+
}
162+
}
163+
}
59164
}

CustomCommands/Services/RegisterCommands.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ 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.IsCommandOnCooldown(player, com)) return;
43+
44+
PluginUtilities.SetCooldown(player, com);
45+
4246
MessageManager.SendMessage(player, com);
4347

4448
PluginUtilities.ExecuteServerCommands(com, player);

CustomCommands/Services/ReplaceTagsFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public string ReplaceMessageTags(string input, CCSPlayerController player)
7272
{"{TIME}", DateTime.Now.ToString("HH:mm:ss") ?? "<TIME not found>"},
7373
{"{DATE}", DateTime.Now.ToString("dd.MM.yyyy") ?? "<DATE not found>"},
7474
{"{PLAYERNAME}", player.PlayerName ?? "<PLAYERNAME not found>"},
75-
{"{USERID}", player.UserId.ToString() ?? "<USERID not found>"},
75+
{"{USERID}", player.Slot.ToString() ?? "<USERID not found>"},
7676
{"{STEAMID2}", steamId.SteamId2 ?? "<STEAMID2 not found>"},
7777
{"{STEAMID3}", steamId.SteamId3 ?? "<STEAMID3 not found>"},
7878
{"{STEAMID32}", steamId.SteamId32.ToString() ?? "<STEAMID32 not found>"},

0 commit comments

Comments
 (0)