Class CommandContext

java.lang.Object
com.hypixel.hytale.server.core.command.system.CommandContext

public final class CommandContext extends Object
Holds data in the context of an in-progress command execution, such as the input, the sender, and the parsed values of arguments. This class also handles the parsing and validation of arguments.

A CommandContext is similar to a ParserContext except it is within the context of the command, as opposed to the system as a whole. Where a ParserContext is before the command is chosen, a CommandContext is created after the command is chosen. A ParserContext is used to create a CommandContext.

  • Constructor Details

    • CommandContext

      public CommandContext(@Nonnull AbstractCommand calledCommand, @Nonnull CommandSender sender, @Nonnull String inputString)
      Default Constructor for a CommandContext instance.
      Parameters:
      calledCommand - The command instance that was called.
      sender - The sender that called the command.
      inputString - The full input string that called the command.
  • Method Details

    • get

      public <DataType> DataType get(@Nonnull Argument<?,DataType> argument)
      Return the value relating to the specified argument, parsed by appendArgumentData(Argument, String[], boolean, ParseResult).

      Note: If you are using an OptionalArg you should use Argument.provided(CommandContext) before fetching, or else you will get an unexpected null value

      Type Parameters:
      DataType - The Type to return.
      Parameters:
      argument - The argument to parse.
      Returns:
      the parse result of the argument, or null if not specified or no default value.
    • getInput

      public String[] getInput(@Nonnull Argument<?,?> argument)
      Get the actual input String(s) associated with this argument, in case you want to do some parsing yourself or use them for another system.
      Parameters:
      argument - The argument to get the input String(s) from.
      Returns:
      the input String(s) of this argument.
    • provided

      public boolean provided(@Nonnull Argument<?,?> argument)
      Check if an Argument is provided on this command context. Never needs to be used for provided arguments.
      Parameters:
      argument - the argument to check if it is provided or not.
      Returns:
      true if the argument is provided on this context, false otherwise.
    • getInputString

      @Nonnull public String getInputString()
      Returns:
      the cleaned input string that called this command.
    • sendMessage

      public void sendMessage(@Nonnull Message message)
      Called to send a message to the command sender.
      Parameters:
      message - The message to send.
    • isPlayer

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

      @Nonnull public <T extends CommandSender> T senderAs(@Nonnull Class<T> senderType)
      Called to fetch the command sender as a specific type.
      Type Parameters:
      T - The type of the command sender.
      Parameters:
      senderType - The class type to cast the sender to.
      Returns:
      the command sender as the specified type, or throws a SenderTypeException if the sender is not of the specified type.
    • senderAsPlayerRef

      @Nullable public Ref<EntityStore> senderAsPlayerRef()
      Returns:
      the entity reference of the command sender, or null if the sender is not a player.
    • sender

      @Nonnull public CommandSender sender()
      Returns:
      the command sender of this command context.
    • getCalledCommand

      @Nonnull public AbstractCommand getCalledCommand()
      Returns:
      the command that was called to create this context.