Skip to content

Cooldown

David edited this page May 30, 2025 · 3 revisions

In order to create a Cooldown, you first need to get your platform's factory.

How to do this in Bukkit:

private CooldownFactory<Player> cooldownFactory;

@Override
public void onEnable()
{
    //must be inside onEnable()
    this.cooldownFactory = BukkitCooldownFactory.createFor(this);
}

Before proceeding, make sure you have read about CooldownFuture, otherwise you will get confused.

The recommended way is to use the builder:

Cooldown<Player> chatCooldown = this.cooldownFactory.newBuilder()
    .withDefaultTime(Duration.ofSeconds(3))
    .rejectsWith(message(ChatColor.RED + "You can talk only in %time%!"))
    .whenOver(message(ChatColor.GREEN + "You may now speak again."))
    .build();

//somewhere else
this.chatCooldown.put(player);

//in the chat event handler
//#test is more powerful than #isOn because it additionally runs the rejection logic
if(!this.chatCooldown.test(player))
    event.setCancelled(true);

If you don't need anything fancy - just the normal operations:

Cooldown<Player> cooldown = this.cooldownFactory.newCooldown();

//somewhere else
this.cooldown.put(player, Duration.ofSeconds(30));

//in a third place
if(this.cooldown.isOn(player))
{
    player.sendMessage(ChatColor.RED + "You must wait 30 seconds before repeating that.");
    return;
}

player.teleport(somewhereCool);
Clone this wiki locally