@@ -26,83 +26,79 @@ public RegisterCommands(ILogger<CustomCommands> Logger, IMessageManager MessageM
26
26
this . CooldownManager = CooldownManager ;
27
27
}
28
28
29
- /// <summary>
30
- /// Adds custom commands to the plugin.
31
- /// </summary>
32
- /// <param name="cmd">The command to add.</param>
33
29
public void AddCommands ( Commands cmd )
34
30
{
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 ) ! ;
38
35
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 ) =>
40
39
{
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 ;
50
42
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 )
52
51
{
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 " ) ;
54
53
return ;
55
54
}
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 ) )
61
57
{
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 ;
68
60
}
69
61
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
+ }
74
71
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 ;
77
79
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 ) ;
80
82
81
- // Sending the message to the player
82
- MessageManager . SendMessage ( player , command ) ;
83
+ // Sending the message to the player
84
+ MessageManager . SendMessage ( player , command ) ;
83
85
84
- // Execute the server commands
85
- PluginUtilities . ExecuteServerCommands ( command , player ) ;
86
+ // Execute the server commands
87
+ PluginUtilities . ExecuteServerCommands ( command , player ) ;
86
88
87
- // Execute the client commands
88
- PluginUtilities . ExecuteClientCommands ( command , player ) ;
89
+ // Execute the client commands
90
+ PluginUtilities . ExecuteClientCommands ( command , player ) ;
89
91
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
+ } ) ;
94
95
}
95
96
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 ( )
102
98
{
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 > ( ) ;
106
102
107
103
foreach ( var cmd in comms )
108
104
{
@@ -120,7 +116,7 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
120
116
}
121
117
122
118
if ( duplicateCommands . Count == 0 )
123
- return comms ;
119
+ return ;
124
120
125
121
// Log the duplicate commands
126
122
Logger . LogError ( $ "------------------------------------------------------------------------") ;
@@ -136,10 +132,47 @@ public List<Commands> CheckForDuplicateCommands(List<Commands> comms)
136
132
continue ;
137
133
}
138
134
139
- commands . Add ( comms [ i ] ) ;
135
+ comms . Add ( comms [ i ] ) ;
140
136
}
141
137
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
+ }
142
175
143
- return commands ;
176
+ PluginGlobals . CustomCommands = newCmds ;
144
177
}
145
178
}
0 commit comments