Class CommandContext<C>

  • Type Parameters:
    C - Command sender type

    public final class CommandContext<C>
    extends java.lang.Object
    Command context used to assist in the parsing of commands
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CommandContext.ArgumentTiming
      Used to track performance metrics related to command parsing.
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandContext​(boolean suggestions, @NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry)
      Deprecated.
      Provide a command manager instead of a caption registry
      CommandContext​(boolean suggestions, @NonNull C commandSender, @NonNull CommandManager<C> commandManager)
      Create a new command context instance
      CommandContext​(@NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry)
      Deprecated.
      Provide a command manager instead of a caption registry
      CommandContext​(@NonNull C commandSender, @NonNull CommandManager<C> commandManager)
      Create a new command context instance
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      @NonNull java.util.Map<@NonNull java.lang.String,​?> asMap()
      Get the current state of this command context as a map of String to context value.
      boolean contains​(@NonNull CloudKey<?> key)
      Check if the context has a value stored for a key
      boolean contains​(@NonNull java.lang.String key)
      Check if the context has a value stored for a key
      @NonNull CommandContext.ArgumentTiming createTiming​(@NonNull CommandArgument<C,​?> argument)
      Create an argument timing for a specific argument
      @NonNull FlagContext flags()
      Get the associated FlagContext instance
      @NonNull java.lang.String formatMessage​(@NonNull Caption caption, @NonNull CaptionVariable... variables)
      Format a caption
      <T> @NonNull T get​(@NonNull CommandArgument<C,​T> keyHolder)
      Get a required argument from the context.
      <T> @NonNull T get​(@NonNull CloudKey<T> key)
      Get a required argument from the context.
      <T> @NonNull T get​(@NonNull CloudKeyHolder<T> keyHolder)
      Get a required argument from the context.
      <T> @NonNull T get​(@NonNull java.lang.String key)
      Get a required argument from the context.
      @NonNull java.util.Map<CommandArgument<@NonNull C,​?>,​CommandContext.ArgumentTiming> getArgumentTimings()
      Get an immutable view of the argument timings map
      @Nullable CommandArgument<C,​?> getCurrentArgument()
      Get the argument that is currently being parsed for this command context.
      <T> @NonNull java.util.Optional<T> getOptional​(@NonNull CommandArgument<C,​T> keyHolder)
      Get a value from its key.
      <T> @NonNull java.util.Optional<T> getOptional​(@NonNull CloudKey<T> key)
      Get a value from its key.
      <T> @NonNull java.util.Optional<T> getOptional​(@NonNull CloudKeyHolder<T> keyHolder)
      Get a value from its key.
      <T> @NonNull java.util.Optional<T> getOptional​(@NonNull java.lang.String key)
      Get a value from its key.
      <T> @Nullable T getOrDefault​(@NonNull CommandArgument<C,​T> argument, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      <T> @Nullable T getOrDefault​(@NonNull CloudKey<T> key, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      <T> @Nullable T getOrDefault​(@NonNull java.lang.String key, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      <T> @Nullable T getOrSupplyDefault​(@NonNull CloudKey<T> key, @NonNull java.util.function.Supplier<@Nullable T> defaultSupplier)
      Get a value if it exists, else return the value supplied by the given supplier
      <T> @Nullable T getOrSupplyDefault​(@NonNull java.lang.String key, @NonNull java.util.function.Supplier<@Nullable T> defaultSupplier)
      Get a value if it exists, else return the value supplied by the given supplier
      @NonNull java.util.LinkedList<@NonNull java.lang.String> getRawInput()
      Get the raw input.
      @NonNull java.lang.String getRawInputJoined()
      Get the raw input as a joined string
      @NonNull C getSender()
      Get the sender that executed the command
      <@NonNull T>
      @NonNull java.util.Optional<T>
      inject​(@NonNull java.lang.Class<@NonNull T> clazz)
      Attempt to retrieve a value that has been registered to the associated command manager's ParameterInjectorRegistry
      boolean isSuggestions()
      Check if this context was created for tab completion purposes
      void remove​(@NonNull CloudKey<?> key)
      Remove a stored value from the context
      void remove​(@NonNull java.lang.String key)
      Remove a stored value from the context
      <T> void set​(@NonNull CloudKey<T> key, @Nullable T value)
      Store or remove a value in the context map.
      <T> void set​(@NonNull java.lang.String key, @Nullable T value)
      Store or remove a value in the context map.
      void setCurrentArgument​(@Nullable CommandArgument<C,​?> argument)
      Set the argument that is currently being parsed for this command context.
      <T> void store​(@NonNull CommandArgument<C,​T> keyHolder, @NonNull T value)
      Store a value in the context map.
      <T> void store​(@NonNull CloudKey<T> key, @NonNull T value)
      Store a value in the context map.
      <T> void store​(@NonNull CloudKeyHolder<T> keyHolder, @NonNull T value)
      Store a value in the context map.
      <T> void store​(@NonNull java.lang.String key, @NonNull T value)
      Store a value in the context map.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • 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 Detail

      • formatMessage

        public @NonNull java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.util.Map<@NonNull java.lang.String,​?> 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 java.util.Optional<T> getOptional​(@NonNull java.lang.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 java.util.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 java.util.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 java.util.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 java.lang.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 java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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 java.lang.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 java.lang.String key,
                                                  @NonNull java.util.function.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 java.util.function.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 java.util.LinkedList<@NonNull java.lang.String> getRawInput()
        Get the raw input. This should only be used when isSuggestions() is true
        Returns:
        Raw input in token form
      • getRawInputJoined

        public @NonNull java.lang.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
      • 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 java.util.Optional<T> inject​(@NonNull java.lang.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