Skip to content

Instantly share code, notes, and snippets.

@Garris0n-
Last active January 12, 2022 00:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garris0n-/f0562ae415746a69ff44 to your computer and use it in GitHub Desktop.
Save Garris0n-/f0562ae415746a69ff44 to your computer and use it in GitHub Desktop.
Example of a self-cancelling Bukkit task.
public void countdown(final Player player){ //A method
new BukkitRunnable(){ //BukkitRunnable, not Runnable
int countdown = 10; //Instance variable in our anonymous class to easily hold the countdown value
@Override
public void run(){
if(countdown <= 0 || !player.isOnline()){ //countdown is over or player left the server, just two example reasons to exit
this.cancel(); //cancel the repeating task
return; //exit the method
}
player.sendMessage(ChatColor.RED + "There are " + countdown + " seconds until timtower gets moderator powers."); //Example usage
countdown--; //decrement
}
}.runTaskTimer(plugin, 0, 20); //Repeating task with 0 ticks initial delay, run once per 20 ticks (one second). Make sure you pass a valid instance of your plugin.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment