Skip to content

Commit da933b9

Browse files
authored
Merge pull request #34 from HerrMagiic/feature/toggleservercommands
Feature/toggleservercommands
2 parents 383342e + a4983c1 commit da933b9

File tree

12 files changed

+85
-11
lines changed

12 files changed

+85
-11
lines changed

CustomCommands/CustomCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CustomCommands;
99
public partial class CustomCommands : BasePlugin, IPluginConfig<CustomCommandsConfig>
1010
{
1111
public override string ModuleName => "CustomCommands";
12-
public override string ModuleVersion => "1.1.0";
12+
public override string ModuleVersion => "2.0.0";
1313
public override string ModuleAuthor => "HerrMagic";
1414
public override string ModuleDescription => "Create your own commands per config";
1515

CustomCommands/Interfaces/ICooldownManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using CounterStrikeSharp.API.Core;
32
using CustomCommands.Model;
43

CustomCommands/Interfaces/IPluginGlobals.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using CounterStrikeSharp.API.Core;
21
using CustomCommands.Model;
32

43
namespace CustomCommands.Interfaces;

CustomCommands/Interfaces/IPluginUtilities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using CounterStrikeSharp.API.Core;
32
using CustomCommands.Model;
43

CustomCommands/Model/CommandsConfig.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class Commands
1212
public Sender PrintTo { get; set; } = Sender.ClientChat;
1313
public List<string> ServerCommands { get; set; } = new();
1414
public Permission Permission { get; set; } = new();
15-
1615
}
1716
public class Cooldown
1817
{

CustomCommands/Services/CooldownManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using System.Text.Json;
32
using CounterStrikeSharp.API.Core;
43
using CustomCommands.Model;

CustomCommands/Services/EventManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using CounterStrikeSharp.API;
32
using CounterStrikeSharp.API.Core;
43
using CounterStrikeSharp.API.Core.Attributes.Registration;

CustomCommands/Services/PluginUtilities.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
using System.Text.Json;
21
using System.Text.RegularExpressions;
32
using CounterStrikeSharp.API;
43
using CounterStrikeSharp.API.Core;
54
using CounterStrikeSharp.API.Modules.Admin;
5+
using CounterStrikeSharp.API.Modules.Cvars;
66
using CustomCommands.Interfaces;
77
using CustomCommands.Model;
8+
using Microsoft.Extensions.Logging;
89

910
namespace CustomCommands.Services;
1011

1112
public class PluginUtilities : IPluginUtilities
1213
{
1314
private readonly IPluginGlobals PluginGlobals;
1415
private readonly IReplaceTagsFunctions ReplaceTagsFunctions;
16+
private readonly ILogger<CustomCommands> Logger;
1517

16-
public PluginUtilities(IPluginGlobals PluginGlobals, IReplaceTagsFunctions ReplaceTagsFunctions)
18+
public PluginUtilities(IPluginGlobals PluginGlobals, IReplaceTagsFunctions ReplaceTagsFunctions,
19+
ILogger<CustomCommands> Logger)
1720
{
1821
this.PluginGlobals = PluginGlobals;
1922
this.ReplaceTagsFunctions = ReplaceTagsFunctions;
23+
this.Logger = Logger;
2024
}
2125

2226
public string[] SplitStringByCommaOrSemicolon(string str)
@@ -25,16 +29,53 @@ public string[] SplitStringByCommaOrSemicolon(string str)
2529
.Where(s => !string.IsNullOrEmpty(s))
2630
.ToArray();
2731
}
28-
32+
/// <summary>
33+
/// Executes the server commands from the command object
34+
/// </summary>
35+
/// <param name="cmd"></param>
36+
/// <param name="player"></param>
2937
public void ExecuteServerCommands(Commands cmd, CCSPlayerController player)
3038
{
3139
if (cmd.ServerCommands.Count == 0) return;
3240

3341
foreach (var serverCommand in cmd.ServerCommands)
3442
{
43+
// If the command starts with "toggle" we want to toggle the cvar
44+
if (serverCommand.StartsWith("toggle"))
45+
{
46+
HandleToggleCommand(serverCommand);
47+
continue;
48+
}
49+
3550
Server.ExecuteCommand(ReplaceTagsFunctions.ReplaceMessageTags(serverCommand, player));
3651
}
3752
}
53+
/// <summary>
54+
/// Handles the toggle command
55+
/// </summary>
56+
/// <param name="serverCommand"></param>
57+
private void HandleToggleCommand(string serverCommand)
58+
{
59+
string commandWithoutToggle = serverCommand.Replace("toggle ", "");
60+
var commandCvar = ConVar.Find(commandWithoutToggle);
61+
if (commandCvar != null)
62+
{
63+
if(commandCvar.GetPrimitiveValue<bool>())
64+
Server.ExecuteCommand($"{commandWithoutToggle} 0");
65+
else
66+
Server.ExecuteCommand($"{commandWithoutToggle} 1");
67+
}
68+
else
69+
{
70+
Logger.LogError($"Couldn't toggle {commandWithoutToggle}. Please check if this command is toggleable");
71+
}
72+
}
73+
/// <summary>
74+
/// Checks if the player has the required permissions to execute the command
75+
/// </summary>
76+
/// <param name="player"></param>
77+
/// <param name="permissions"></param>
78+
/// <returns></returns>
3879
public bool RequiresPermissions(CCSPlayerController player, Permission permissions)
3980
{
4081
if (!permissions.RequiresAllPermissions)

CustomCommands/Services/ReplaceTagsFunction.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public string ReplaceLanguageTags(string input)
5555
{
5656
// Return the group captured in the regex, which is the string after "="
5757
string lang = match.Groups[1].Value;
58-
5958
return input.Replace(match.Value, plugin.Localizer[lang] ?? "<LANG in CustomCommands/lang/<language.json> not found>");
6059
}
6160
else

CustomCommands/test.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
* [ ] check if the string pattern check works
66
* [ ] check cooldown funktion for normal int and than object
77
* [ ] check if padleft color tag works now
8+
* [ ] If this works remove all spaced in the example files with color tags
89
* [ ] check if command args works
10+
* [ ] check if duplicated commands still wont work with this feature
11+
* [ ] check if toggle feature works

0 commit comments

Comments
 (0)