-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClockUtils.java
More file actions
163 lines (142 loc) · 5.77 KB
/
ClockUtils.java
File metadata and controls
163 lines (142 loc) · 5.77 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package wtf.nucker.spigotutilities.utils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Consumer;
/**
* A useful class for using time in your plugins
* @see java.util.concurrent.TimeUnit This TimeUnit class for more usful time converstion
*/
public class ClockUtils {
private static JavaPlugin plugin = JavaPlugin.getProvidingPlugin(getClass());
/**
* A method to convert ticks to seconds
* @param ticks the amount of ticks you want to convert
* @return the ticks provided as seconds
*/
public static long ticksToSeconds(long ticks) {
return ticks * 20;
}
/**
* A method to convert seconds to ticks
* @param seconds the amount of seconds you want to convert
* @return the seconds provided as ticks
*/
public static long secondsToTicks(long seconds) {
return seconds / 20;
}
/**
* A method to run countdowns easily in your plugins
* @param amount The amount of seconds you want to countdown from
* @param runningCode The code ran every time one second of the countdown passes (includes the last tick).
* The consumer extends the {@link CountingRunnable} class.
* @param endCode The code ran at the end when the countdown reaches its final tick
* The consumer extends the {@link CountingRunnable} class.
*/
public static void countDown(int amount, Consumer<CountingRunnable> runningCode , Consumer<CountingRunnable> endCode) {
new BukkitRunnable() {
int i = amount;
final CountingRunnable countingRunnable = new CountingRunnable(i, this);
public void run() {
if(i < 0) this.cancel();
if(i == 0) {
endCode.accept(countingRunnable);
this.cancel();
}
runningCode.accept(countingRunnable);
i--;
countingRunnable.setAmount(i);
}
}.runTaskTimer(ClockUtils.plugin, 0L, 20L);
}
/**
* A method to run count ups easily in your plugins
* @param amount The amount of seconds you want to countdown from
* @param runningCode The code ran every time one second of the countdown passes (includes the last tick).
* The consumer extends the {@link CountingRunnable} class.
* @param endCode The code ran at the end when the countdown reaches its final tick
* The consumer extends the {@link CountingRunnable} class.
*/
public static void countUp(int amount, Consumer<CountingRunnable> runningCode , Consumer<CountingRunnable> endCode) {
new BukkitRunnable() {
int i = 0;
final CountingRunnable countingRunnable = new CountingRunnable(i, this);
public void run() {
if(i == amount) {
endCode.accept(countingRunnable);
this.cancel();
}
runningCode.accept(countingRunnable);
i++;
countingRunnable.setAmount(i);
}
}.runTaskTimer(ClockUtils.plugin, 0L, 20L);
}
/**
* Runs your code after a delay
* @param delay the delay between you calling the method and running the code.
* @param runCode The code ran when the delay ends. The consumer extends the {@link CountingRunnable} class.
*/
public static void runCodeLater(int delay, Consumer<BukkitRunnable> runCode) {
ClockUtils.countDown(delay,c->{}, c -> {
if(c.getAmount() == 0) {
runCode.accept(c.getRunnable());
}
});
}
/**
* Runs code after delay on repeat until.
* @param delay The delay between each code run
* @param runCode The code ran when the delay is completed
* @return The runnable instance so you can cancel it if you want
*/
public static BukkitRunnable runInterval(int delay, Consumer<CountingRunnable> runCode) {
BukkitRunnable runnable = new BukkitRunnable() {
int i = delay;
final CountingRunnable countingRunnable = new CountingRunnable(i, this);
public void run() {
if(i < 0) this.i = delay;
runCode.accept(countingRunnable);
i--;
countingRunnable.setAmount(i);
}
};
runnable.runTaskTimer(ClockUtils.plugin, 0L, 20L);
return runnable;
}
/**
* A class to get the amount a runnable is on as well as the runnable instance
*/
public static class CountingRunnable {
private final BukkitRunnable runnable;
private int amount;
/**
* Private as its only meant to be used in {@link ClockUtils}
* @param amount The amount the runnable is currently on
* @param runnable An instance of the runnable
*/
private CountingRunnable(int amount, BukkitRunnable runnable) {
this.runnable = runnable;
this.amount = amount;
}
/**
* Get instance of the runnable
* @return the instance of the runnable
*/
public BukkitRunnable getRunnable() {
return runnable;
}
/**
* Get the count the runnable is on
* @return the amount the runnable is on
*/
public int getAmount() {
return amount;
}
@SuppressWarnings("UnusedReturnValue")
private int setAmount(int newAmount) {
this.amount = newAmount;
return this.getAmount();
}
}
}