Skip to content

Command Cooldowns

WARNING

Command cooldowns only work for players.

You might want to spam-proof your commands, which is where the command cooldown system comes into play. Stellar will automatically handle the checking of cooldowns, while allowing you to customize it as much as you want.

You can either set add cooldowns with a duration and an execution, or with a duration and a message.

TIP

If you omit the TimeUnit in any cooldown method, and it will default to milliseconds.

Cooldown Executions

You can add a cooldown execution as such:

Java
new StellarCommand("message")
        .addCooldown(5, TimeUnit.SECONDS, (context, remaining) -> {
            context.getSender().sendMessage(ChatColor.RED + "Please wait ${TimeUnit.MILLISECONDS.toSeconds(remaining)} more seconds!"); // this is also the default message
        })
        .addOnlinePlayersArgument("target")
        .addStringArgument("message", StringType.PHRASE)
        .addExecution(Player.class, context -> {
            Player target = context.getArgument("target");
            String message = context.getArgument("message");
            target.sendMessage(context.getSender().getName() + " -> " + message + ".");
        })
        .register();

The above code will run that message the player whenever they run that command twice within 5 seconds. The function itself will return the CommandContext and the remaining duration in milliseconds, as a long.

Cooldown Message

For a simpler system, you can instead return cooldown messages which will be sent to the player. Cooldown messages use cooldown executions under the hood.

You have three types of cooldown message methods that you can use:

  • addComponentMessageCooldown — this will send the returning Component to the player.
  • addMessageCooldown — this will send the returning String parsed by MiniMessage to the player.
  • addRawMessageCooldown — this will send the returning String with no modifications to the player.
Java
new StellarCommand("message")
        .addComponentMessageCooldown(
                5,
                TimeUnit.SECONDS,
                (context, remaining) -> Component.text("Please wait " + TimeUnit.MILLISECONDS.toSeconds(remaining) + " more seconds!", NamedTextColor.RED)
        )
        .addOnlinePlayersArgument("target")
        .addStringArgument("message", StringType.PHRASE)
        .addExecution(Player.class, context -> {
            Player target = context.getArgument("target");
            String message = context.getArgument("message");
            target.sendMessage(context.getSender().getName() + " -> " + message + ".");
        })
        .register(this);
Java
new StellarCommand("message")
        .addMessageCooldown(
                5,
                TimeUnit.SECONDS,
                (context, remaining) -> "<red>Please wait " + TimeUnit.MILLISECONDS.toSeconds(remaining) + " more seconds!"
        )
        .addOnlinePlayersArgument("target")
        .addStringArgument("message", StringType.PHRASE)
        .addExecution(Player.class, context -> {
            Player target = context.getArgument("target");
            String message = context.getArgument("message");
            target.sendMessage(context.getSender().getName() + " -> " + message + ".");
        })
        .register(this);
Java
new StellarCommand("message")
        .addStringMessageCooldown(
                5,
                TimeUnit.SECONDS,
                (context, remaining) -> ChatColor.RED + "Please wait " + TimeUnit.MILLISECONDS.toSeconds(remaining) + " more seconds!"
        )
        .addOnlinePlayersArgument("target")
        .addStringArgument("message", StringType.PHRASE)
        .addExecution(Player.class, context -> {
            Player target = context.getArgument("target");
            String message = context.getArgument("message");
            target.sendMessage(context.getSender().getName() + " -> " + message + ".");
        })
        .register(this);

Released under the MIT License.