1- using System . Text . Json ;
21using System . Text . RegularExpressions ;
32using CounterStrikeSharp . API ;
43using CounterStrikeSharp . API . Core ;
54using CounterStrikeSharp . API . Modules . Admin ;
5+ using CounterStrikeSharp . API . Modules . Cvars ;
66using CustomCommands . Interfaces ;
77using CustomCommands . Model ;
8+ using Microsoft . Extensions . Logging ;
89
910namespace CustomCommands . Services ;
1011
1112public 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 )
0 commit comments