Skip to content

Cooldowns

Berke Akçen edited this page Jul 19, 2024 · 1 revision

Table of contents

There are 2 type of cooldowns in Command Framework.

  1. General cooldowns: This type of cooldowns are automatically handled by the framework when the commands executed.
  2. Custom cooldowns: This type of cooldowns must be handled by the user in a command's method body using the CommandArguments#hasCooldown method. Second type of cooldowns can directly stop the execution of the code so users do not need to put a return statement in a method body.

General Cooldowns

This type of cooldowns are automatically handled by the Command Framework. To create this type of cooldown is very easy, all have to do is create a command and annotate it with @Cooldown annotation, the rest will be handled by the framework.

For this type, the cooldown can check can not be done by the user using the CommandArguments#hasCooldown method so the cooldown check will be done before the command method's body executed.

public class Main extends JavaPlugin {
   
   @Override
   public void onEnable() {
      CommandFramework commandFramework = new CommandFramework(this);
      commandFramework.registerCommands(this);
   }

   @Command(name = "test")
   @Cooldown(cooldown = 5)
   public void testCommand(CommandArguments args) {
      args.sendMessage("Test command executed successfully.");
   }
}

Cooldowns in a Method Body.

CommandArguments#hasCooldown, this method can not be invoked in a tab completer and returns true if player has cooldown and should have to wait to perform this command again. Also, this method shouldn't be used to in a condition because it will force method to leave right after its execution, if the command sender has cooldown.


public class Main extends JavaPlugin {
   
   @Override
   public void onEnable() {
      CommandFramework commandFramework = new CommandFramework(this);
      commandFramework.registerCommands(this);
      // To be able to call CommandArguments#hasCooldown method, this option must be enabled.
      commandFramework.enableOption(Option.CUSTOM_COOLDOWN_CHECKER);
   }

   @Command(name = "test")
   @Cooldown(cooldown = 5)
   public void testCommand(CommandArguments args) {
      args.checkCooldown();
      // In the first try, the command sender see the message below.
      // In the second try, if they haven't waited for 5 seconds, in this case, the code will be stopped right after
      // args.checkCooldown() method invoked and the sender will receive a message that says they have to wait a little bit.
      args.sendMessage("Test command executed successfully.");
   }
}