@@ -16,11 +16,15 @@ public class RegisteredCommand
1616 public string Command ;
1717 public string Description ;
1818 public Action < WFPlayer , string [ ] > Callback ;
19- public RegisteredCommand ( string command , string description , Action < WFPlayer , string [ ] > callback )
19+ public string [ ] Aliases ;
20+
21+ public RegisteredCommand ( string command , string description , Action < WFPlayer , string [ ] > callback , string [ ] ? aliases = null )
2022 {
2123 Command = command . ToLower ( ) ; // make sure its lower case to not mess anything up
2224 Description = description ;
2325 Callback = callback ;
26+ Aliases = aliases ?? [ ] ;
27+ Aliases = [ .. Aliases . Select ( alias => alias . ToLower ( ) ) ] ;
2428 }
2529
2630 public void Invoke ( WFPlayer player , string [ ] args )
@@ -40,10 +44,10 @@ public WFPlayer GetPlayer(string playerIdent)
4044 // if there is no player with the username try to find someone with that fisher ID
4145 if ( selectedPlayer == null )
4246 selectedPlayer = AllPlayers . ToList ( ) . Find ( p => p . FisherID . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
43-
47+
4448 return selectedPlayer ;
4549 }
46-
50+
4751 public void RegisterDefaultCommands ( )
4852 {
4953 RegisterCommand ( "help" , ( player , args ) =>
@@ -58,7 +62,7 @@ public void RegisterDefaultCommands()
5862 } ) ;
5963 SetCommandDescription ( "help" , "Shows all commands" ) ;
6064
61- RegisterCommand ( "exit" , ( player , args ) =>
65+ RegisterCommand ( command : "exit" , aliases : [ "shutdown" ] , cb : ( player , args ) =>
6266 {
6367 // make sure the player is the host
6468 if ( player . SteamId != serverPlayer . SteamId )
@@ -79,7 +83,7 @@ public void RegisterDefaultCommands()
7983 string playerIdent = string . Join ( " " , args ) ;
8084 // try find a user with the username first
8185 var kickedplayer = GetPlayer ( playerIdent ) ;
82-
86+
8387 if ( kickedplayer == null && System . Text . RegularExpressions . Regex . IsMatch ( playerIdent , @"^7656119\d{10}$" ) )
8488 {
8589 // if it is a steam ID, try to find the player by steam ID
@@ -88,10 +92,10 @@ public void RegisterDefaultCommands()
8892 kickPlayer ( steamId ) ;
8993 return ;
9094 }
91-
95+
9296 if ( kickedplayer == null )
9397 {
94- messagePlayer ( "That's not a player!" , player . SteamId ) ;
98+ messagePlayer ( "That's not a player!" , player . SteamId ) ;
9599 }
96100 else
97101 {
@@ -109,14 +113,14 @@ public void RegisterDefaultCommands()
109113 string playerIdent = string . Join ( " " , args ) ;
110114 // try to find a user with the username first
111115 var playerToBan = GetPlayer ( playerIdent ) ;
112-
116+
113117 var previousPlayer = PreviousPlayers . ToList ( ) . Find ( p => p . FisherID . Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
114118 if ( previousPlayer != null )
115119 {
116120 messagePlayer ( $ "There is a previous player with that name, if you meant to ban them add a # before the ID: #{ playerIdent } ", player . SteamId ) ;
117121 return ;
118122 }
119-
123+
120124 previousPlayer = PreviousPlayers . ToList ( ) . Find ( p => $ "#{ p . FisherID } ". Equals ( playerIdent , StringComparison . OrdinalIgnoreCase ) ) ;
121125 if ( previousPlayer != null )
122126 {
@@ -126,7 +130,7 @@ public void RegisterDefaultCommands()
126130 Username = previousPlayer . Username ,
127131 } ;
128132 }
129-
133+
130134 // use regex to check if its a steam ID
131135 if ( playerToBan == null && System . Text . RegularExpressions . Regex . IsMatch ( playerIdent , @"^7656119\d{10}$" ) )
132136 {
@@ -136,11 +140,11 @@ public void RegisterDefaultCommands()
136140 banPlayer ( steamId ) ;
137141 else
138142 banPlayer ( steamId , true ) ;
139-
143+
140144 messagePlayer ( $ "Banned player with Steam ID { playerIdent } ", player . SteamId ) ;
141145 return ;
142146 }
143-
147+
144148 if ( playerToBan == null )
145149 {
146150 messagePlayer ( "Player not found!" , player . SteamId ) ;
@@ -159,7 +163,7 @@ public void RegisterDefaultCommands()
159163 } ) ;
160164 SetCommandDescription ( "ban" , "Bans a player from the server" ) ;
161165
162- RegisterCommand ( "prev" , ( player , args ) =>
166+ RegisterCommand ( command : "prev" , aliases : [ "recent" ] , cb : ( player , args ) =>
163167 {
164168 if ( ! isPlayerAdmin ( player . SteamId ) ) return ;
165169 var sb = new StringBuilder ( ) ;
@@ -174,7 +178,7 @@ public void RegisterDefaultCommands()
174178 {
175179 continue ;
176180 }
177-
181+
178182 // get the time since the player left in a human readable format
179183 string timeLeft =
180184 $ "{ Math . Round ( ( DateTime . UtcNow - DateTimeOffset . FromUnixTimeSeconds ( prevPlayer . leftTimestamp ) . UtcDateTime ) . TotalMinutes ) } minutes ago";
@@ -183,19 +187,25 @@ public void RegisterDefaultCommands()
183187 messagePlayer ( sb . ToString ( ) , player . SteamId ) ;
184188 } ) ;
185189 SetCommandDescription ( "prev" , "Shows a list of previous players that were connected to the server" ) ;
186-
190+
187191 }
188192
189- public void RegisterCommand ( string command , Action < WFPlayer , string [ ] > cb )
193+ public void RegisterCommand ( string command , Action < WFPlayer , string [ ] > cb , string [ ] ? aliases = null )
190194 {
195+ aliases ??= [ ] ;
191196
192197 if ( Commands . Any ( c => c . Command == command ) )
193198 {
194199 Log ( $ "Command '{ command } ' is already registerd!") ;
195200 return ;
196201 }
202+ else if ( aliases . Any ( alias => Commands . Find ( c => c . Aliases . Contains ( alias ) ) != null ) )
203+ {
204+ Log ( $ "'{ command } ' has an alias that is already registerd elsewhere!") ;
205+ return ;
206+ }
197207
198- Commands . Add ( new RegisteredCommand ( command , "" , cb ) ) ;
208+ Commands . Add ( new RegisteredCommand ( command , "" , cb , aliases ) ) ;
199209
200210 }
201211
@@ -217,7 +227,7 @@ public void SetCommandDescription(string command, string description)
217227
218228 public void InvokeCommand ( WFPlayer player , string command , string [ ] args )
219229 {
220- var cmd = Commands . Find ( c => c . Command == command ) ;
230+ var cmd = FindCommand ( command ) ;
221231 if ( cmd == null )
222232 {
223233 Log ( $ "Command '{ command } ' not found!") ;
@@ -228,11 +238,18 @@ public void InvokeCommand(WFPlayer player, string command, string[] args)
228238
229239 public bool DoseCommandExist ( string command )
230240 {
231- var cmd = Commands . Find ( c => c . Command == command ) ;
241+ var cmd = FindCommand ( command ) ;
232242 if ( cmd == null )
233243 return false ;
234244
235245 return true ;
236246 }
247+
248+ public RegisteredCommand ? FindCommand ( string name )
249+ {
250+ return Commands . Find ( c =>
251+ c . Command == name . ToLower ( ) || c . Aliases . Contains ( name . ToLower ( ) )
252+ ) ;
253+ }
237254 }
238255}
0 commit comments