Class CommandContext

java.lang.Object
com.ultikits.ultitools.abstracts.command.CommandContext

public final class CommandContext extends Object
Immutable command execution context containing all information about a command invocation. This class follows the Context Object pattern, encapsulating command execution data.
Since:
6.2.0
Version:
2.0.0
Author:
wisdomme
  • Field Details

    • sender

      private final org.bukkit.command.CommandSender sender
      The sender of the command.
    • command

      private final org.bukkit.command.Command command
      The Bukkit command object.
    • alias

      private final String alias
      The alias used to invoke the command.
    • rawArgs

      private final String[] rawArgs
      The raw arguments passed to the command.
    • parsedParams

      private final Map<String,String[]> parsedParams
      The parsed parameters mapped by their names.
    • matchedMethod

      @Nullable private final Method matchedMethod
      The matched method for execution.
    • matchedFormat

      @Nullable private final String matchedFormat
      The format string of the matched command.
    • executorClass

      @Nullable private final Class<?> executorClass
      The concrete BaseCommandExecutor class dispatching this command -- this.getClass() captured in onCommand, i.e. the SAME class PluginManager's load-time contract gate inspects via executor.getClass(). WR-02 (05-REVIEW.md): matchedMethod's getDeclaringClass() is NOT always this class -- an inherited, unoverridden @CmdMapping method's declaring class is whatever ancestor first declared it. Threading the concrete executor class through the context lets com.ultikits.ultitools.utils.ReflectionUtil#resolveMethodOrClassAnnotation(Method, Class, Class) resolve a class-level @CmdCD/@UsageLimit against the class the load-time gate actually checked, not an implementation detail of which ancestor happens to declare the mapping method.

      null for a context built without it (e.g. a unit test constructing CommandContext directly) -- callers fall back to the pre-WR-02, declaring-class-only resolution in that case; see resolveMethodOrClassAnnotation's own javadoc.

      Since:
      6.3.0
    • executor

      @Nullable private final Object executor
      The executor instance dispatching this command -- this captured in BaseCommandExecutor#onCommand. The config-bound @CmdCD cache is kept per executor instance, because neither the binding key nor the executor class identifies the owning module when a CooldownValidator is shared (#531). null for a context built without it.
      Since:
      6.3.0
    • timestamp

      private final long timestamp
      Timestamp when the command was received.
  • Constructor Details

    • CommandContext

      public CommandContext()
  • Method Details

    • isPlayer

      public boolean isPlayer()
      Checks if the sender is a player.
      Returns:
      true if sender is a player, false otherwise
    • getPlayer

      @Nullable public org.bukkit.entity.Player getPlayer()
      Gets the sender as a player.
      Returns:
      the player, or null if sender is not a player
    • getArgCount

      public int getArgCount()
      Gets the number of arguments.
      Returns:
      the number of raw arguments
    • getArg

      @Nullable public String getArg(int index)
      Gets an argument at the specified index.
      Parameters:
      index - the argument index (0-based)
      Returns:
      the argument value, or null if index is out of bounds
    • getParam

      @Nullable public String[] getParam(String name)
      Gets a parsed parameter by name.
      Parameters:
      name - the parameter name
      Returns:
      the parameter values, or null if not found
    • getParamValue

      @Nullable public String getParamValue(String name)
      Gets a single parsed parameter value by name.
      Parameters:
      name - the parameter name
      Returns:
      the first parameter value, or null if not found
    • withParsedParams

      public CommandContext withParsedParams(Map<String,String[]> params)
      Creates a new context with additional parsed parameters.
      Parameters:
      params - the parameters to add
      Returns:
      a new context with the merged parameters
    • withMatchedMethod

      public CommandContext withMatchedMethod(Method method, String format)
      Creates a new context with a matched method.
      Parameters:
      method - the matched method
      format - the command format
      Returns:
      a new context with the matched method