Class AbstractCommand

java.lang.Object
fr.luc.crcore.util.command.AbstractCommand
All Implemented Interfaces:
Command
Direct Known Subclasses:
BaseCommand, SubCommand

public abstract class AbstractCommand extends Object implements Command
Base partagée par BaseCommand (top-level Bukkit) et SubCommand (feuille ou groupe imbriqué). Porte tous les champs communs : nom, aliases, permission, player-only, description, usage, arguments typés, et un registre de sous-commandes imbriquées.

Une commande peut être :

  • feuille : pas de sous-commandes, implémente execute(CommandContext)
  • groupe : sous-commandes via addSubCommand(SubCommand), le routage est récursif. Si aucune sous-commande ne matche, execute(CommandContext) est appelé en fallback (par défaut : liste les sous-commandes).
  • hybride : groupe ET arguments propres — déconseillé, le routage donne priorité aux sous-commandes.

L'override par les plugins de jeu se fait via replaceSubCommand(String, SubCommand) (remplacer une sous-commande par nom) ou en sous-classant et en redéfinissant execute(CommandContext).

  • Constructor Details

    • AbstractCommand

      protected AbstractCommand(String name, String... aliases)
  • Method Details

    • getName

      public final String getName()
      Specified by:
      getName in interface Command
    • getAliases

      public final List<String> getAliases()
      Specified by:
      getAliases in interface Command
    • getPermission

      public final String getPermission()
      Specified by:
      getPermission in interface Command
    • isPlayerOnly

      public final boolean isPlayerOnly()
      Specified by:
      isPlayerOnly in interface Command
    • getDescription

      public final String getDescription()
      Specified by:
      getDescription in interface Command
    • getUsage

      public final String getUsage()
      Usage explicite si défini via usage(String), sinon usage auto-construit.
    • addAlias

      protected final void addAlias(String... aliases)
      Ajoute un ou plusieurs alias. Les aliases sont case-insensitive.
    • permission

      protected final void permission(String permission)
      Définit la permission Bukkit requise (ex. "crcore.team.create").
    • playerOnly

      protected final void playerOnly()
      Restreint l'exécution aux joueurs (refus pour console).
    • description

      protected final void description(String description)
      Description courte affichée dans l'aide.
    • usage

      protected final void usage(String usage)
      Usage explicite (sinon construit automatiquement à partir des arguments).
    • argument

      protected final void argument(String name, ArgumentType<?> type)
      Déclare un argument positionnel obligatoire.
    • optionalArgument

      protected final void optionalArgument(String name, ArgumentType<?> type)
      Déclare un argument positionnel optionnel (peut être omis par l'utilisateur).
    • addSubCommand

      protected final void addSubCommand(SubCommand sub)
      Enregistre une sous-commande. Lève IllegalStateException si une sous-commande du même nom existe déjà (utiliser replaceSubCommand(String, SubCommand) pour overrider).
    • replaceSubCommand

      public final Optional<SubCommand> replaceSubCommand(String name, SubCommand replacement)
      Remplace une sous-commande existante par son nom. Utilisé par les plugins de jeu pour overrider un comportement par défaut.
      coreCommand.findSubCommand("team")
          .ifPresent(team -> team.replaceSubCommand("create", new MyCustomCreate(svc)));
      
      Returns:
      l'ancienne sous-commande remplacée, ou Optional.empty() si aucune n'existait sous ce nom.
    • findSubCommand

      public final Optional<SubCommand> findSubCommand(String label)
      Recherche une sous-commande par nom ou par alias (case-insensitive).
    • getSubCommands

      public final Collection<SubCommand> getSubCommands()
      Toutes les sous-commandes enregistrées, dans l'ordre d'insertion.
    • hasSubCommands

      public final boolean hasSubCommands()
    • getRequiredArgumentCount

      public final int getRequiredArgumentCount()
    • getTotalArgumentCount

      public final int getTotalArgumentCount()
    • dispatch

      public final CommandResult dispatch(org.bukkit.command.CommandSender sender, String label, String[] args)
      Achemine l'exécution : si args[0] matche une sous-commande, on recurse dessus avec args[1..]. Sinon on appelle execute(CommandContext) de cette commande. Gère les checks permission / player-only.
    • tabComplete

      public final List<String> tabComplete(org.bukkit.command.CommandSender sender, String[] args)
      Auto-complétion récursive : si args a une seule valeur, suggère les sous-commandes accessibles + les arguments propres. Sinon recurse vers la sous-commande qui matche.
      Specified by:
      tabComplete in interface Command
    • execute

      public CommandResult execute(CommandContext context)
      Logique métier de la commande. Override par les sous-classes.

      Comportement par défaut :

      Specified by:
      execute in interface Command
    • listSubCommands

      protected CommandResult listSubCommands(CommandContext context)
      Default execute pour les groupes : affiche la liste des sous-commandes accessibles.
    • buildDefaultUsage

      protected String buildDefaultUsage()
      Construit un usage par défaut à partir du nom et des arguments déclarés.
    • checkAccess

      protected boolean checkAccess(org.bukkit.command.CommandSender sender)
      Check standard de permission + player-only.
    • buildContext

      protected CommandContext buildContext(org.bukkit.command.CommandSender sender, String label, String[] rawArgs)
      Parse les arguments raw et construit le CommandContext.