/
Timer.java
140 lines (116 loc) · 3.69 KB
/
Timer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package fr.aumgn.bukkitutils.timer;
import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import com.google.common.base.Stopwatch;
public abstract class Timer implements Runnable {
private static final int TICKS_PER_SECONDS = 20;
private final Plugin plugin;
private final int majorDelay;
private final int minorDelay;
private final String format;
private final String endMessage;
private Runnable runnable;
private int remainingTime;
private int taskId;
private Stopwatch watch;
private int currentDelay;
private int pauseDelay;
private Timer(boolean dummy, Plugin plugin, TimerConfig config, int seconds) {
this.plugin = plugin;
this.majorDelay = config.getMajorDuration();
this.minorDelay = config.getMinorDuration();
this.format = config.getFormat();
this.endMessage = config.getEndMessage();
this.taskId = -1;
this.remainingTime = seconds;
this.currentDelay = 0;
}
public Timer(Plugin plugin, TimerConfig config, int seconds, Runnable runnable) {
this(true, plugin, config, seconds);
this.runnable = runnable;
}
public Timer(Plugin plugin, TimerConfig config, int seconds) {
this(false, plugin, config, seconds);
this.runnable = new Runnable() {
public void run() {
onFinish();
}
};
}
private void scheduleAndPrintTime(int delay) {
long minutes = TimeUnit.SECONDS.toMinutes(remainingTime);
String msg = String.format(format, minutes, remainingTime % 60);
sendTimeMessage(getCurrentColor() + msg);
schedule(delay);
}
private void schedule(int delay) {
currentDelay = delay;
watch = new Stopwatch();
watch.start();
taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin, this, (long) delay * TICKS_PER_SECONDS);
}
private ChatColor getCurrentColor() {
if (remainingTime >= majorDelay) {
return ChatColor.YELLOW;
} else if (remainingTime >= minorDelay){
return ChatColor.GOLD;
} else {
return ChatColor.RED;
}
}
public int getRemainingTime() {
return remainingTime;
}
public void start() {
remainingTime -= currentDelay;
if (remainingTime > majorDelay) {
schedule(majorDelay);
} else if (remainingTime > minorDelay) {
schedule(minorDelay);
} else if (remainingTime > 0) {
schedule(1);
} else {
runnable.run();
}
}
@Override
public void run() {
remainingTime -= currentDelay;
if (remainingTime > majorDelay) {
scheduleAndPrintTime(majorDelay);
} else if (remainingTime > minorDelay) {
scheduleAndPrintTime(minorDelay);
} else if (remainingTime > 0) {
scheduleAndPrintTime(1);
} else {
runnable.run();
}
}
public void cancel() {
if (taskId != -1) {
Bukkit.getScheduler().cancelTask(taskId);
taskId = -1;
}
}
public void pause() {
cancel();
watch.stop();
pauseDelay = (int) watch.elapsedTime(TimeUnit.SECONDS);
remainingTime -= pauseDelay;
}
public void resume() {
int delay = currentDelay - pauseDelay;
if (delay > 0) {
scheduleAndPrintTime(delay);
} else {
runnable.run();
}
}
public void onFinish() {
sendTimeMessage(endMessage);
}
public abstract void sendTimeMessage(String string);
}