@@ -26,83 +26,79 @@ public RegisterCommands(ILogger<CustomCommands> Logger, IMessageManager MessageM
2626 this . CooldownManager = CooldownManager ;
2727 }
2828
29- /// <summary>
30- /// Adds custom commands to the plugin.
31- /// </summary>
32- /// <param name="cmd">The command to add.</param>
3329 public void AddCommands ( Commands cmd )
3430 {
35- CustomCommands plugin = ( PluginContext . Plugin as CustomCommands ) ! ;
36-
37- string [ ] aliases = PluginUtilities . GettingCommandsFromString ( cmd . Command ) ;
31+ if ( ! cmd . IsRegisterable )
32+ return ;
33+
34+ var pluginContext = ( PluginContext . Plugin as CustomCommands ) ! ;
3835
39- for ( int i = 0 ; i < aliases . Length ; i ++ )
36+ pluginContext . AddCommand ( cmd . Command , cmd . Description ,
37+ [ CommandHelper ( whoCanExecute : CommandUsage . CLIENT_ONLY ) ]
38+ ( player , info ) =>
4039 {
41- string alias = aliases [ i ] ;
42- // System.Console.WriteLine($"Pre Command: {alias}.");
43- plugin . AddCommand ( alias , cmd . Description ,
44- [ CommandHelper ( whoCanExecute : CommandUsage . CLIENT_ONLY ) ]
45- ( player , info ) =>
46- {
47- if ( player == null )
48- return ;
49- System . Console . WriteLine ( $ "Post Command: { alias } .") ;
40+ if ( player == null )
41+ return ;
5042
51- if ( info . ArgCount < alias . Split ( ' ' ) . Length )
43+ var command = cmd ;
44+
45+ // Check if the command has arguments and if it does, check if the command exists and is the right one
46+ if ( info . ArgCount > 1 )
47+ {
48+ var findcommand = PluginGlobals . CustomCommands . Find ( x => x . Command == command . Command && x . Argument == info . ArgString ) ;
49+ // Check if the command is the right one with the right arguments
50+ if ( findcommand is null )
5251 {
53- player . PrintToChat ( $ "This command requires at least { alias . Split ( ' ' ) . Length - 1 } arguments. ") ;
52+ player . PrintToChat ( "This command requires no Arguments or you added to many " ) ;
5453 return ;
5554 }
56-
57- var command = cmd ;
58-
59- // Check if the command has arguments and if it does, check if the command really exists in the list
60- if ( info . ArgCount > 1 )
55+
56+ if ( ! findcommand . Argument ! . Equals ( info . ArgString ) )
6157 {
62- var findcommand = PluginGlobals . CustomCommands . Find ( x => x . Command . Contains ( alias + $ " { info . ArgString } ") ) ;
63- // Check if the command is equal to the found command
64- if ( findcommand ! != command )
65- return ;
66-
67- command = findcommand ;
58+ player . PrintToChat ( "Wrong Arguments" ) ;
59+ return ;
6860 }
6961
70- // Check if the player has the permission to use the command
71- if ( command ! . Permission . PermissionList . Count > 0 && command . Permission != null )
72- if ( ! PluginUtilities . RequiresPermissions ( player , command . Permission ) )
73- return ;
62+ command = findcommand ;
63+ }
64+
65+ // This will exit the command if the command has arguments but the client didn't provide any
66+ if ( info . ArgCount <= 1 && ! string . IsNullOrEmpty ( command . Argument ! ) )
67+ {
68+ player . PrintToChat ( "This command requires Arguments" ) ;
69+ return ;
70+ }
7471
75- // Check if the command is on cooldown
76- if ( CooldownManager . IsCommandOnCooldown ( player , command ) ) return ;
72+ // Check if the player has the permission to use the command
73+ if ( command ! . Permission ? . PermissionList . Count > 0 && command . Permission != null )
74+ if ( ! PluginUtilities . RequiresPermissions ( player , command . Permission ) )
75+ return ;
76+
77+ // Check if the command is on cooldown
78+ if ( CooldownManager . IsCommandOnCooldown ( player , command ) ) return ;
7779
78- // Set the cooldown for the command if it has a cooldown set
79- CooldownManager . SetCooldown ( player , command ) ;
80+ // Set the cooldown for the command if it has a cooldown set
81+ CooldownManager . SetCooldown ( player , command ) ;
8082
81- // Sending the message to the player
82- MessageManager . SendMessage ( player , command ) ;
83+ // Sending the message to the player
84+ MessageManager . SendMessage ( player , command ) ;
8385
84- // Execute the server commands
85- PluginUtilities . ExecuteServerCommands ( command , player ) ;
86+ // Execute the server commands
87+ PluginUtilities . ExecuteServerCommands ( command , player ) ;
8688
87- // Execute the client commands
88- PluginUtilities . ExecuteClientCommands ( command , player ) ;
89+ // Execute the client commands
90+ PluginUtilities . ExecuteClientCommands ( command , player ) ;
8991
90- // Execute the client commands from the server
91- PluginUtilities . ExecuteClientCommandsFromServer ( command , player ) ;
92- } ) ;
93- }
92+ // Execute the client commands from the server
93+ PluginUtilities . ExecuteClientCommandsFromServer ( command , player ) ;
94+ } ) ;
9495 }
9596
96- /// <summary>
97- /// Checks for duplicate commands in the provided list and removes them.
98- /// </summary>
99- /// <param name="comms">The list of commands to check for duplicates.</param>
100- /// <returns>A new list of commands without any duplicates.</returns>
101- public List < Commands > CheckForDuplicateCommands ( List < Commands > comms )
97+ public void CheckForDuplicateCommands ( )
10298 {
103- List < Commands > duplicateCommands = new ( ) ;
104- List < Commands > commands = new ( ) ;
105- List < string > commandNames = new ( ) ;
99+ var comms = PluginGlobals . CustomCommands ;
100+ var duplicateCommands = new List < Commands > ( ) ;
101+ var commandNames = new List < string > ( ) ;
106102
107103 foreach ( var cmd in comms )
108104 {
@@ -120,7 +116,7 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
120116 }
121117
122118 if ( duplicateCommands . Count == 0 )
123- return comms ;
119+ return ;
124120
125121 // Log the duplicate commands
126122 Logger . LogError ( $ "------------------------------------------------------------------------") ;
@@ -136,10 +132,47 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
136132 continue ;
137133 }
138134
139- commands . Add ( comms [ i ] ) ;
135+ comms . Add ( comms [ i ] ) ;
140136 }
141137 Logger . LogError ( $ "------------------------------------------------------------------------") ;
138+ }
139+
140+ public void ConvertingCommandsForRegister ( )
141+ {
142+ var newCmds = new List < Commands > ( ) ;
143+
144+ foreach ( var cmd in PluginGlobals . CustomCommands )
145+ {
146+ var splitCommands = PluginUtilities . SplitStringByCommaOrSemicolon ( cmd . Command ) ;
147+ splitCommands = PluginUtilities . AddCSSTagsToAliases ( splitCommands . ToList ( ) ) ;
148+
149+ foreach ( var split in splitCommands )
150+ {
151+ var args = split . Split ( ' ' ) ;
152+
153+ if ( args . Length == 1 )
154+ {
155+ var newCmd = cmd . Clone ( ) as Commands ;
156+ newCmd ! . ID = Guid . NewGuid ( ) ;
157+ newCmd . Command = split . Trim ( ) ;
158+ newCmds . Add ( newCmd ) ;
159+ }
160+ else if ( args . Length > 1 )
161+ {
162+ var newCmd = cmd . Clone ( ) as Commands ;
163+
164+ if ( newCmds . Any ( p => p . Command . Contains ( args [ 0 ] ) ) )
165+ newCmd ! . IsRegisterable = false ;
166+
167+ newCmd ! . ID = Guid . NewGuid ( ) ;
168+ newCmd . Command = args [ 0 ] . Trim ( ) ;
169+ args [ 0 ] = "" ;
170+ newCmd . Argument = string . Join ( " " , args ) . Trim ( ) ;
171+ newCmds . Add ( newCmd ) ;
172+ }
173+ }
174+ }
142175
143- return commands ;
176+ PluginGlobals . CustomCommands = newCmds ;
144177 }
145178}
0 commit comments