-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DurationTag.java
515 lines (456 loc) · 16.7 KB
/
DurationTag.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package com.denizenscript.denizencore.objects.core;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
/**
* Durations are a convenient way to get a 'unit of time' within Denizen.
*/
public class DurationTag implements ObjectTag {
// <--[language]
// @name DurationTag Objects
// @group Object System
// @description
// Durations are a unified and convenient way to get a 'unit of time' throughout Denizen.
// Many commands and features that require a duration can be satisfied by specifying a number
// and unit of time, especially command arguments that are prefixed 'duration:', etc.
// The unit of time can be specified by using one of the following:
// T=ticks, M=minutes, S=seconds, H=hours, D=days, W=Weeks.
// Not using a unit will imply seconds. Examples: 10s, 50m, 1d, 20.
//
// Specifying a range of duration will result in a randomly selected duration that is
// in between the range specified. The smaller value should be first. Examples:
// '10s-25s', '1m-2m'.
//
// These use the object notation "d@".
// The identity format for DurationTags is the number of seconds, followed by an 's'.
//
// -->
/////////////////////
// STATIC METHODS AND FIELDS
/////////////////
// Define a 'ZERO' Duration
final public static DurationTag ZERO = new DurationTag(0);
/////////////////////
// OBJECT FETCHER
/////////////////
@Deprecated
public static DurationTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("d")
public static DurationTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
string = CoreUtilities.toLowerCase(string);
if (string.startsWith("d@")) {
string = string.substring("d@".length());
}
if (string.equals("instant")) {
return new DurationTag(0);
}
if (string.isEmpty()) {
return null;
}
// Pick a duration between a high and low number if there is a '-' present, but it's not "E-2" style scientific notation.
if (string.contains("-") && !string.contains("e-")) {
String[] split = string.split("-", 2);
if (split.length == 2) {
DurationTag low = DurationTag.valueOf(split[0], context);
DurationTag high = DurationTag.valueOf(split[1], context);
// Make sure 'low' and 'high' returned valid Durations,
// and that 'low' is less time than 'high'.
if (low != null && high != null) {
if (high.getSecondsAsInt() < low.getSecondsAsInt()) {
DurationTag temp = low;
low = high;
high = temp;
}
int seconds = CoreUtilities.getRandom()
.nextInt((high.getSecondsAsInt() - low.getSecondsAsInt() + 1))
+ low.getSecondsAsInt();
if (Debug.verbose) {
Debug.log("Getting random duration between " + low.identify()
+ " and " + high.identify() + "... " + seconds + "s");
}
return new DurationTag(seconds);
}
else {
return null;
}
}
}
String numericString = Character.isDigit(string.charAt(string.length() - 1)) ? string : string.substring(0, string.length() - 1);
// Standard Duration. Check the type and create new DurationTag object accordingly.
try {
if (string.endsWith("t")) {
// Matches TICKS, so 1 tick = .05 seconds
return new DurationTag(Double.valueOf(numericString) * 0.05);
}
else if (string.endsWith("d")) {
// Matches DAYS, so 1 day = 86400 seconds
return new DurationTag(Double.valueOf(numericString) * 86400);
}
else if (string.endsWith("w")) {
// Matches WEEKS, so 1 week = 604800 seconds
return new DurationTag(Double.valueOf(numericString) * 604800);
}
else if (string.endsWith("m")) {
// Matches MINUTES, so 1 minute = 60 seconds
return new DurationTag(Double.valueOf(numericString) * 60);
}
else if (string.endsWith("h")) {
// Matches HOURS, so 1 hour = 3600 seconds
return new DurationTag(Double.valueOf(numericString) * 3600);
}
else if (string.endsWith("s")) {
// seconds
return new DurationTag(Double.valueOf(numericString));
}
else if (numericString.equals(string)) {
// seconds
return new DurationTag(Double.valueOf(numericString));
}
else {
// Invalid.
if (context == null || context.debug) {
Debug.echoError("Duration type '" + string + "' is not valid.");
}
return null;
}
}
catch (Exception e) {
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) {
try {
return valueOf(string, CoreUtilities.noDebugContext) != null;
}
catch (Exception e) {
return false;
}
}
/////////////////////
// CONSTRUCTORS
/////////////////
/**
* Creates a duration object when given number of seconds.
*
* @param seconds the number of seconds.
*/
public DurationTag(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 DurationTag(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 DurationTag(long ticks) {
this.seconds = ticks / 20.0;
if (this.seconds < 0) {
this.seconds = 0;
}
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
// The amount of seconds in the duration.
private double seconds;
// Duration's default ObjectTag prefix.
private String prefix = "Duration";
/**
* 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 (int) (seconds * 20);
}
/**
* Gets the number of milliseconds in this duration.
*
* @return the number of milliseconds.
*/
public long getMillis() {
Double millis = seconds * 1000;
return millis.longValue();
}
/**
* 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 - i;
if (result < 0.5) {
return d < 0 ? -i : i;
}
else {
return d < 0 ? -(i + 1) : i + 1;
}
}
/////////////////////
// ObjectTag Methods
/////////////////
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
return "d@" + seconds + "s <GR>(" + formatted() + ")";
}
@Override
public boolean isUnique() {
// Durations are not unique, cannot be saved or persisted.
return false;
}
@Override
public String getObjectType() {
return "duration";
}
@Override
public String identify() {
return "d@" + seconds + "s";
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
public static void registerTags() {
/////////////////////
// CONVERSION ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <DurationTag.in_years>
// @returns ElementTag(Decimal)
// @description
// returns the number of years in the Duration.
// -->
registerTag("in_years", (attribute, object) -> {
return new ElementTag(object.seconds / (86400 * 365));
}, "years");
// <--[tag]
// @attribute <DurationTag.in_weeks>
// @returns ElementTag(Decimal)
// @description
// returns the number of weeks in the Duration.
// -->
registerTag("in_weeks", (attribute, object) -> {
return new ElementTag(object.seconds / 604800);
}, "weeks");
// <--[tag]
// @attribute <DurationTag.in_days>
// @returns ElementTag(Decimal)
// @description
// returns the number of days in the Duration.
// -->
registerTag("in_days", (attribute, object) -> {
return new ElementTag(object.seconds / 86400);
}, "days");
// <--[tag]
// @attribute <DurationTag.in_hours>
// @returns ElementTag(Decimal)
// @description
// returns the number of hours in the Duration.
// -->
registerTag("in_hours", (attribute, object) -> {
return new ElementTag(object.seconds / 3600);
}, "hours");
// <--[tag]
// @attribute <DurationTag.in_minutes>
// @returns ElementTag(Decimal)
// @description
// returns the number of minutes in the Duration.
// -->
registerTag("in_minutes", (attribute, object) -> {
return new ElementTag(object.seconds / 60);
}, "minutes");
// <--[tag]
// @attribute <DurationTag.in_seconds>
// @returns ElementTag(Decimal)
// @description
// returns the number of seconds in the Duration.
// -->
registerTag("in_seconds", (attribute, object) -> {
return new ElementTag(object.seconds);
}, "seconds");
// <--[tag]
// @attribute <DurationTag.in_milliseconds>
// @returns ElementTag(Decimal)
// @description
// returns the number of milliseconds in the Duration.
// -->
registerTag("in_milliseconds", (attribute, object) -> {
return new ElementTag(object.seconds * 1000);
}, "milliseconds");
// <--[tag]
// @attribute <DurationTag.in_ticks>
// @returns ElementTag(Number)
// @description
// returns the number of ticks in the Duration. (20t/second)
// -->
registerTag("in_ticks", (attribute, object) -> {
return new ElementTag((long) (object.seconds * 20L));
}, "ticks");
// <--[tag]
// @attribute <DurationTag.sub[<duration>]>
// @returns ElementTag(Number)
// @description
// returns this duration minus another.
// -->
registerTag("sub", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag DurationTag.sub[...] must have a value.");
return null;
}
return new DurationTag(object.getTicks() - DurationTag.valueOf(attribute.getContext(1), attribute.context).getTicks());
});
// <--[tag]
// @attribute <DurationTag.add[<duration>]>
// @returns ElementTag(Number)
// @description
// returns this duration plus another.
// -->
registerTag("add", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag DurationTag.add[...] must have a value.");
return null;
}
return new DurationTag(object.getTicks() + DurationTag.valueOf(attribute.getContext(1), attribute.context).getTicks());
});
registerTag("time", (attribute, object) -> {
Deprecations.timeTagRewrite.warn(attribute.context);
return new TimeTag(object.getMillis());
});
// <--[tag]
// @attribute <DurationTag.type>
// @returns ElementTag
// @description
// Always returns 'Duration' for DurationTag objects. All objects fetchable by the Object Fetcher will return the
// type of object that is fulfilling this attribute.
// -->
registerTag("type", (attribute, object) -> {
return new ElementTag("Duration");
});
/////////////////////
// FORMAT ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <DurationTag.formatted>
// @returns ElementTag
// @description
// returns the value of the duration in an easily readable
// format like 2h 30m, where minutes are only shown if there
// is less than a day left and seconds are only shown if
// there are less than 10 minutes left.
// -->
registerTag("formatted", (attribute, object) -> {
return new ElementTag(object.formatted());
}, "value");
}
public static ObjectTagProcessor<DurationTag> tagProcessor = new ObjectTagProcessor<>();
public static void registerTag(String name, TagRunnable.ObjectInterface<DurationTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
public String formatted() {
// Make sure you don't change these longs into doubles
// and break the code
long seconds = (long) this.seconds;
long days = seconds / 86400;
long hours = (seconds - days * 86400) / 3600;
long minutes = (seconds - days * 86400 - hours * 3600) / 60;
seconds = seconds - days * 86400 - hours * 3600 - minutes * 60;
String timeString = "";
if (days > 0) {
timeString = days + "d ";
}
if (hours > 0) {
timeString = timeString + hours + "h ";
}
if (minutes > 0 && days == 0) {
timeString = timeString + minutes + "m ";
}
if (seconds > 0 && minutes < 10 && hours == 0 && days == 0) {
timeString = timeString + seconds + "s";
}
if (timeString.isEmpty()) {
if (this.seconds <= 0) {
timeString = "forever";
}
else {
timeString = ((double) ((long) (this.seconds * 100)) / 100d) + "s";
}
}
return timeString.trim();
}
}