1- using NitroxModel . DataStructures . Util ;
2- using NitroxModel . Helper ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Collections . ObjectModel ;
4+ using System . Linq ;
5+ using System . Text ;
36using NitroxModel . DataStructures . GameLogic ;
4- using NitroxModel . Packets ;
7+ using NitroxModel . DataStructures . Util ;
8+ using NitroxModel . Helper ;
59using NitroxModel . Logger ;
6- using System . Text ;
10+ using NitroxModel . Packets ;
711
812namespace NitroxServer . ConsoleCommands . Abstract
913{
10- public abstract class Command
14+ public abstract partial class Command
1115 {
12- public string Name { get ; protected set ; }
13- public string [ ] Alias { get ; protected set ; }
14- public string Description { get ; protected set ; }
15- public string ArgsDescription { get ; protected set ; }
16- public Perms RequiredPermLevel { get ; protected set ; }
16+ private readonly List < string > aliases ;
17+ private int optional , required ;
1718
18- protected Command ( string name , Perms requiredPermLevel , string argsDescription , string description ) : this ( name , requiredPermLevel , argsDescription , "" , null )
19- {
20- ArgsDescription = argsDescription ;
21- Name = name ;
22- Description = description ;
23- }
19+ public ReadOnlyCollection < string > Aliases => aliases . AsReadOnly ( ) ;
20+
21+ public string Name { get ; }
22+ public string Description { get ; }
23+ public Perms RequiredPermLevel { get ; }
24+ public bool AllowedArgOverflow { get ; }
25+ public List < IParameter < object > > Parameters { get ; }
2426
25- protected Command ( string name , Perms requiredPermLevel , string argsDescription , string description , string [ ] alias )
27+ protected Command ( string name , Perms perm , string description , bool allowedArgOveflow = false )
2628 {
2729 Validate . NotNull ( name ) ;
28- Validate . NotNull ( argsDescription ) ;
29- Validate . NotNull ( description ) ;
3030
3131 Name = name ;
32- Description = string . IsNullOrEmpty ( description ) ? "No description" : description ;
33- ArgsDescription = argsDescription ;
34- RequiredPermLevel = requiredPermLevel ;
35- Alias = alias ?? new string [ 0 ] ;
32+ RequiredPermLevel = perm ;
33+ aliases = new List < string > ( ) ;
34+ Parameters = new List < IParameter < object > > ( ) ;
35+ AllowedArgOverflow = allowedArgOveflow ;
36+ Description = string . IsNullOrEmpty ( description ) ? "No description provided" : description ;
3637 }
3738
38- public abstract void RunCommand ( string [ ] args , Optional < Player > sender ) ;
39+ protected abstract void Execute ( CallArgs args ) ;
3940
40- public abstract bool VerifyArgs ( string [ ] args ) ;
41-
42- public override string ToString ( )
41+ public void TryExecute ( Optional < Player > sender , string [ ] args )
4342 {
44- return $ "{ nameof ( Name ) } : { Name } , { nameof ( Description ) } : { Description } , { nameof ( ArgsDescription ) } : { ArgsDescription } , { nameof ( Alias ) } : [{ string . Join ( ", " , Alias ) } ]";
43+ if ( args . Length < required )
44+ {
45+ SendMessage ( sender , $ "Error: Invalid Parameters\n Usage: { ToHelpText ( true ) } ") ;
46+ return ;
47+ }
48+
49+ if ( ! AllowedArgOverflow && args . Length > optional + required )
50+ {
51+ SendMessage ( sender , $ "Error: Too many Parameters\n Usage: { ToHelpText ( true ) } ") ;
52+ return ;
53+ }
54+
55+ try
56+ {
57+ Execute ( new CallArgs ( this , sender , args ) ) ;
58+ }
59+ catch ( ArgumentException e )
60+ {
61+ SendMessage ( sender , $ "Error: { e . Message } ") ;
62+ }
63+ catch ( Exception e )
64+ {
65+ Log . Error ( "Fatal error while trying to execute the command" , e ) ;
66+ }
4567 }
4668
47- public string ToHelpText ( )
69+ public string ToHelpText ( bool cropText = false )
4870 {
4971 StringBuilder cmd = new StringBuilder ( Name ) ;
72+ if ( Aliases ? . Count > 0 )
73+ {
74+ cmd . AppendFormat ( "/{0}" , string . Join ( "/" , Aliases ) ) ;
75+ }
76+ cmd . AppendFormat ( " {0}" , string . Join ( " " , Parameters ) ) ;
77+ return cropText ? $ "{ cmd } " : $ "{ cmd , - 32 } - { Description } ";
78+ }
79+
80+ protected void AddParameter < T > ( T param ) where T : IParameter < object >
81+ {
82+ Validate . NotNull ( param as object ) ;
5083
51- if ( Alias . Length > 0 )
84+ Parameters . Add ( param ) ;
85+ if ( param . IsRequired )
5286 {
53- cmd . AppendFormat ( "/{0}" , string . Join ( "/" , Alias ) ) ;
87+ required ++ ;
5488 }
55- cmd . AppendFormat ( " {0}" , ArgsDescription ) ;
89+ else
90+ {
91+ optional ++ ;
92+ }
93+ }
5694
57- return $ "{ cmd , - 35 } - { Description } ";
95+ protected void AddAlias ( params string [ ] alias )
96+ {
97+ aliases . AddRange ( alias ) ;
5898 }
5999
100+ protected void AddAlias ( string alias )
101+ {
102+ aliases . Add ( alias ) ;
103+ }
104+
105+ /// <summary>
106+ /// Send a message to an existing player
107+ /// </summary>
60108 public void SendMessageToPlayer ( Optional < Player > player , string message )
61109 {
62110 if ( player . HasValue )
@@ -65,10 +113,13 @@ public void SendMessageToPlayer(Optional<Player> player, string message)
65113 }
66114 }
67115
68- public void Notify ( Optional < Player > player , string message )
116+ /// <summary>
117+ /// Send a message to an existing player and logs it in the console
118+ /// </summary>
119+ public void SendMessage ( Optional < Player > player , string message )
69120 {
70- Log . Info ( message ) ;
71121 SendMessageToPlayer ( player , message ) ;
122+ Log . Info ( message ) ;
72123 }
73124 }
74125}
0 commit comments