Skip to content

Commit ba735fd

Browse files
committed
Added Cooldown functionality
1 parent e3d881a commit ba735fd

File tree

5 files changed

+86
-35
lines changed

5 files changed

+86
-35
lines changed

CustomCommands/Interfaces/IPluginUtilities.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +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 OnCooldown(CCSPlayerController player, Commands cmd);
12+
bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd);
13+
void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int cooldownTime);
1314
void SetCooldown(CCSPlayerController player, Commands cmd);
1415
}

CustomCommands/Services/PluginUtilities.cs

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,80 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio
5757
return true;
5858
}
5959
}
60-
public bool OnCooldown(CCSPlayerController player, Commands cmd)
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)
6168
{
62-
63-
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;
6494
}
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+
65134
/// <summary>
66135
/// Sets the cooldown for the command
67136
/// </summary>
@@ -75,40 +144,16 @@ public void SetCooldown(CCSPlayerController player, Commands cmd)
75144
{
76145
case JsonValueKind.Number:
77146
int cooldown = (int)cmd.Cooldown;
78-
79147
if (cooldown == 0)
80148
break;
81149

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-
150+
AddToCooldownList(false, player.UserId ?? 0, cmd.ID, cooldown);
90151
break;
152+
91153
case JsonValueKind.Object:
92154
Cooldown cooldownObject = (Cooldown)cmd.Cooldown;
93155

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-
156+
AddToCooldownList(cooldownObject.IsGlobal, player.UserId ?? 0, cmd.ID, cooldownObject.CooldownTime);
112157
break;
113158

114159
default:

CustomCommands/Services/RegisterCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public void AddCommands(Commands com)
3939
if (!PluginUtilities.RequiresPermissions(player, com.Permission))
4040
return;
4141

42-
if(PluginUtilities.OnCooldown(player, com))
43-
return;
42+
if(PluginUtilities.IsCommandOnCooldown(player, com)) return;
4443

4544
PluginUtilities.SetCooldown(player, com);
4645

CustomCommands/test.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Test this:
2+
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

Examples/Cooldown.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"Description": "Cooldown example command",
1515
"Command": "cooldown",
1616
"Cooldown": {
17-
"CooldownTime": 5,
17+
"CooldownTime": 5, // required, Cooldown in seconds
1818
"IsGlobal": false, // If true, cooldown will be global for all users
19-
"CooldownMessage": "This command is on cooldown for {0} more seconds!"
19+
"CooldownMessage": "This command is on cooldown for {TIME} more seconds!"
2020
},
2121
"Message": "Cool cooldown message!",
2222
"PrintTo": 0

0 commit comments

Comments
 (0)