-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Duration.java
368 lines (290 loc) · 10.5 KB
/
Duration.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package net.aufdemrand.denizen.objects;
import com.google.common.primitives.Ints;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.ChatColor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Durations are a more convenient way to get a 'unit of time' within Denizen.
*
*/
public class Duration implements dObject {
// Use regex pattern matching to easily determine if a string
// value is a valid Duration.
final static Pattern match =
Pattern.compile("(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d|)(?:(?:-\\d+.\\d+|.\\d+|\\d+)(?:t|m|s|h|d|))?",
Pattern.CASE_INSENSITIVE);
// Define a 'ZERO' Duration
final public static Duration ZERO = new Duration(0);
/**
* Gets a Duration Object from a dScript argument. Durations must be a positive
* number. Can specify the unit of time by using one of the following: T=ticks, M=minutes,
* S=seconds, H=hours, D=days. Not using a unit will imply seconds. Examples: 10s, 50m, 1d, 50.
*
* @param string the Argument value.
* @return a Duration, or null if incorrectly formatted.
*/
@ObjectFetcher("d")
public static Duration valueOf(String string) {
if (string == null) return null;
// Pick a duration between a high and low number if there is a '-' present.
if (string.indexOf("-") > 0
&& Duration.matches(string.split("-", 2)[0])
&& Duration.matches(string.split("-", 2)[1])) {
String[] split = string.split("-", 2);
Duration low = Duration.valueOf(split[0]);
Duration high = Duration.valueOf(split[1]);
// Make sure 'low' and 'high' returned valid Durations,
// and that 'low' is less time than 'high'.
if (low != null && high != null
&& low.getSecondsAsInt() < high.getSecondsAsInt()) {
int seconds = Utilities.getRandom()
.nextInt((high.getSecondsAsInt() - low.getSecondsAsInt() + 1)
+ low.getSecondsAsInt());
// Send the result to the debugger since it's probably good to know what is being chosen.
dB.echoDebug("Getting random duration between " + low.identify()
+ " and " + high.identify() + "... " + seconds + "s");
return new Duration(seconds);
} else return null;
}
// Standard Duration. Check the type and create new Duration object accordingly.
Matcher m = match.matcher(string);
if (m.matches()) {
if (m.group().toUpperCase().endsWith("T"))
// Matches TICKS, so 1 tick = .05 seconds
return new Duration(Double.valueOf(m.group(1)) * 0.05);
else if (m.group().toUpperCase().endsWith("D"))
// Matches DAYS, so 1 day = 86400 seconds
return new Duration(Double.valueOf(m.group(1)) * 86400);
else if (m.group().toUpperCase().endsWith("M"))
// Matches MINUTES, so 1 minute = 60 seconds
return new Duration(Double.valueOf(m.group(1)) * 60);
else if (m.group().toUpperCase().endsWith("H"))
// Matches HOURS, so 1 hour = 3600 seconds
return new Duration(Double.valueOf(m.group(1)) * 3600);
else // seconds
return new Duration(Double.valueOf(m.group(1)));
}
return null;
}
/**
* Checks to see if the string is a valid Duration.
*
* @param string the String to match.
* @return true if valid.
*/
public static boolean matches(String string) {
Matcher m = match.matcher(string);
if (m.matches()) return true;
return false;
}
// The amount of seconds in the duration.
private double seconds;
// Duration's default dObject prefix.
private String prefix = "Duration";
/**
* Creates a duration object when given number of seconds.
*
* @param seconds the number of seconds.
*/
public Duration(double seconds) {
this.seconds = seconds;
if (this.seconds < 0) this.seconds = 0;
}
/**
* Creates a duration object when given number of seconds.
*
* @param seconds the number of seconds.
*/
public Duration(int seconds) {
this.seconds = seconds;
if (this.seconds < 0) this.seconds = 0;
}
/**
* Creates a duration object when given number of Bukkit ticks.
*
* @param ticks the number of ticks.
*/
public Duration (long ticks) {
this.seconds = ticks / 20;
if (this.seconds < 0) this.seconds = 0;
}
/**
* Gets the number of ticks of this duration. There are 20 ticks
* per second.
*
* @return the number of ticks.
*/
public long getTicks() {
return (long) (seconds * 20);
}
/**
* Gets the number of ticks of this duration as an integer. There are
* 20 per second.
*
* @return the number of ticks.
*/
public int getTicksAsInt() {
return Ints.checkedCast((long) (seconds * 20));
}
/**
* Gets the number of milliseconds in this duration.
*
* @return the number of milliseconds.
*/
public long getMillis() {
return (long) (seconds * 1000);
}
/**
* Gets the number of seconds of this duration.
*
* @return number of seconds
*/
public double getSeconds() {
return seconds;
}
/**
* Gets the number of seconds as an integer value of the duration.
*
* @return number of seconds rounded to the nearest second
*/
public int getSecondsAsInt() {
// Durations that are a fraction of a second
// will return as 1 when using this method.
if (seconds < 1 && seconds > 0) return 1;
return round(seconds);
}
private int round(double d){
double dAbs = Math.abs(d);
int i = (int) dAbs;
double result = dAbs - (double) i;
if(result<0.5){
return d<0 ? -i : i;
}else{
return d<0 ? -(i+1) : i+1;
}
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debug() {
return ChatColor.DARK_GRAY + prefix + "='"
+ ChatColor.YELLOW + identify()
+ ChatColor.DARK_GRAY + "' ";
}
// Durations are not unique, cannot be saved or persisted.
@Override
public boolean isUnique() {
return false;
}
@Override
public String getType() {
return "duration";
}
/**
* Return the value of this Duration. This will also return a
* valid String that can be re-interpreted with Duration.valueOf()
* thus acting as a form of 'serialization/deserialization'.
*
* @return a valid String-form Duration.
*/
@Override
public String identify() {
double seconds = getTicks() / 20;
double days = seconds / 86400;
double hours = seconds / 3600;
double minutes = seconds / 60;
if (days >= 1)
return days + "d";
if (hours >= 2)
return hours + "h";
if (minutes >= 2)
return minutes + "m";
else return seconds + "s";
}
/**
* Acts just like identify().
*
* @return a valid String-form Duration.
*/
@Override
public String toString() {
return identify();
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
// <--
// <d@duration.in_seconds> -> Element(number)
// returns the number of seconds in the Duration.
// -->
if (attribute.startsWith("in_seconds"))
return new Element(String.valueOf(seconds))
.getAttribute(attribute.fulfill(1));
// <--
// <d@duration.in_seconds> -> Element(number)
// returns the number of hours in the Duration.
// -->
if (attribute.startsWith("in_hours"))
return new Element(String.valueOf(seconds / 1800))
.getAttribute(attribute.fulfill(1));
// <--
// <d@duration.in_minutes> -> Element(number)
// returns the number of minutes in the Duration.
// -->
if (attribute.startsWith("in_minutes"))
return new Element(String.valueOf(seconds / 60))
.getAttribute(attribute.fulfill(1));
// <--
// <d@duration.in_ticks> -> Element(number)
// returns the number of ticks in the Duration. (20t/second)
// -->
if (attribute.startsWith("in_ticks"))
return new Element(String.valueOf(getTicksAsInt()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("debug.log")) {
dB.log(debug());
return new Element(Boolean.TRUE.toString())
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(debug()))
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug")) {
return new Element(debug())
.getAttribute(attribute.fulfill(1));
}
// <--
// <d@duration.value> -> Element
// returns the value of the duration, in the best format
// possible.
// -->
if (attribute.startsWith("value")) {
if (seconds % 43200 == 0)
return new Element(seconds / 86400 + "d")
.getAttribute(attribute.fulfill(1));
else if (seconds % 1800 == 0)
return new Element(seconds / 3600 + "h")
.getAttribute(attribute.fulfill(1));
else if (seconds % 30 == 0)
return new Element(seconds / 60 + "m")
.getAttribute(attribute.fulfill(1));
else return new Element(seconds + "s")
.getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute.fulfill(0));
}
}