-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DurationTag.java
673 lines (584 loc) · 23.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
package com.denizenscript.denizencore.objects.core;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.TagRunnable;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Durations are a convenient way to get a 'unit of time' within Denizen.
*/
public class DurationTag implements ObjectTag {
// <--[language]
// @name Duration
// @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 d@
// object fetcher notation can also be used, and is encouraged. 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: d@10s, d@50m, d@1d, d@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:
// d@10s-25s, d@1m-2m.
//
// For format info, see <@link language d@>
// -->
/////////////////////
// STATIC METHODS AND FIELDS
/////////////////
// Define a 'ZERO' Duration
final public static DurationTag ZERO = new DurationTag(0);
/////////////////////
// OBJECT FETCHER
/////////////////
// <--[language]
// @name d@
// @group Object Fetcher System
// @description
// d@ refers to the 'object identifier' of a 'Duration'. The 'd@' is notation for Denizen's Object
// Fetcher. Durations must be a positive number or range of numbers followed optionally by
// a unit of time, and prefixed by d@. Examples: d@3s, d@1d, d@10s-20s.
//
// For general info, see <@link language Duration>
// -->
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.replace("d@", ""));
if (string.equals("instant")) {
return new DurationTag(0);
}
// 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]);
DurationTag high = DurationTag.valueOf(split[1]);
// 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", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds / (86400 * 365)).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("years", tagProcessor.registeredObjectTags.get("in_years"));
// <--[tag]
// @attribute <DurationTag.in_weeks>
// @returns ElementTag(Decimal)
// @description
// returns the number of weeks in the Duration.
// -->
registerTag("in_weeks", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds / 604800).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("weeks", tagProcessor.registeredObjectTags.get("in_weeks"));
// <--[tag]
// @attribute <DurationTag.in_days>
// @returns ElementTag(Decimal)
// @description
// returns the number of days in the Duration.
// -->
registerTag("in_days", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds / 86400).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("days", tagProcessor.registeredObjectTags.get("in_days"));
// <--[tag]
// @attribute <DurationTag.in_hours>
// @returns ElementTag(Decimal)
// @description
// returns the number of hours in the Duration.
// -->
registerTag("in_hours", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds / 3600).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("hours", tagProcessor.registeredObjectTags.get("in_hours"));
// <--[tag]
// @attribute <DurationTag.in_minutes>
// @returns ElementTag(Decimal)
// @description
// returns the number of minutes in the Duration.
// -->
registerTag("in_minutes", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds / 60).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("minutes", tagProcessor.registeredObjectTags.get("in_minutes"));
// <--[tag]
// @attribute <DurationTag.in_seconds>
// @returns ElementTag(Decimal)
// @description
// returns the number of seconds in the Duration.
// -->
registerTag("in_seconds", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("seconds", tagProcessor.registeredObjectTags.get("in_seconds"));
// <--[tag]
// @attribute <DurationTag.in_milliseconds>
// @returns ElementTag(Decimal)
// @description
// returns the number of milliseconds in the Duration.
// -->
registerTag("in_milliseconds", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).seconds * 1000).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("milliseconds", tagProcessor.registeredObjectTags.get("in_milliseconds"));
// <--[tag]
// @attribute <DurationTag.in_ticks>
// @returns ElementTag(Number)
// @description
// returns the number of ticks in the Duration. (20t/second)
// -->
registerTag("in_ticks", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag((long) (((DurationTag) object).seconds * 20L)).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("ticks", tagProcessor.registeredObjectTags.get("in_ticks"));
// <--[tag]
// @attribute <DurationTag.sub[<duration>]>
// @returns ElementTag(Number)
// @description
// returns this duration minus another.
// -->
registerTag("sub", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag DurationTag.sub[...] must have a value.");
return null;
}
return new DurationTag(((DurationTag) object).getTicks() - DurationTag.valueOf(attribute.getContext(1)).getTicks())
.getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <DurationTag.add[<duration>]>
// @returns ElementTag(Number)
// @description
// returns this duration plus another.
// -->
registerTag("add", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag DurationTag.add[...] must have a value.");
return null;
}
return new DurationTag(((DurationTag) object).getTicks() + DurationTag.valueOf(attribute.getContext(1)).getTicks())
.getObjectAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <DurationTag.time>
// @returns ElementTag
// @description
// returns the date-time specified by the duration object.
// -->
registerTag("time", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
Date currentDate = new Date(((DurationTag) object).getTicks() * 50);
SimpleDateFormat format = new SimpleDateFormat();
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <DurationTag.time.year>
// @returns ElementTag(Number)
// @description
// Returns the current year of the time specified by the duration object.
// -->
if (attribute.startsWith("year")) {
return new ElementTag(currentDate.getYear() + 1900 /* ??? */).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <DurationTag.time.month>
// @returns ElementTag(Number)
// @description
// Returns the current month of the time specified by the duration object.
// -->
else if (attribute.startsWith("month")) {
return new ElementTag(currentDate.getMonth() + 1).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <DurationTag.time.day>
// @returns ElementTag(Number)
// @description
// Returns the current day of the time specified by the duration object.
// -->
else if (attribute.startsWith("day")) {
return new ElementTag(currentDate.getDate()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <DurationTag.time.day_of_week>
// @returns ElementTag(Number)
// @description
// Returns the current day-of-the-week of the time specified by the duration object.
// -->
else if (attribute.startsWith("day_of_week")) {
return new ElementTag(currentDate.getDay()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <DurationTag.time.hour>
// @returns ElementTag(Number)
// @description
// Returns the current hour of the time specified by the duration object.
// -->
else if (attribute.startsWith("hour")) {
return new ElementTag(currentDate.getHours()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <DurationTag.time.minute>
// @returns ElementTag(Number)
// @description
// Returns the current minute of the time specified by the duration object.
// -->
else if (attribute.startsWith("minute")) {
return new ElementTag(currentDate.getMinutes()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <DurationTag.time.second>
// @returns ElementTag(Number)
// @description
// Returns the current second of the time specified by the duration object.
// -->
else if (attribute.startsWith("second")) {
return new ElementTag(currentDate.getSeconds()).getObjectAttribute(attribute.fulfill(1));
}
// TODO: Custom format option
else {
format.applyPattern("EEE, d MMM yyyy HH:mm:ss");
return new ElementTag(format.format(currentDate))
.getObjectAttribute(attribute);
}
}
});
// <--[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", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag("Duration").getObjectAttribute(attribute.fulfill(1));
}
});
/////////////////////
// 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", new TagRunnable.ObjectForm() {
@Override
public ObjectTag run(Attribute attribute, ObjectTag object) {
return new ElementTag(((DurationTag) object).formatted()).getObjectAttribute(attribute.fulfill(1));
}
});
registerTag("value", tagProcessor.registeredObjectTags.get("formatted"));
}
public static ObjectTagProcessor tagProcessor = new ObjectTagProcessor();
public static void registerTag(String name, TagRunnable.ObjectForm runnable) {
tagProcessor.registerTag(name, runnable);
}
@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 = String.valueOf(days) + "d ";
}
if (hours > 0) {
timeString = timeString + String.valueOf(hours) + "h ";
}
if (minutes > 0 && days == 0) {
timeString = timeString + String.valueOf(minutes) + "m ";
}
if (seconds > 0 && minutes < 10 && hours == 0 && days == 0) {
timeString = timeString + String.valueOf(seconds) + "s";
}
if (timeString.isEmpty()) {
if (this.seconds <= 0) {
timeString = "forever";
}
else {
timeString = ((double) ((long) (this.seconds * 100)) / 100d) + "s";
}
}
return timeString.trim();
}
}