Skip to content

Commit 98c04a3

Browse files
authored
Merge pull request #32 from HerrMagiic/feature/colorbugfix
Fixing problem with color tag at start of string
2 parents 0a630b1 + f7c0c5b commit 98c04a3

File tree

7 files changed

+173
-101
lines changed

7 files changed

+173
-101
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
using CounterStrikeSharp.API.Core;
3+
using CustomCommands.Model;
4+
5+
namespace CustomCommands.Interfaces;
6+
7+
public interface ICooldownManager
8+
{
9+
bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd);
10+
void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int cooldownTime);
11+
bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate, CCSPlayerController player, Commands cmd);
12+
void SetCooldown(CCSPlayerController player, Commands cmd);
13+
}

CustomCommands/Interfaces/IPluginUtilities.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ 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);
12+
string PadLeftColorTag(string input);
1513
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
using System.Text.Json;
3+
using CounterStrikeSharp.API.Core;
4+
using CustomCommands.Model;
5+
6+
namespace CustomCommands.Interfaces;
7+
8+
public class CooldownManager : ICooldownManager
9+
{
10+
public IPluginGlobals PluginGlobals { get; }
11+
public CooldownManager(IPluginGlobals PluginGlobals)
12+
{
13+
this.PluginGlobals = PluginGlobals;
14+
}
15+
16+
/// <summary>
17+
/// Checks if the command is on cooldown
18+
/// </summary>
19+
/// <param name="player"></param>
20+
/// <param name="cmd"></param>
21+
/// <returns></returns>
22+
public bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd)
23+
{
24+
// Check global cooldown
25+
if (IsCommandOnCooldownWithCondition(x => x.IsGlobal == true && x.CommandID == cmd.ID, player, cmd))
26+
return true;
27+
28+
// Check player cooldown
29+
if (IsCommandOnCooldownWithCondition(x => x.PlayerID == player.UserId && x.CommandID == cmd.ID, player, cmd))
30+
return true;
31+
32+
return false;
33+
}
34+
35+
public bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate, CCSPlayerController player, Commands cmd)
36+
{
37+
int index = PluginGlobals.CooldownTimer.FindIndex(x => predicate(x) && x.CooldownTime > DateTime.Now);
38+
39+
if (index != -1)
40+
{
41+
string timeleft = PluginGlobals.CooldownTimer[index].CooldownTime.Subtract(DateTime.Now).Seconds.ToString();
42+
player.PrintToChat($"{PluginGlobals.Config.Prefix}{cmd.Cooldown.CooldownMessage.Replace("{TIME}", timeleft)
43+
?? $"This command is for {timeleft} seconds on cooldown"}");
44+
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
51+
/// <summary>
52+
/// Adds the command to the cooldown list
53+
/// </summary>
54+
/// <param name="isGlobal"></param>
55+
/// <param name="playerID"></param>
56+
/// <param name="commandID"></param>
57+
/// <param name="cooldownTime"></param>
58+
public void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int cooldownTime)
59+
{
60+
var timer = new CooldownTimer() {
61+
IsGlobal = isGlobal,
62+
CommandID = commandID,
63+
CooldownTime = DateTime.Now.AddSeconds(cooldownTime)
64+
};
65+
66+
if (isGlobal)
67+
{
68+
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
69+
x.IsGlobal == true
70+
&& x.CommandID == commandID);
71+
if (index != -1)
72+
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
73+
else
74+
PluginGlobals.CooldownTimer.Add(timer);
75+
}
76+
else
77+
{
78+
timer.PlayerID = playerID;
79+
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
80+
x.PlayerID == playerID
81+
&& x.CommandID == commandID);
82+
if (index != -1)
83+
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
84+
else
85+
PluginGlobals.CooldownTimer.Add(timer);
86+
}
87+
}
88+
89+
/// <summary>
90+
/// Sets the cooldown for the command
91+
/// </summary>
92+
/// <param name="player">Need to add the player if the Cooldown is only for a specific player</param>
93+
/// <param name="cmd"></param>
94+
public void SetCooldown(CCSPlayerController player, Commands cmd)
95+
{
96+
if (cmd.Cooldown is JsonElement jsonElement)
97+
{
98+
switch (jsonElement.ValueKind)
99+
{
100+
case JsonValueKind.Number:
101+
int cooldown = (int)cmd.Cooldown;
102+
if (cooldown == 0)
103+
break;
104+
105+
AddToCooldownList(false, player.UserId ?? 0, cmd.ID, cooldown);
106+
break;
107+
108+
case JsonValueKind.Object:
109+
Cooldown cooldownObject = (Cooldown)cmd.Cooldown;
110+
111+
AddToCooldownList(cooldownObject.IsGlobal, player.UserId ?? 0, cmd.ID, cooldownObject.CooldownTime);
112+
break;
113+
114+
default:
115+
break;
116+
}
117+
}
118+
}
119+
}

CustomCommands/Services/PluginUtilities.cs

Lines changed: 27 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -57,108 +57,41 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio
5757
return true;
5858
}
5959
}
60-
6160
/// <summary>
62-
/// Checks if the command is on cooldown
61+
/// Moves the color tag one space the the left if it's not already there.
62+
/// Because the game doesn't support color tags at the start of a string.
6363
/// </summary>
64-
/// <param name="player"></param>
65-
/// <param name="cmd"></param>
64+
/// <param name="input"></param>
6665
/// <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)
66+
public string PadLeftColorTag(string input)
10467
{
105-
var timer = new CooldownTimer() {
106-
IsGlobal = isGlobal,
107-
CommandID = commandID,
108-
CooldownTime = DateTime.Now.AddSeconds(cooldownTime)
68+
string[] colorTagList = new string[] {
69+
"{DEFAULT}",
70+
"{WHITE}",
71+
"{DARKRED}",
72+
"{RED}",
73+
"{LIGHTRED}",
74+
"{GREEN}",
75+
"{LIME}",
76+
"{OLIVE}",
77+
"{ORANGE}",
78+
"{GOLD}",
79+
"{YELLOW}",
80+
"{BLUE}",
81+
"{DARKBLUE}",
82+
"{LIGHTPURPLE}",
83+
"{PURPLE}",
84+
"{SILVER}",
85+
"{BLUEGREY}",
86+
"{GREY}",
10987
};
110-
111-
if (isGlobal)
88+
foreach (var colorTag in colorTagList)
11289
{
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)
90+
if (input.StartsWith(colorTag))
14491
{
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;
92+
return " " + input;
16193
}
16294
}
95+
return input;
16396
}
16497
}

CustomCommands/Services/RegisterCommands.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ public class RegisterCommands : IRegisterCommands
1111
private readonly IPluginGlobals PluginGlobals;
1212
private readonly PluginContext PluginContext;
1313
private readonly IPluginUtilities PluginUtilities;
14+
private readonly ICooldownManager CooldownManager;
1415

1516
public RegisterCommands(ILogger<CustomCommands> Logger, IMessageManager MessageManager,
1617
IPluginGlobals PluginGlobals, IPluginContext PluginContext,
17-
IPluginUtilities PluginUtilities)
18+
IPluginUtilities PluginUtilities, ICooldownManager CooldownManager)
1819
{
1920
this.Logger = Logger;
2021
this.MessageManager = MessageManager;
2122
this.PluginGlobals = PluginGlobals;
2223
this.PluginContext = (PluginContext as PluginContext)!;
2324
this.PluginUtilities = PluginUtilities;
25+
this.CooldownManager = CooldownManager;
2426
}
2527

2628
public void AddCommands(Commands com)
@@ -39,9 +41,9 @@ public void AddCommands(Commands com)
3941
if (!PluginUtilities.RequiresPermissions(player, com.Permission))
4042
return;
4143

42-
if(PluginUtilities.IsCommandOnCooldown(player, com)) return;
44+
if(CooldownManager.IsCommandOnCooldown(player, com)) return;
4345

44-
PluginUtilities.SetCooldown(player, com);
46+
CooldownManager.SetCooldown(player, com);
4547

4648
MessageManager.SendMessage(player, com);
4749

CustomCommands/Services/ReplaceTagsFunction.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ 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;
1819

19-
public ReplaceTagsFunctions(IPluginGlobals PluginGlobals, IPluginContext PluginContext, ILogger<CustomCommands> Logger)
20+
public ReplaceTagsFunctions(IPluginGlobals PluginGlobals, IPluginContext PluginContext,
21+
ILogger<CustomCommands> Logger, IPluginUtilities PluginUtilities)
2022
{
2123
this.PluginGlobals = PluginGlobals;
2224
this.PluginContext = (PluginContext as PluginContext)!;
2325
this.Logger = Logger;
26+
this.PluginUtilities = PluginUtilities;
2427
}
2528

2629
public string[] ReplaceTags(string[] input, CCSPlayerController player)
@@ -93,6 +96,9 @@ public string ReplaceMessageTags(string input, CCSPlayerController player)
9396

9497
public string ReplaceColorTags(string input)
9598
{
99+
// PadLeft the color tag if it's not already there because the game doesn't support color tags at the start of a string.
100+
input = PluginUtilities.PadLeftColorTag(input);
101+
96102
Dictionary<string, string> replacements = new()
97103
{
98104
{"{DEFAULT}", $"{ChatColors.Default}"},

CustomCommands/test.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
* [ ] check if USERID tag is the right one from the console
55
* [ ] check if the string pattern check works
66
* [ ] check cooldown funktion for normal int and than object
7+
* [ ] check if padleft color tag works now

0 commit comments

Comments
 (0)