Cooldowns
What is it?
Cooldown.java
is a pretty easy way to make a delay before using a command/item etc. Cooldown.java
provides quite a bit of flexibility. It also gives you the ability to specify what happens after the cooldown.
Cooldown will not save after restart.
How to use it
After 1.8-beta cooldown class is using ticks in cooldownTime argument, not seconds.
Declare Cooldown
variable.
private Cooldown<Player> cooldown = new Cooldown<>();
// You also can use string, UUID and etc. for T arg.
Adding cooldown
// Just add cooldown
cooldown.addNewCooldown(player, cooldownTime);
// Rewrite cooldown if exists
cooldown.addNewCooldown(player, cooldownTime, true);
// Execute runnable after cooldown is complete.
cooldown.addNewCooldown(player, cooldownTime, new Runnable());
// Rewirite cooldown if exists and execute runnable after cooldown is complete.
cooldown.addNewCooldown(player, cooldownTime, new Runnable(), true);
Removing and checking cooldowns
cooldown.containCooldown(player); // Check for cooldown
cooldown.removeCooldown(player); // Remove cooldown
CooldownTask
CooldownTask
class is a simple implementation of BukkitRunnable
with some QOL improvements.
Example
Cooldown<T> cooldown = new Cooldown<T>(); // replace T with class like player or etc.
cooldown.addCooldownTask(T, 60); // 60 ticks = 3 seconds
And done.
So whats special in this class? Basicly you can change task ticks left and runnable when task is already running. Also you can get ticks left via getTicksLeft
method. But Cooldown
class is using BukkitRunnable
class in his map, so you need to cast BukkitRunnable
to CooldownTask
class.
CooldownTask mytask = (CooldownTask) cooldown.getCooldowns().get(T);
Debug.info("Tick left: " + mytask.getTickLeft()); // Tick left: 60
// Lets change tick left variable.
mytask.setTime(120); // 6 seconds
Debug.info("Tick left: " + mytask.getTickLeft()); // Tick left: 120
You also can change runnable when task is already running and calling that runnable either.
CooldownTask mytask = (CooldownTask) cooldown.getCooldowns().get(T);
Runnable newRunnable = new Runnable() {
@Override
public void run() {
Debug.info("New runnable!");
}
}
CooldownTask task1 = new CooldownTask().setTime(60); // Task without runnable
CooldownTask task2 = new CooldownTask().setTime(60); //Task without INITIAL runnable
// Starting our tasks.
task1.runTaskTimer(plugin, 0, 1);
task2.runTaskTimer(plugin, 0, 1);
task2.setRunnable(newRunnable);
// Task 1 after completion: nothing
// Task 2 after completion: 'New runnable!' in console
Using it
You can use CooldownTask class with or without Cooldown class. And this is a reason why methods in CooldownTask are named setTime
and getTime
.
Last updated
Was this helpful?