55import fr .traqueur .commands .api .exceptions .ArgsWithInfiniteArgumentException ;
66import fr .traqueur .commands .api .requirements .Requirement ;
77import org .bukkit .command .CommandSender ;
8- import org .bukkit .plugin .java . JavaPlugin ;
8+ import org .bukkit .plugin .Plugin ;
99
1010import java .util .ArrayList ;
1111import java .util .Arrays ;
1212import java .util .List ;
13+ import java .util .stream .Collectors ;
1314
1415/**
1516 * This class is the base class for all commands.
1617 * It contains all the necessary methods to create a command.
1718 * It is abstract and must be inherited to be used.
1819 * @param <T> The plugin that owns the command.
1920 */
20- public abstract class Command <T extends JavaPlugin > {
21+ public abstract class Command <T extends Plugin > {
22+
23+ private CommandManager <T > manager ;
2124
22- private CommandManager manager ;
23- // Attributs de la classe
2425 /**
2526 * The plugin that owns the command.
2627 */
@@ -39,7 +40,7 @@ public abstract class Command<T extends JavaPlugin> {
3940 /**
4041 * The subcommands of the command.
4142 */
42- private final List <Command <? >> subcommands ;
43+ private final List <Command <T >> subcommands ;
4344
4445 /**
4546 * The arguments of the command.
@@ -110,7 +111,7 @@ public Command(T plugin, String name) {
110111 * This method is called to set the manager of the command.
111112 * @param manager The manager of the command.
112113 */
113- protected void setManager (CommandManager manager ) {
114+ protected void setManager (CommandManager < T > manager ) {
114115 this .manager = manager ;
115116 }
116117
@@ -184,7 +185,7 @@ public final List<String> getAliases() {
184185 * This method is called to get the subcommands of the command.
185186 * @return The subcommands of the command.
186187 */
187- public final List <Command <? >> getSubcommands () {
188+ public final List <Command <T >> getSubcommands () {
188189 return subcommands ;
189190 }
190191
@@ -280,9 +281,10 @@ public final void addAlias(String alias) {
280281 * This method is called to add subcommands to the command.
281282 * @param commands The subcommands to add.
282283 */
283- public final void addSubCommand (Command <?>... commands ) {
284- List <Command <?>> commandsList = Arrays .asList (commands );
285- commandsList .forEach (command -> command .setSubcommand (true ));
284+ @ SafeVarargs
285+ public final void addSubCommand (Command <T >... commands ) {
286+ List <Command <T >> commandsList = Arrays .asList (commands );
287+ commandsList .forEach (Command ::setSubcommand );
286288 this .subcommands .addAll (commandsList );
287289 }
288290
@@ -447,19 +449,45 @@ public final boolean isSubCommand() {
447449 return subcommand ;
448450 }
449451
450- /**
451- * Set if the command is subcommand
452- * @param subcommand the new value
453- */
454- public final void setSubcommand (boolean subcommand ) {
455- this .subcommand = subcommand ;
456- }
457-
458452 /**
459453 * This method is called to get the plugin that owns the command.
460454 * @return The plugin that owns the command.
461455 */
462456 public final T getPlugin () {
463457 return plugin ;
464458 }
459+
460+ /**
461+ * This method is called to generate a default usage for the command.
462+ * @return The default usage of the command.
463+ */
464+ protected String generateDefaultUsage (CommandSender sender , String label ) {
465+ StringBuilder usage = new StringBuilder ();
466+ usage .append ("/" );
467+ Arrays .stream (label .split ("\\ ." )).forEach (s -> usage .append (s ).append (" " ));
468+
469+ StringBuilder firstArg = new StringBuilder ();
470+ this .getSubcommands ()
471+ .stream ().filter (subCommand -> subCommand .getPermission ().isEmpty () || sender .hasPermission (subCommand .getPermission ()))
472+ .forEach (subCommand -> firstArg .append (subCommand .getName ()).append ("|" ));
473+ if (firstArg .length () > 0 ) {
474+ firstArg .deleteCharAt (firstArg .length () - 1 );
475+ usage .append ("<" ).append (firstArg ).append (">" );
476+ }
477+ if ((!this .getArgs ().isEmpty () || !this .getOptinalArgs ().isEmpty ()) && firstArg .length () > 0 ) {
478+ usage .append ("|" );
479+ }
480+
481+ usage .append (this .getArgs ().stream ().map (argument -> "<" + argument .arg () + ">" ).collect (Collectors .joining (" " )));
482+ usage .append (" " );
483+ usage .append (this .getOptinalArgs ().stream ().map (argument -> "[" + argument .arg () + "]" ).collect (Collectors .joining (" " )));
484+ return usage .toString ();
485+ }
486+
487+ /**
488+ * Set if the command is subcommand
489+ */
490+ private void setSubcommand () {
491+ this .subcommand = true ;
492+ }
465493}
0 commit comments