Advertisement
JolyJDIA

Untitled

Jun 5th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package jolyjdia.cooldown;
  2.  
  3. import org.bukkit.scheduler.BukkitRunnable;
  4.  
  5. public abstract class CountDownTimer {
  6.     private final BukkitRunnable runnable = new BukkitRunnable() {
  7.         @Override
  8.         public void run() {
  9.             final long millisLeft = stopTimeInFuture - System.currentTimeMillis();
  10.  
  11.             if (millisLeft <= 0) {
  12.                 onFinish();
  13.                 super.cancel();
  14.             } else {
  15.                 onTick(millisLeft);
  16.             }
  17.         }
  18.     };
  19.     private final long millisInFuture;
  20.     private final long countdownInterval;
  21.     private long stopTimeInFuture;
  22.  
  23.     protected CountDownTimer(long millisInFuture, long countDownInterval) {
  24.         this.millisInFuture = millisInFuture;
  25.         this.countdownInterval = countDownInterval;
  26.     }
  27.     public final void start(JavaPlugin plugin) {
  28.         if (millisInFuture <= 0) {
  29.             onFinish();
  30.         } else {
  31.             this.stopTimeInFuture = System.currentTimeMillis() + millisInFuture;
  32.             this.runnable.runTaskTimer(plugin, countdownInterval, countdownInterval);
  33.         }
  34.     }
  35.  
  36.     public void cancel() {
  37.         this.runnable.cancel();
  38.     }
  39.  
  40.     public abstract void onTick(long millisUntilFinished);
  41.     public abstract void onFinish();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement