-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.java
More file actions
144 lines (122 loc) · 4.36 KB
/
Copy pathTimer.java
File metadata and controls
144 lines (122 loc) · 4.36 KB
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
141
142
143
144
package com.rappytv.speedruntimer.util;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.format.NamedTextColor;
import net.labymod.api.client.component.format.TextDecoration;
import java.util.TimerTask;
public class Timer {
private static final String displayFormat = "%s:%s:%s";
private static final java.util.Timer timer = new java.util.Timer();
private final Runnable onCountdownComplete;
private TimerState state = TimerState.OFF;
private TimerDirection direction = TimerDirection.COUNT_DOWN;
private long seconds = 0;
public Timer(Runnable onCountdownComplete) {
this.onCountdownComplete = onCountdownComplete;
}
public void startCountUp() {
if(this.state != TimerState.OFF) return;
this.direction = TimerDirection.COUNT_UP;
this.seconds = 0;
this.start();
}
public void startCountDown(long seconds) {
if(this.state != TimerState.OFF) return;
this.direction = TimerDirection.COUNT_DOWN;
this.seconds = seconds;
this.start();
}
public void reset() {
if(this.state == TimerState.OFF) return;
this.direction = TimerDirection.COUNT_UP;
this.seconds = 0;
this.state = TimerState.OFF;
}
public Component getDisplay() {
long hours = this.seconds / 3600;
long minutes = (this.seconds % 3600) / 60;
long seconds = this.seconds % 60;
Component component = Component.text(String.format(
displayFormat,
(String.valueOf(hours).length() > 1 ? "" : "0") + hours,
(String.valueOf(minutes).length() > 1 ? "" : "0") + minutes,
(String.valueOf(seconds).length() > 1 ? "" : "0") + seconds
));
if(this.state == TimerState.PAUSED) component.decorate(TextDecoration.ITALIC).color(NamedTextColor.RED);
else component.color(NamedTextColor.GREEN);
return component.decorate(TextDecoration.BOLD);
}
private void start() {
this.state = TimerState.RUNNING;
timer.schedule(new TimerTask() {
@Override
public void run() {
if(Timer.this.state == TimerState.OFF) this.cancel();
if(Timer.this.state == TimerState.PAUSED) return;
if(Timer.this.direction == TimerDirection.COUNT_UP) Timer.this.seconds++;
else if(Timer.this.direction == TimerDirection.COUNT_DOWN) {
Timer.this.seconds--;
if(Timer.this.seconds < 0) {
Timer.this.seconds = 0;
Timer.this.state = TimerState.PAUSED;
Timer.this.onCountdownComplete.run();
}
}
}
}, 1000, 1000);
}
public long resolveSeconds(String timeValue) {
String format = timeValue.substring(timeValue.length() - 1);
long duration;
try {
duration = Integer.parseInt(
timeValue.length() > 1
? timeValue.substring(0, timeValue.length() - 1)
: timeValue
);
} catch(NumberFormatException e) {
return -1;
}
return switch(format) {
case "s" -> duration;
case "m" -> duration * 60;
case "h" -> duration * 60 * 60;
case "d" -> duration * 60 * 60 * 24;
case "w" -> duration * 60 * 60 * 24 * 7;
case "y" -> duration * 60 * 60 * 24 * 7 * 52;
default -> {
try {
yield this.resolveSeconds(Integer.parseInt(timeValue) + "s");
} catch (NumberFormatException e) {
yield -1;
}
}
};
}
public TimerDirection getDirection() {
return this.direction;
}
public void setDirection(TimerDirection direction) {
this.direction = direction;
}
public TimerState getState() {
return this.state;
}
public void setState(TimerState state) {
this.state = state;
}
public long getSeconds() {
return this.seconds;
}
public void setSeconds(long seconds) {
this.seconds = seconds;
}
public enum TimerDirection {
COUNT_UP,
COUNT_DOWN
}
public enum TimerState {
RUNNING,
PAUSED,
OFF
}
}