Advertisement
acuddlyheadcrab

CountdownPlugin

Apr 24th, 2012
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. package com.acuddlyheadcrab.testplugin;
  2.  
  3. import java.util.Random;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.event.Listener;
  7. import org.bukkit.plugin.java.JavaPlugin;
  8.  
  9. public class TestPlugin extends JavaPlugin implements Listener{
  10.    
  11.     public TestPlugin plugin = this;
  12.    
  13.     private int countdown_secs = 0; //hehe secs... sex
  14.    
  15.     public void onEnable() {
  16.         /* this is just used to  get a random number between 3 and 13 for demonstration
  17.         +3 so that we don't have 0 for the countdown */
  18.         int rand_seconds = new Random().nextInt(7)+3;
  19.        
  20.         System.out.println("Starting countdown");
  21.         startCountdown(rand_seconds);
  22.     }
  23.    
  24.     public void onDisable() {}
  25.    
  26.     public void startCountdown(final int seconds){
  27.         long increment = 20; // just so you know, 1 tick == 1/20 second
  28.         countdown_secs = seconds;
  29.        
  30.         Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  31.             @Override
  32.             public void run() {
  33.                
  34.                
  35. //              this is where you check if the countdown is done yet
  36.                 if(countdown_secs<1){
  37.                     Bukkit.getScheduler().cancelTasks(plugin);
  38.                     System.out.println("Boom! Countdown is done!!");
  39.                 }
  40.                
  41.                 displayCountdown();
  42.                
  43.                 countdown_secs--;
  44.                
  45.             }
  46.         }, increment, increment);
  47.        
  48.        
  49.     }
  50.  
  51.     public void displayCountdown() {
  52.         // put however you want to display the countdown info here
  53.         int seconds = countdown_secs;
  54.         System.out.println("Countdown: "+seconds);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement