Class 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 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

      • 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,​?> 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
      • 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> @NonNullget​(@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> @NonNullget​(@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> @NonNullget​(@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> @NonNullget​(@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> @NullablegetOrDefault​(@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> @NullablegetOrDefault​(@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> @NullablegetOrDefault​(@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> @NullablegetOrSupplyDefault​(@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> @NullablegetOrSupplyDefault​(@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
      • 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