Last active
January 12, 2022 00:07
-
-
Save Garris0n-/f0562ae415746a69ff44 to your computer and use it in GitHub Desktop.
Example of a self-cancelling Bukkit task.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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