Class CommandFramework

java.lang.Object
dev.despical.commandframework.CommandFramework
All Implemented Interfaces:
org.bukkit.command.CommandExecutor, org.bukkit.command.TabCompleter

@NonExtendable public class CommandFramework extends Object
Main class of the framework to register commands, add tab completions and implement a function to run if there is no matched commands related this framework.
Since:
1.0.0
Author:
Despical
  • Field Details

    • instance

      protected static CommandFramework instance
    • registry

      protected final dev.despical.commandframework.internal.CommandRegistry registry
    • parameterHandler

      protected final dev.despical.commandframework.internal.ParameterHandler parameterHandler
  • Constructor Details

    • CommandFramework

      public CommandFramework(@NotNull @NotNull org.bukkit.plugin.Plugin plugin)
  • Method Details

    • registerCommands

      public final void registerCommands(@NotNull @NotNull Object instance)
      Registers commands from the specified instance's class.

      This method scans the class of the provided instance and registers all commands defined within that class. The class should contain methods annotated to be recognized as commands.

      Parameters:
      instance - the instance of the class from which commands will be registered. Must not be null.
    • registerAllInPackage

      public final void registerAllInPackage(@NotNull @NotNull String packageName)
      Scans all classes within the specified package, creates instances of them, and registers them as commands.

      Note: Scanned classes must have a public no-args constructor. Interfaces and abstract classes are automatically skipped.

      Parameters:
      packageName - The full path of the package to scan (e.g., "com.example.project.commands")
      See Also:
    • unregisterCommand

      public final void unregisterCommand(@NotNull @NotNull String commandName)
      Unregisters a command and its associated tab completer if they are registered with the specified name.
      Parameters:
      commandName - the name of the command to be unregistered. Must not be null or empty.
      Throws:
      IllegalArgumentException - if commandName is null or an empty string.
    • unregisterCommands

      public final void unregisterCommands()
      Unregisters all commands and tab completers that were registered using the instance of this object.
    • addCustomParameter

      public final <A, B extends A> void addCustomParameter(@NotNull @NotNull String value, @NotNull @NotNull Function<CommandArguments,B> function)
      Adds a custom parameter to the parameter handler.

      This method allows the addition of a custom parameter to the parameter handler by specifying a value and a function that converts CommandArguments to an instance of a type that extends CommandFramework.

      Type Parameters:
      A - the type of the parent class that the custom parameter's type extends
      B - the type of the custom parameter, which extends CommandFramework
      Parameters:
      value - the value to call custom parameter using @Param, must not be null, can be a class name
      function - a function that takes CommandArguments and returns an instance of CommandFramework, must not be null
      Throws:
      NullPointerException - if value is already added as a custom parameter
    • addCustomParameter

      public final <T> void addCustomParameter(@NotNull @NotNull Class<T> clazz, @NotNull @NotNull Function<CommandArguments,T> function)
      Adds a custom parameter to the parameter handler using a class type.

      This method allows the addition of a custom parameter to the parameter handler by specifying a class and a function that converts CommandArguments to an instance of CommandFramework.

      The parameter will be registered using the Class.getSimpleName() as the key.

      Type Parameters:
      T - the type of the custom parameter
      Parameters:
      clazz - the class of the custom parameter, used to derive the key using Class.getSimpleName(), must not be null
      function - a function that takes CommandArguments and returns an instance of CommandFramework, must not be null
      Throws:
      CommandException - if a custom parameter with the same class name is already registered
    • getLogger

      @NotNull @Contract(pure=true) public final @NotNull Logger getLogger()
      Returns the logger instance of Command Framework. By default, logger is plugin's logger.
      Returns:
      the current logger instance.
      Since:
      1.4.8
    • setLogger

      public final void setLogger(@NotNull @NotNull Logger logger)
      Changes default logger
      Parameters:
      logger - the non-null new logger instance
      Since:
      1.4.8
    • options

      public final OptionManager options()
      Returns the option manager.
      Returns:
      the option manager.
      Since:
      1.4.8
    • getCommands

      @NotNull @Contract(pure=true) public final @NotNull List<Command> getCommands()
      Get an unmodifiable copy of registered commands.
      Returns:
      list of the commands.
    • getSubCommands

      @NotNull @Contract(pure=true) public final @NotNull List<Command> getSubCommands()
      Get an unmodifiable copy of registered sub-commands-.
      Returns:
      list of the sub-commands.
    • getAllCommands

      @NotNull @Contract(pure=true) public final @NotNull List<Command> getAllCommands()
      Get an unmodifiable copy of registered commands and sub-commands.
      Returns:
      list of the commands and sub-commands.
    • getPlugin

      @NotNull @Contract(pure=true) public final @NotNull org.bukkit.plugin.Plugin getPlugin()
    • setCommandMap

      protected final void setCommandMap(org.bukkit.command.CommandMap commandMap)
    • getInstance

      public static CommandFramework getInstance()
    • onCommand

      public boolean onCommand(@NotNull @NotNull org.bukkit.command.CommandSender sender, @NotNull org.bukkit.command.Command cmd, @NotNull @NotNull String label, String[] args)
      Specified by:
      onCommand in interface org.bukkit.command.CommandExecutor
    • onTabComplete

      public List<String> onTabComplete(@NotNull @NotNull org.bukkit.command.CommandSender sender, @NotNull org.bukkit.command.Command cmd, @NotNull @NotNull String label, String[] args)
      Specified by:
      onTabComplete in interface org.bukkit.command.TabCompleter