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)
      Create a new command context instance
      CommandContext​(@NonNull C commandSender, @NonNull CaptionRegistry<C> captionRegistry)
      Create a new command context instance
    • Method Summary

      Modifier and Type Method Description
      @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> argument)
      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
      <T> @NonNull java.util.Optional<T> getOptional​(@NonNull CommandArgument<C,​T> argument)
      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 java.lang.String key, @Nullable T defaultValue)
      Get a value if it exists, else return the provided default value
      @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
      boolean isSuggestions()
      Check if this context was created for tab completion purposes
      void remove​(@NonNull java.lang.String key)
      Remove a stored value from the context
      <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

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

        public CommandContext​(boolean suggestions,
                              @NonNull C commandSender,
                              @NonNull CaptionRegistry<C> captionRegistry)
        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
    • 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
      • 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 CommandArgument<C,​T> argument)
        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:
        argument - Argument
        Returns:
        Value
      • remove

        public void remove​(@NonNull java.lang.String key)
        Remove a stored value from the context
        Parameters:
        key - Key to remove
      • 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 CommandArgument<C,​T> argument)
        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:
        argument - The argument
        Returns:
        Stored value
        Throws:
        java.lang.NullPointerException - If no such value is stored
      • 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
      • 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