package xyz.upperlevel.uppercore.task;

import lombok.Getter;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.function.Consumer;

public class Countdown {
    @Getter
    private final Plugin plugin;

    @Getter
    private BukkitRunnable task;

    @Getter
    private final long startAt, repeatEach;

    @Getter
    private long currentTick;

    private final Consumer<Long> onTick;
    private final Runnable onEnd;

    public Countdown(Plugin plugin, long startAt, long repeatEach, Consumer<Long> onTick, Runnable onEnd) {
        this.plugin = plugin;
        this.startAt = startAt;
        this.repeatEach = repeatEach;
        this.onTick = onTick;
        this.onEnd = onEnd;
    }

    public void start() {
        if (task != null) {
            stop();
        }
        currentTick = startAt;
        task = new BukkitRunnable() {
            @Override
            public void run() {
                onTick.accept(currentTick / repeatEach);
                if (currentTick > 0)
                    currentTick -= repeatEach;
                else {
                    onEnd.run();
                    stop();
                }
            }
        };
        task.runTaskTimer(plugin, 0, repeatEach);
    }

    public boolean isStarted() {
        return task != null;
    }

    public void stop() {
        if (task != null) {
            task.cancel();
            task = null;
        }
    }

    public String format(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date(currentTick * 50));
    }
}

@@ -77,64 +77,74 @@
 kit.
-plugin.Plugin;%0Aimport org.bukkit.scheduler.BukkitRunnabl
+scheduler.BukkitRunnable;%0Aimport xyz.upperlevel.uppercore.Uppercor
 e;%0A%0A
@@ -245,16 +245,25 @@
 %0Apublic 
+abstract 
 class Co
@@ -274,79 +274,15 @@
 own 
-%7B%0A    @Getter%0A    private final Plugin plugin;%0A%0A    @Getter%0A    private
+extends
  Buk
@@ -293,22 +293,17 @@
 unnable 
-task;%0A
+%7B
 %0A    @Ge
@@ -399,84 +399,8 @@
 k;%0A%0A
-    private final Consumer%3CLong%3E onTick;%0A    private final Runnable onEnd;%0A%0A
     
@@ -420,23 +420,8 @@
 own(
-Plugin plugin, 
 long
@@ -449,80 +449,11 @@
 Each
-, Consumer%3CLong%3E onTick, Runnable onEnd) %7B%0A        this.plugin = plugin;
+) %7B
 %0A   
@@ -523,66 +523,8 @@
 ch;%0A
-        this.onTick = onTick;%0A        this.onEnd = onEnd;%0A
     
@@ -560,35 +560,38 @@
         
-if (tas
+currentTic
 k 
-!
 = 
-null) %7B
+startAt;
 %0A       
@@ -595,29 +595,91 @@
     
-    stop(
+runTaskTimer(Uppercore.plugin(), 0, repeatEach
 );%0A    
+%7D%0A%0A
     
-%7D
+public long getTime() %7B
 %0A   
@@ -675,32 +675,39 @@
 ime() %7B%0A        
+return 
 currentTick = st
@@ -706,65 +706,115 @@
 ick 
-= startAt
+/ repeatEach
 ;%0A    
+%7D%0A%0A
     
-task = new BukkitRunnable() %7B%0A        
+protected abstract void onTick(long time);%0A%0A    protected abstract void onEnd();%0A%0A
     
@@ -823,24 +823,16 @@
 verride%0A
-        
     publ
@@ -859,29 +859,14 @@
     
-        onTick.accept
+onTick
 (cur
@@ -897,24 +897,16 @@
         
-       
- 
 if (curr
@@ -930,24 +930,16 @@
         
-       
- 
 currentT
@@ -957,24 +957,16 @@
 atEach;%0A
-        
         
@@ -988,25 +988,13 @@
     
-        
 onEnd
-.run
 ();%0A
@@ -1009,20 +1009,20 @@
     
-        stop
+super.cancel
 ();%0A
@@ -1033,218 +1033,374 @@
     
-   
-     
 %7D%0A    
-        %7D%0A        %7D;%0A        task.runTaskTimer(plugin, 0, repeatEach);%0A    %7D%0A%0A    public boolean isStarted() %7B%0A        return task != null;%0A
+%7D%0A%0A    public String toString(String pattern) %7B%0A        return new SimpleDateFormat(pattern).format(new Date(currentTick * 50));%0A    %7D%0A%0A    public static Countdown create(long startAt, long repeatEach, Consumer%3CLong%3E onTick, Runnable onEnd) %7B%0A        return new Countdown(startAt, repeatEach) %7B%0A 
     
-%7D%0A%0A
+  
     
-public void stop() %7B%0A        if (task != null
+ @Override%0A            protected void onTick(long tick
 ) %7B%0A
@@ -1415,187 +1415,159 @@
     
+ 
-task.cancel();%0A            task = null;%0A        %7D
+   onTick.accept(tick);%0A            %7D%0A%0A            @Override
 %0A    
-%7D%0A%0A
+    
     p
-ublic String format(String pattern) %7B%0A        return new SimpleDateFormat(pattern).format(new Date(currentTick * 50))
+rotected void onEnd() %7B%0A                onEnd.run();%0A            %7D%0A        %7D
 ;%0A  

Enhance Countdown class