Class CommandArguments

java.lang.Object
dev.despical.commandframework.CommandArguments

public final class CommandArguments extends Object
A utility class to use command arguments without external Bukkit parameters and includes some useful methods to improve code quality and performance.
Since:
1.0.0
Author:
Despical
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    This method checks if the current command sender has a cooldown on the command that is associated with current CommandArguments object.
    Concatenates all arguments into a single String object.
    concatRangeOf(int from, int to)
    Concatenates a range of elements from the specified array into a single string, using a space as the delimiter.
    findOption(@NotNull String option)
     
    @Nullable String
    getArgument(int index)
    Retrieves the argument at the specified index.
    @NotNull String
    getArgument(int index, String defaultValue)
    Returns the indexed element from the arguments array, or the defaultValue if and only if index is out the bounds.
    boolean
    Returns the boolean value of the indexed element from the arguments array.
    double
    Returns the double value of the indexed element from the arguments array.
    float
    getArgumentAsFloat(int index)
    Returns the float value of the indexed element from the arguments array.
    int
    getArgumentAsInt(int index)
    Returns the integer value of the indexed element from the arguments array.
    long
    getArgumentAsLong(int index)
    Returns the long value of the indexed element from the arguments array.
    @NotNull String[]
    Retrieves the array of arguments associated with this object.
    @NotNull org.bukkit.command.Command
    Retrieves the Bukkit command associated with this object.
    Retrieves the base command associated with this object.
    @Nullable String
    Gets the first argument.
    @NotNull String
    Retrieves the label associated with this object.
    @Nullable String
    Gets the last argument.
    int
    Returns the number of arguments passed to the command.
    @Nullable List<String>
    getOption(@NotNull String option)
     
    Optional<org.bukkit.entity.Player>
    getPlayer(int index)
    Gets player object from the server with given argument.
    Optional<org.bukkit.entity.Player>
    Gets player object from the server with given name.
    <T extends org.bukkit.command.CommandSender>
    T
    Do not try to cast objects except subclasses of CommandSender otherwise ClassCastException will occur.
    boolean
    hasPermission(String permission)
    Returns true if the command sender has required permission or, if permission is empty.
    boolean
    Returns true if the arguments array is empty, otherwise false.
    boolean
    isFlagPresent(@NotNull String flag)
     
    boolean
    isFloatingDecimal(int index)
    Checks if the value obtained from the argument at the specified index can be successfully parsed into a floating-point decimal using Double.parseDouble.
    boolean
    Checks if the given string can be successfully parsed into a floating decimal using Double.parseDouble.
    boolean
    isInteger(int index)
    Checks if the value obtained from the argument at the specified index can be successfully parsed into an integer using Integer.parseInt.
    boolean
    isInteger(String string)
    Checks if the given string can be successfully parsed into an integer using Integer.parseInt.
    boolean
    isNumeric(int index)
    Checks if the value obtained from the argument at the specified index is numeric, i.e., if it contains only digit characters (0-9).
    boolean
    isNumeric(String string)
    Checks if the given string is numeric, i.e., if it contains only digit characters (0-9).
    boolean
    Returns true if, and only if, command sender is console.
    boolean
    Returns true if, and only if, command sender is player.
    boolean
    Sends the specified Message to command sender associated with this object.
    void
    Sends message to sender without receiving the command sender.
    void
    sendMessage(String message, Object... params)
    Sends message to sender without receiving command sender with the given parameters.
    void
    sendMessage(net.kyori.adventure.text.Component component)
    Sends the specified Component to command sender associated with this object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getSender

      @NotNull @Contract(pure=true) public <T extends org.bukkit.command.CommandSender> T getSender()
      Do not try to cast objects except subclasses of CommandSender otherwise ClassCastException will occur. Also casting for Player or ConsoleCommandSender isn't needed.
      Type Parameters:
      T - CommandSender
      Returns:
      sender of command as Player or CommandSender
    • getCommand

      @Nullable @Contract(pure=true) public Command getCommand()
      Retrieves the base command associated with this object.
      Returns:
      the base command.
      Since:
      1.4.8
    • getBukkitCommand

      @NotNull @Contract(pure=true) public @NotNull org.bukkit.command.Command getBukkitCommand()
      Retrieves the Bukkit command associated with this object.
      Returns:
      the base command as a Bukkit command.
    • getLabel

      @NotNull @Contract(pure=true) public @NotNull String getLabel()
      Retrieves the label associated with this object.
      Returns:
      label of the command.
    • getArguments

      @NotNull @Contract(pure=true) public @NotNull String[] getArguments()
      Retrieves the array of arguments associated with this object.
      Returns:
      arguments of the command.
    • getArgument

      @Nullable @Contract(pure=true) public @Nullable String getArgument(int index)
      Retrieves the argument at the specified index.
      Parameters:
      index - the index of desired argument.
      Returns:
      indexed element or null if index out of bounds
    • getFirst

      @Nullable @Contract(pure=true) public @Nullable String getFirst()
      Gets the first argument.
      Returns:
      The first argument, or null if no arguments exist.
    • getLast

      @Nullable @Contract(pure=true) public @Nullable String getLast()
      Gets the last argument.
      Returns:
      The last argument, or null if no arguments exist.
    • getArgument

      @NotNull @Contract(pure=true) public @NotNull String getArgument(int index, String defaultValue)
      Returns the indexed element from the arguments array, or the defaultValue if and only if index is out the bounds.
      Parameters:
      index - the index of desired argument.
      defaultValue - the default value to return if the index is out of bounds.
      Returns:
      the argument at the specified index, or the default value if the index is out of bounds.
    • getArgumentAsInt

      @Contract(pure=true) public int getArgumentAsInt(int index)
      Returns the integer value of the indexed element from the arguments array.
      Parameters:
      index - the index of desired argument.
      Returns:
      Integer if indexed element is primitive type of int or 0 if element is null.
    • getArgumentAsDouble

      @Contract(pure=true) public double getArgumentAsDouble(int index)
      Returns the double value of the indexed element from the arguments array.
      Parameters:
      index - the index of desired argument.
      Returns:
      Double if indexed element is primitive type of double or 0 if element is null.
    • getArgumentAsFloat

      @Contract(pure=true) public float getArgumentAsFloat(int index)
      Returns the float value of the indexed element from the arguments array.
      Parameters:
      index - the index of desired argument.
      Returns:
      Float if indexed element is primitive type of float or 0 if element is null.
    • getArgumentAsLong

      @Contract(pure=true) public long getArgumentAsLong(int index)
      Returns the long value of the indexed element from the arguments array.
      Parameters:
      index - the index of desired argument.
      Returns:
      Long if indexed element is primitive type of long or 0 if element is null.
    • getArgumentAsBoolean

      @Contract(pure=true) public boolean getArgumentAsBoolean(int index)
      Returns the boolean value of the indexed element from the arguments array.
      Parameters:
      index - the index of desired argument.
      Returns:
      Boolean if indexed element is primitive type of boolean or 0 if element is null.
    • isArgumentsEmpty

      @Contract(pure=true) public boolean isArgumentsEmpty()
      Returns true if the arguments array is empty, otherwise false.
      Returns:
      true if arguments are empty, otherwise false.
    • sendMessage

      public void sendMessage(String message)
      Sends message to sender without receiving the command sender.
      Parameters:
      message - the message will be sent to sender.
    • sendMessage

      public void sendMessage(String message, Object... params)
      Sends message to sender without receiving command sender with the given parameters.
      Parameters:
      message - the message will be sent to sender.
      params - the parameters to format the message.
    • sendMessage

      public boolean sendMessage(Message message)
      Sends the specified Message to command sender associated with this object.
      Parameters:
      message - the Message object to be sent.
      See Also:
    • sendMessage

      public void sendMessage(net.kyori.adventure.text.Component component)
      Sends the specified Component to command sender associated with this object.
      Parameters:
      component - the Component object to be sent.
    • isSenderConsole

      @Contract(pure=true) public boolean isSenderConsole()
      Returns true if, and only if, command sender is console.
      Returns:
      true if, and only if, command sender is console, otherwise false.
    • isSenderPlayer

      @Contract(pure=true) public boolean isSenderPlayer()
      Returns true if, and only if, command sender is player.
      Returns:
      true if, and only if, command sender is player, otherwise false.
    • hasPermission

      @Contract(pure=true) public boolean hasPermission(String permission)
      Returns true if the command sender has required permission or, if permission is empty.
      Parameters:
      permission - the permission to check.
      Returns:
      true if the command sender has required permission or, if permission is empty, otherwise false.
    • getLength

      @Contract(pure=true) public int getLength()
      Returns the number of arguments passed to the command.
      Returns:
      length of the arguments array.
    • getPlayer

      @Contract(pure=true) public Optional<org.bukkit.entity.Player> getPlayer(String name)
      Gets player object from the server with given name.
      Parameters:
      name - the name of player.
      Returns:
      player with the given name if online, otherwise empty optional.
      Throws:
      IllegalArgumentException - if the name is null.
      Since:
      1.3.6
      See Also:
    • getPlayer

      @Contract(pure=true) public Optional<org.bukkit.entity.Player> getPlayer(int index)
      Gets player object from the server with given argument.
      Parameters:
      index - the index of desired argument.
      Returns:
      player with the given name if online, otherwise empty optional.
      Throws:
      IllegalArgumentException - if given index is out of bounds.
      Since:
      1.3.6
      See Also:
    • concatArguments

      @Contract(pure=true) public String concatArguments()
      Concatenates all arguments into a single String object.
      Returns:
      all arguments as a single String object.
      Since:
      1.3.8
    • concatRangeOf

      @Contract(pure=true) public String concatRangeOf(int from, int to)
      Concatenates a range of elements from the specified array into a single string, using a space as the delimiter.
      Parameters:
      from - the starting index (inclusive) of the range.
      to - the ending index (exclusive) of the range.
      Returns:
      a string containing the concatenated elements within the specified range, separated by a space.
      Throws:
      ArrayIndexOutOfBoundsException - if from is negative, to is greater than the length of the array, or from is greater than to.
      Since:
      1.3.8
    • isNumeric

      @Contract(pure=true) public boolean isNumeric(int index)
      Checks if the value obtained from the argument at the specified index is numeric, i.e., if it contains only digit characters (0-9).
      Parameters:
      index - The index of the argument from which the value is retrieved.
      Returns:
      true if the value at the specified argument index is numeric, false otherwise. Returns false for null or empty values obtained from the argument.
    • isNumeric

      @Contract(pure=true) public boolean isNumeric(String string)
      Checks if the given string is numeric, i.e., if it contains only digit characters (0-9).
      Parameters:
      string - The input string to be checked for numeric content.
      Returns:
      true if the input string is numeric, false otherwise. Returns false for null or empty strings.
    • isInteger

      @Contract(pure=true) public boolean isInteger(int index)
      Checks if the value obtained from the argument at the specified index can be successfully parsed into an integer using Integer.parseInt.
      Parameters:
      index - The index of the argument from which the value is retrieved.
      Returns:
      true if the value at the specified argument index can be parsed into an integer, false otherwise. Returns false for null or empty values obtained from the argument. Also returns false for values that cannot be parsed into an integer.
    • isInteger

      @Contract(pure=true) public boolean isInteger(String string)
      Checks if the given string can be successfully parsed into an integer using Integer.parseInt.
      Parameters:
      string - The input string to be checked for its ability to be parsed into an integer.
      Returns:
      true if the string can be parsed into an integer, false otherwise. Returns false for null strings or strings that cannot be parsed into an integer.
    • isFloatingDecimal

      @Contract(pure=true) public boolean isFloatingDecimal(int index)
      Checks if the value obtained from the argument at the specified index can be successfully parsed into a floating-point decimal using Double.parseDouble.
      Parameters:
      index - The index of the argument from which the value is retrieved.
      Returns:
      true if the value at the specified argument index can be parsed into a floating-point decimal, false otherwise. Returns false for null or empty values obtained from the argument. Also returns false for values that cannot be parsed into a floating-point decimal.
    • isFloatingDecimal

      @Contract(pure=true) public boolean isFloatingDecimal(String string)
      Checks if the given string can be successfully parsed into a floating decimal using Double.parseDouble. Supports primitive types such as Integer, Float, Double, Long, etc.
      Parameters:
      string - The input string to be checked for its ability to be parsed into a decimal.
      Returns:
      true if the string can be parsed into a decimal, false otherwise. Returns false for null strings or strings that cannot be parsed into a decimal.
    • checkCooldown

      public boolean checkCooldown()
      This method checks if the current command sender has a cooldown on the command that is associated with current CommandArguments object.
      For example,
      
           @Command(name = "test")
           @Cooldown(cooldown = 5)
           public void testCommand(CommandArguments args) {
               if (args.getLength() != 1) {
                 	// if sender has cooldown, execution will be end here
                 	args.checkCooldown();
               }
      
               args.sendMessage("Test command successfully executed.");
           }
       
      Note that execution will be stopped if this method returns true.
      Returns:
      true if the sender has a cooldown on this command
    • getOption

      @Nullable @Contract(pure=true) public @Nullable List<String> getOption(@NotNull @NotNull String option)
    • findOption

      @Contract(pure=true) public Optional<List<String>> findOption(@NotNull @NotNull String option)
    • isFlagPresent

      @Contract(pure=true) public boolean isFlagPresent(@NotNull @NotNull String flag)