Skip to content

Command arguments

Berke Akçen edited this page Jul 19, 2024 · 10 revisions

Table of contents

Command Argument Basics

CommandArguments is a utility class that includes the basic parameters related Bukkit commands and bunch of useful methods to improve your code.

Basic Methods

    // We have created a basic command named "example"
    @Command(
            name = "example"
    )
    public void exampleCommandMethod(CommandArguments arguments) {
      // To get command sender use CommandArguments#getSender method
      CommandSender sender = arguments.getSender();

      // To check who is the sender
      if (arguments.isSenderPlayer()) {
          // now sender is player
      } else {
          // and now sender is console
      }

      // To get as Bukkit's command use CommandArguments#getCommand method
      // After that you can be able to get command's name, description, permission, etc.
      org.bukkit.command.Command bukkitCommand = arguments.getBukkitCommand();

      // To get Command object associated with this arguments.
      me.despical.commandframework.annotations.Command command = arguments.getCommand();

      // To get label of command use CommandArguments#getLabel method
      String label = arguments.getLabel();

      // To get arguments of command use CommandArguments#getArguments() method
      // and don't forget these arguments is not all the parts of default arguments
      // because they'll be splitted after sub-command parts
      String[] args = arguments.getArguments();
       
      // To get specific argument without receiving argument array
      // There is no exception during this operation but also don't forget
      // that method can be null if index is out of bounds.
      String firstArgument = arguments.getArgument(0);

      // To get arguments array is empty or not without receiving array.
      boolean isArgsEmpty = arguments.isArgumentsEmpty();

      // To send message to command sender without receivingits object
      arguments.sendMessage("Hey there!");
 
      // To check if command sender has permission without receiving sender object
      if (arguments.hasPermission("command.framework") {
          // sender has the given permission
      } else {
          // sender has not the given permission
      }

      // To get arguments length without receiving string array
      int argumentsLength = arguments.getLength();

      // Gets the first argument, can be null.
      String nullableArgument = arguments.getArgument(0);

      // Gets the first argument, returns the default value in case index is out of bounds
      String notNullArgument = arguments.getArgument(0, "default value");    
}

Useful Methods

    // We have created a basic command named "example"
    @Command(
            name = "example"
    )
    public void exampleCommandMethod(CommandArguments arguments) {
       // Gets player from the given name, in Optional type
       Optional<Player> playerFromName = arguments.getPlayer("Despical");
       playerFromName.ifPresent(player -> player.sendMessage("Hello World!"));

       // Gets player from the given argument index, in this case first argument
       Optional<Player> playerFromArgs = arguments.getPlayer(0);
       playerFromArgs .ifPresent(player -> player.sendMessage("Hello World!"));

      // Assume that our arguments array is = ["example", "array", "with", "multiple", "arguments"]
      
      String concatenatedArgs = arguments.concatArguments();
      // concatenatedArgs will be equals to = "example array with multiple arguments"

      // from index is inclusive, to index is exclusive
      String concatenatedArgs = arguments.concatRangeOf(1, 4);
      // concatenatedArgs will be equals to = "array with multiple arguments"

      // Checks if the first argument is numeric or not.
      // Does not checks for bounds so should be careful about the overflowing.
      // This method will return false for non-positive or floating decimals.
      boolean isNumeric = arguments.isNumeric(0); // Method takes string values as well other than argument indexes.

      // This method checks if the first argument is an integer or not,
      // checks for bounds so it can throw exception if the integer limit is exceed in both positive or negative ways.
      // And will return false if the given argument is a floating point.
      boolean isInteger = arguments.isInteger(0); // Method takes string values as well other than argument indexes.

      // This method checks if the first argument is a floating decimal or not,
      // checks for bounds so it can throw exception if the double limit is exceed in both positive or negative ways.
      // And will return true if the given argument is an integer.
      boolean isFloatingDecimal = arguments.isFloatingDecimal(0); // Method takes string values as well other than argument indexes.

      // To check if the command sender has cooldown for this command.
      boolean hasCooldown = arguments.hasCooldwon();

      // To send pre-defined error messages in Message enum.
      // Use Message#setMessage method to change these error messages.
      boolean success = arguments.sendMessage(Message.SHORT_ARG_SIZE);
}