Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jolyjdia.cooldown;
- import org.bukkit.scheduler.BukkitRunnable;
- public abstract class CountDownTimer {
- private final BukkitRunnable runnable = new BukkitRunnable() {
- @Override
- public void run() {
- final long millisLeft = stopTimeInFuture - System.currentTimeMillis();
- if (millisLeft <= 0) {
- onFinish();
- super.cancel();
- } else {
- onTick(millisLeft);
- }
- }
- };
- private final long millisInFuture;
- private final long countdownInterval;
- private long stopTimeInFuture;
- protected CountDownTimer(long millisInFuture, long countDownInterval) {
- this.millisInFuture = millisInFuture;
- this.countdownInterval = countDownInterval;
- }
- public final void start(JavaPlugin plugin) {
- if (millisInFuture <= 0) {
- onFinish();
- } else {
- this.stopTimeInFuture = System.currentTimeMillis() + millisInFuture;
- this.runnable.runTaskTimer(plugin, countdownInterval, countdownInterval);
- }
- }
- public void cancel() {
- this.runnable.cancel();
- }
- public abstract void onTick(long millisUntilFinished);
- public abstract void onFinish();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement