The BukkitRunnable is a Runnable found in Bukkit. It's possible to schedule a task directly from a BukkitRunnable, and also cancel it from inside itself.
Important: The time on the tasks is measured in Ticks. A second has 20 ticks.
Non-RepeatingTask:
JavaPlugin plugin; //Your plugin instance
Long timeInSeconds = 10;
Long timeInTicks = 20 * timeInSeconds;
new BukkitRunnable() {
@Override
public void run() {
//The code inside will be executed in {timeInTicks} ticks.
}
}.runTaskLater(plugin, timeInTicks); // Your plugin instance, the time to be delayed.
Repeating Task:
JavaPlugin plugin; //Your plugin instance
Long timeInSeconds = 10;
Long timeInTicks = 20 * timeInSeconds;
new BukkitRunnable() {
@Override
public void run() {
//The code inside will be executed in {timeInTicks} ticks.
//After that, it'll be re-executed every {timeInTicks} ticks;
//Task can also cancel itself from running, if you want to.
if (boolean) {
this.cancel();
}
}
}.runTaskTimer(plugin, timeInTicks, timeInTicks); //Your plugin instance,
//the time to wait until first execution,
//the time inbetween executions.