1515
1616import java .lang .reflect .Method ;
1717import java .util .ArrayList ;
18+ import java .util .Arrays ;
1819import java .util .LinkedList ;
1920import java .util .List ;
2021
@@ -37,6 +38,7 @@ public class Command {
3738 private final List <CommandParameter > commandParameters = new ArrayList <>();
3839
3940 private boolean player ;
41+ private boolean subCommand ;
4042 private String permission , permissionMessage ;
4143
4244 /**
@@ -64,6 +66,32 @@ public Command(Object object, String name, String[] aliases, Method method, Stri
6466 this .commandExecutor = new CommandExecutor (this );
6567 }
6668
69+ /**
70+ * Gets a sub command
71+ *
72+ * @param args arguments
73+ * @return {@link Command}
74+ */
75+
76+ public final Command getSubCommand (String [] args ) {
77+ for (Command command : subCommands ) {
78+ String [] commandArg = command .getName ().split (" " );
79+
80+ List <String > strings = new ArrayList <>(Arrays .asList (commandArg ));
81+ List <String > argsList = new ArrayList <>(Arrays .asList (args ));
82+
83+ strings .removeIf (s -> !argsList .contains (s ));
84+
85+ commandArg = strings .toArray (new String [0 ]);
86+
87+ if (commandArg .length != 0 && commandArg [commandArg .length - 1 ].equalsIgnoreCase (args [args .length - 1 ])) {
88+ return command ;
89+ }
90+ }
91+
92+ return null ;
93+ }
94+
6795 /**
6896 * Executes the command
6997 *
@@ -72,11 +100,11 @@ public Command(Object object, String name, String[] aliases, Method method, Stri
72100 */
73101
74102 @ SneakyThrows
75- public void execute (CommandSender commandSender , String [] args ) {
103+ public void execute (CommandSender commandSender , String [] args , boolean skip ) {
76104
77105 if (async ) {
78106 CommandHandler .getInstance ().getJavaPlugin ().getServer ().getScheduler ()
79- .runTaskAsynchronously (CommandHandler .getInstance ().getJavaPlugin (), () -> execute (commandSender , args ));
107+ .runTaskAsynchronously (CommandHandler .getInstance ().getJavaPlugin (), () -> execute (commandSender , args , skip ));
80108 return ;
81109 }
82110
@@ -95,12 +123,36 @@ public void execute(CommandSender commandSender, String[] args) {
95123 return ;
96124 }
97125
126+ Command subCommand = getSubCommand (args );
127+
128+ if (!skip ) {
129+ if (subCommand == null ) {
130+ if (helpCommandService == null ) {
131+ this .execute (commandSender , args , true );
132+ return ;
133+ }
134+
135+ helpCommandService .get (this , getSubCommands ()).forEach (commandSender ::sendMessage );
136+ return ;
137+ }
138+ }
139+
98140 if (args .length < commandParameters .size ()) {
99141 commandSender .sendMessage (ChatColor .RED + "Wrong Usage: " + usage );
100142 return ;
101143 }
102144
103145 List <Object > objects = new ArrayList <>();
146+ List <CommandParameter > commandParameters = this .commandParameters ;
147+
148+ Method method = this .method ;
149+ Object object = this .object ;
150+
151+ if (!skip ) {
152+ commandParameters = this .subCommand && subCommand != null ? subCommand .getCommandParameters () : this .commandParameters ;
153+ method = this .subCommand && subCommand != null ? subCommand .getMethod () : this .method ;
154+ object = this .subCommand && subCommand != null ? subCommand .getObject () : this .object ;
155+ }
104156
105157 if (player ) {
106158 Player player = (Player ) commandSender ;
0 commit comments