Class CommandContext<C>

java.lang.Object
cloud.commandframework.context.CommandContext<C>
Type Parameters:
C - Command sender type

public final class CommandContext<C> extends Object
Command context used to assist in the parsing of commands
  • Constructor Details

    • CommandContext

      @Deprecated public CommandContext(@NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry)
      Deprecated.
      Provide a command manager instead of a caption registry
      Create a new command context instance
      Parameters:
      commandSender - Sender of the command
      captionRegistry - Caption registry
    • CommandContext

      public CommandContext(@NonNull C commandSender, @NonNull CommandManager<C> commandManager)
      Create a new command context instance
      Parameters:
      commandSender - Sender of the command
      commandManager - Command manager
      Since:
      1.3.0
    • CommandContext

      @Deprecated public CommandContext(boolean suggestions, @NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry)
      Deprecated.
      Provide a command manager instead of a caption registry
      Create a new command context instance
      Parameters:
      suggestions - Whether or not the context is created for command suggestions
      commandSender - Sender of the command
      captionRegistry - Caption registry
    • CommandContext

      public CommandContext(boolean suggestions, @NonNull C commandSender, @NonNull CommandManager<C> commandManager)
      Create a new command context instance
      Parameters:
      suggestions - Whether or not the context is created for command suggestions
      commandSender - Sender of the command
      commandManager - Command manager
      Since:
      1.3.0
  • Method Details

    • formatMessage

      public @NonNull String formatMessage(@NonNull Caption caption, @NonNull CaptionVariable... variables)
      Format a caption
      Parameters:
      caption - Caption key
      variables - Replacements
      Returns:
      Formatted message
    • getSender

      public @NonNull C getSender()
      Get the sender that executed the command
      Returns:
      Command sender
    • isSuggestions

      public boolean isSuggestions()
      Check if this context was created for tab completion purposes
      Returns:
      true if this context is requesting suggestions, else false
    • store

      public <T> void store(@NonNull String key, @NonNull T value)
      Store a value in the context map. This will overwrite any existing value stored with the same key
      Type Parameters:
      T - Value type
      Parameters:
      key - Key
      value - Value
    • store

      public <T> void store(@NonNull CloudKey<T> key, @NonNull T value)
      Store a value in the context map. This will overwrite any existing value stored with the same key
      Type Parameters:
      T - Value type
      Parameters:
      key - Key
      value - Value
    • store

      public <T> void store(@NonNull CommandArgument<C,​T> keyHolder, @NonNull T value)
      Store a value in the context map. This will overwrite any existing value stored with the same key
      Type Parameters:
      T - Value type
      Parameters:
      keyHolder - Holder of the identifying key
      value - Value
    • store

      public <T> void store(@NonNull CloudKeyHolder<T> keyHolder, @NonNull T value)
      Store a value in the context map. This will overwrite any existing value stored with the same key
      Type Parameters:
      T - Value type
      Parameters:
      keyHolder - Holder of the identifying key
      value - Value
      Since:
      1.4.0
    • set

      public <T> void set(@NonNull String key, @Nullable T value)
      Store or remove a value in the context map. This will overwrite any existing value stored with the same key.

      If the provided value is null, any current value stored for the provided key will be removed.

      Type Parameters:
      T - Value type
      Parameters:
      key - Key
      value - Value
      Since:
      1.3.0
    • set

      public <T> void set(@NonNull CloudKey<T> key, @Nullable T value)
      Store or remove a value in the context map. This will overwrite any existing value stored with the same key.

      If the provided value is null, any current value stored for the provided key will be removed.

      Type Parameters:
      T - Value type
      Parameters:
      key - Key
      value - Value
      Since:
      1.4.0
    • contains

      public boolean contains(@NonNull String key)
      Check if the context has a value stored for a key
      Parameters:
      key - Key
      Returns:
      Whether the context has a value for the provided key
      Since:
      1.3.0
    • contains

      public boolean contains(@NonNull CloudKey<?> key)
      Check if the context has a value stored for a key
      Parameters:
      key - Key
      Returns:
      Whether the context has a value for the provided key
      Since:
      1.4.0
    • asMap

      public @NonNull Map<@NonNull String,​@Nullable ?> asMap()
      Get the current state of this command context as a map of String to context value.
      Returns:
      An immutable copy of this command context as a map
      Since:
      1.3.0
    • getOptional

      public <T> @NonNull Optional<T> getOptional(@NonNull String key)
      Get a value from its key. Will return Optional.empty() if no value is stored with the given key
      Type Parameters:
      T - Value type
      Parameters:
      key - Key
      Returns:
      Value
    • getOptional

      public <T> @NonNull Optional<T> getOptional(@NonNull CloudKey<T> key)
      Get a value from its key. Will return Optional.empty() if no value is stored with the given key
      Type Parameters:
      T - Value type
      Parameters:
      key - Key
      Returns:
      Value
      Since:
      1.4.0
    • getOptional

      public <T> @NonNull Optional<T> getOptional(@NonNull CommandArgument<C,​T> keyHolder)
      Get a value from its key. Will return Optional.empty() if no value is stored with the given key
      Type Parameters:
      T - Value type
      Parameters:
      keyHolder - Holder of the key
      Returns:
      Value
    • getOptional

      public <T> @NonNull Optional<T> getOptional(@NonNull CloudKeyHolder<T> keyHolder)
      Get a value from its key. Will return Optional.empty() if no value is stored with the given key
      Type Parameters:
      T - Value type
      Parameters:
      keyHolder - Holder of the key
      Returns:
      Value
      Since:
      1.4.0
    • remove

      public void remove(@NonNull String key)
      Remove a stored value from the context
      Parameters:
      key - Key to remove
    • remove

      public void remove(@NonNull CloudKey<?> key)
      Remove a stored value from the context
      Parameters:
      key - Key to remove
      Since:
      1.4.0
    • get

      public <T> @NonNull T get(@NonNull String key)
      Get a required argument from the context. This will thrown an exception if there's no value associated with the given key
      Type Parameters:
      T - Argument type
      Parameters:
      key - Argument key
      Returns:
      Argument
      Throws:
      NullPointerException - If no such argument is stored
    • get

      public <T> @NonNull T get(@NonNull CloudKey<T> key)
      Get a required argument from the context. This will thrown an exception if there's no value associated with the given key
      Type Parameters:
      T - Argument type
      Parameters:
      key - Argument key
      Returns:
      Argument
      Throws:
      NullPointerException - If no such argument is stored
      Since:
      1.4.0
    • get

      public <T> @NonNull T get(@NonNull CommandArgument<C,​T> keyHolder)
      Get a required argument from the context. This will thrown an exception if there's no value associated with the given argument
      Type Parameters:
      T - Argument type
      Parameters:
      keyHolder - Holder of the identifying key
      Returns:
      Stored value
      Throws:
      NullPointerException - If no such value is stored
    • get

      public <T> @NonNull T get(@NonNull CloudKeyHolder<T> keyHolder)
      Get a required argument from the context. This will thrown an exception if there's no value associated with the given argument
      Type Parameters:
      T - Argument type
      Parameters:
      keyHolder - Holder of the identifying key
      Returns:
      Stored value
      Throws:
      NullPointerException - If no such value is stored
      Since:
      1.4.0
    • getOrDefault

      public <T> @Nullable T getOrDefault(@NonNull CommandArgument<C,​T> argument, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      Type Parameters:
      T - Argument type
      Parameters:
      argument - Argument
      defaultValue - Default value
      Returns:
      Stored value, or supplied default value
    • getOrDefault

      public <T> @Nullable T getOrDefault(@NonNull String key, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      Type Parameters:
      T - Argument type
      Parameters:
      key - Argument key
      defaultValue - Default value
      Returns:
      Argument, or supplied default value
    • getOrDefault

      public <T> @Nullable T getOrDefault(@NonNull CloudKey<T> key, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      Type Parameters:
      T - Argument type
      Parameters:
      key - Argument key
      defaultValue - Default value
      Returns:
      Argument, or supplied default value
      Since:
      1.4.0
    • getOrSupplyDefault

      public <T> @Nullable T getOrSupplyDefault(@NonNull String key, @NonNull Supplier<@Nullable T> defaultSupplier)
      Get a value if it exists, else return the value supplied by the given supplier
      Type Parameters:
      T - Argument type
      Parameters:
      key - Argument key
      defaultSupplier - Supplier of default value
      Returns:
      Argument, or supplied default value
      Since:
      1.2.0
    • getOrSupplyDefault

      public <T> @Nullable T getOrSupplyDefault(@NonNull CloudKey<T> key, @NonNull Supplier<@Nullable T> defaultSupplier)
      Get a value if it exists, else return the value supplied by the given supplier
      Type Parameters:
      T - Argument type
      Parameters:
      key - Argument key
      defaultSupplier - Supplier of default value
      Returns:
      Argument, or supplied default value
      Since:
      1.4.0
    • getRawInput

      public @NonNull LinkedList<@NonNull String> getRawInput()
      Get the raw input. This should only be used when isSuggestions() is true
      Returns:
      Raw input in token form
    • getRawInputJoined

      public @NonNull String getRawInputJoined()
      Get the raw input as a joined string
      Returns:
      getRawInput() joined with " " as the delimiter
      Since:
      1.1.0
    • createTiming

      public @NonNull CommandContext.ArgumentTiming createTiming(@NonNull CommandArgument<C,​?> argument)
      Create an argument timing for a specific argument
      Parameters:
      argument - Argument
      Returns:
      Created timing instance
    • getArgumentTimings

      public @NonNull Map<CommandArgument<@NonNull C,​@NonNull ?>,​CommandContext.ArgumentTiming> getArgumentTimings()
      Get an immutable view of the argument timings map
      Returns:
      Argument timings
    • flags

      public @NonNull FlagContext flags()
      Get the associated FlagContext instance
      Returns:
      Flag context
    • getCurrentArgument

      public @Nullable CommandArgument<C,​?> getCurrentArgument()
      Get the argument that is currently being parsed for this command context. This value will be updated whenever the context is used to provide new suggestions or parse a new command argument
      Returns:
      Currently parsing CommandArgument or null
      Since:
      1.2.0
    • setCurrentArgument

      public void setCurrentArgument(@Nullable CommandArgument<C,​?> argument)
      Set the argument that is currently being parsed for this command context. This value should be updated whenever the context is used to provide new suggestions or parse a new command argument
      Parameters:
      argument - Currently parsing CommandArgument or null
      Since:
      1.2.0
    • inject

      public <@NonNull T> @NonNull Optional<T> inject(@NonNull Class<@NonNull T> clazz)
      Attempt to retrieve a value that has been registered to the associated command manager's ParameterInjectorRegistry
      Type Parameters:
      T - Type to inject
      Parameters:
      clazz - Class of type to inject
      Returns:
      Optional that may contain the created value
      Since:
      1.3.0