-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
FlagManager.java
1041 lines (911 loc) · 35.7 KB
/
FlagManager.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.aufdemrand.denizen.flags;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.events.core.FlagSmartEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.configuration.ConfigurationSection;
import java.util.*;
public class FlagManager {
// Valid flag actions
public static enum Action {
SET_VALUE, SET_BOOLEAN, INCREASE, DECREASE, MULTIPLY,
DIVIDE, INSERT, REMOVE, SPLIT, SPLIT_NEW, DELETE
}
// Constructor
private Denizen denizen;
public FlagManager(Denizen denizen) {
this.denizen = denizen;
}
// Static methods
public static boolean playerHasFlag(dPlayer player, String flagName) {
if (player == null || flagName == null) {
return false;
}
return DenizenAPI.getCurrentInstance().flagManager()
.getPlayerFlag(player, flagName).size() > 0;
}
public static boolean entityHasFlag(dEntity entity, String flagName) {
if (entity == null || flagName == null) {
return false;
}
return DenizenAPI.getCurrentInstance().flagManager()
.getEntityFlag(entity, flagName).size() > 0;
}
public static boolean npcHasFlag(dNPC npc, String flagName) {
if (npc == null || flagName == null) {
return false;
}
return DenizenAPI.getCurrentInstance().flagManager()
.getNPCFlag(npc.getId(), flagName).size() > 0;
}
public static boolean serverHasFlag(String flagName) {
if (flagName == null) {
return false;
}
return DenizenAPI.getCurrentInstance().flagManager()
.getGlobalFlag(flagName).size() > 0;
}
public static void clearNPCFlags(int npcid) {
DenizenAPI.getCurrentInstance().getSaves().set("NPCs." + npcid, null);
}
public static void clearEntityFlags(dEntity entity) {
DenizenAPI.getCurrentInstance().getSaves().set("Entities." + entity.getSaveName(), null);
}
/**
* Returns a NPC Flag object. If this flag currently exists
* it will be populated with the current values. If the flag does NOT exist,
* it will be created with blank values.
*/
public Flag getNPCFlag(int npcid, String flagName) {
return new Flag("NPCs." + npcid + ".Flags." + flagName.toUpperCase(), flagName, "n@" + npcid);
}
/**
* Returns a Global Flag object. If this flag currently exists
* it will be populated with the current values. If the flag does NOT exist,
* it will be created with blank values.
*/
public Flag getGlobalFlag(String flagName) {
return new Flag("Global.Flags." + flagName.toUpperCase(), flagName, "SERVER");
}
/**
* Returns a Flag Object tied to a Player. If this flag currently exists
* it will be populated with the current values. If the flag does NOT exist,
* it will be created with blank values.
*/
public Flag getPlayerFlag(dPlayer player, String flagName) {
if (player == null) {
return new Flag("players.00.UNKNOWN.Flags." + flagName.toUpperCase(), flagName, "p@null");
}
return new Flag("Players." + player.getSaveName() + ".Flags." + flagName.toUpperCase(), flagName, player.identify());
}
public Flag getEntityFlag(dEntity entity, String flagName) {
if (entity == null) {
return new Flag("Entities.00.UNKNOWN.Flags." + flagName.toUpperCase(), flagName, "e@null");
}
return new Flag("Entities." + entity.getSaveName() + ".Flags." + flagName.toUpperCase(), flagName, entity.identify());
}
/**
* Returns a list of flag names currently attached to an NPC.
*/
public Set<String> listNPCFlags(int npcid) {
ConfigurationSection section = denizen.getSaves().getConfigurationSection("NPCs." + npcid + ".Flags");
return section != null ? _filterExpirations(section.getValues(true).keySet()) : null;
}
public void shrinkGlobalFlags(Collection<String> set) {
for (String str : new HashSet<>(set)) {
if (!serverHasFlag(str)) {
set.remove(str);
}
}
}
public void shrinkPlayerFlags(dPlayer player, Collection<String> set) {
for (String str : new HashSet<>(set)) {
if (!playerHasFlag(player, str)) {
set.remove(str);
}
}
}
public void shrinkEntityFlags(dEntity entity, Collection<String> set) {
for (String str : new HashSet<>(set)) {
if (!entityHasFlag(entity, str)) {
set.remove(str);
}
}
}
/**
* Returns a list of flag names currently attached to the server.
*/
public Set<String> listGlobalFlags() {
ConfigurationSection section = denizen.getSaves().getConfigurationSection("Global.Flags");
return section != null ? _filterExpirations(section.getValues(true).keySet()) : null;
}
/**
* Returns a list of flag names currently attached to a player.
*/
public Set<String> listPlayerFlags(dPlayer player) {
ConfigurationSection section = denizen.getSaves().getConfigurationSection("Players." + player.getSaveName() + ".Flags");
return section != null ? _filterExpirations(section.getValues(true).keySet()) : null;
}
public Set<String> listEntityFlags(dEntity entity) {
ConfigurationSection section = denizen.getSaves().getConfigurationSection("Entities." + entity.getSaveName() + ".Flags");
return section != null ? _filterExpirations(section.getValues(true).keySet()) : null;
}
public Set<String> _filterExpirations(Set<String> flagKeys) {
for (Iterator<String> iter = flagKeys.iterator(); iter.hasNext(); ) {
if (iter.next().endsWith("-expiration")) {
iter.remove();
}
}
return flagKeys;
}
public class Flag {
private Value value;
private String flagPath;
private String flagName;
private String flagOwner;
private long expiration = -1L;
private boolean valid = true;
Flag(String flagPath, String flagName, String flagOwner) {
this.flagPath = flagPath;
this.flagName = flagName;
this.flagOwner = flagOwner;
rebuild();
}
/**
* Gets whether the flag is still valid.
*/
public boolean StillValid() {
return valid;
}
/**
* Gets all values currently stored in the flag.
*/
public List<String> values() {
checkExpired();
return value.asList();
}
/**
* Gets a specific value stored in a flag when given an index.
*/
public Value get(int index) {
checkExpired();
return value.get(index);
}
// <--[event]
// @Events
// flag cleared
// player flag cleared
// player flag <flagname> cleared
// npc flag cleared
// npc flag <flagname> cleared
// server flag cleared
// server flag <flagname> cleared
//
// @Regex ^on (player |entity |npc |server )flag( [^\s]+)? cleared$
//
// @Warning This event will fire rapidly and not exactly when you might expect it to fire. Generally, do not use this event unless you know what you're doing.
//
// @Triggers when a flag is cleared
// @Context
// <context.owner> returns an Element of the flag owner's object.
// <context.name> returns an Element of the flag name.
// <context.type> returns an Element of the flag type.
// <context.old_value> returns an Element of the flag's previous value.
//
// -->
/**
* Clears all values from a flag, essentially making it null.
*/
public void clear() {
String OldOwner = flagOwner;
String OldName = flagName;
dObject OldValue = FlagSmartEvent.isActive() ? (value.size() > 1
? value.asList()
: value.size() == 1 ? new Element(value.get(0).asString()) : new Element("null")) : null;
denizen.getSaves().set(flagPath, null);
denizen.getSaves().set(flagPath + "-expiration", null);
valid = false;
rebuild();
if (FlagSmartEvent.isActive()) {
List<String> world_script_events = new ArrayList<>();
Map<String, dObject> context = new HashMap<>();
dPlayer player = null;
if (dPlayer.matches(OldOwner)) {
player = dPlayer.valueOf(OldOwner);
}
dNPC npc = null;
if (Depends.citizens != null && dNPC.matches(OldOwner)) {
npc = dNPC.valueOf(OldOwner);
}
String type;
if (player != null) {
type = "player";
}
else if (npc != null) {
type = "npc";
}
else {
type = "server";
}
world_script_events.add(type + " flag cleared");
world_script_events.add(type + " flag " + OldName + " cleared");
context.put("owner", new Element(OldOwner));
context.put("name", new Element(OldName));
context.put("type", new Element(type));
context.put("old_value", OldValue);
world_script_events.add("flag cleared");
OldEventManager.doEvents(world_script_events,
new BukkitScriptEntryData(player, npc), context);
}
}
/**
* Gets the first value stored in the Flag.
*/
public Value getFirst() {
checkExpired();
return value.get(1);
}
/**
* Gets the last value stored in the Flag.
*/
public Value getLast() {
checkExpired();
return value.get(value.size());
}
/**
* Sets the value of the most recent value added to the flag. This does
* not create a new value unless the flag is currently empty of values.
*/
public void set(Object obj) {
set(obj, -1);
}
/**
* Sets a specific value in the flag. Adds the value to the flag if
* the index doesn't exist. If the index is less than 0, it instead
* clears the flag and works as if setting a blank flag. If the flag is
* currently empty, the value is added.
*/
public void set(Object obj, int index) {
checkExpired();
// No index? Clear the flag and set the whole thing.
if (index < 0) {
value.values = null;
value.size = 1;
value.firstValue = (String) obj;
}
else if (size() == 0) {
value.firstValue = (String) obj;
value.size = 1;
}
else if (index > 0) {
value.mustBeList();
if (value.values.size() > index - 1) {
value.values.set(index - 1, (String) obj);
// Index higher than currently exists? Add the item to the end of the list.
}
else {
value.values.add((String) obj);
value.size++;
}
}
valid = true;
save();
rebuild();
}
/**
* Adds a value to the end of the Flag's Values. This value will have an index
* of size() + 1. Returns the index of the value added. This could change if
* values are removed.
*/
public int add(Object obj) {
checkExpired();
value.mustBeList();
value.values.add((String) obj);
value.size++;
valid = true;
save();
rebuild();
return size();
}
/**
* Splits a dScript list into values that are then added to the flag.
* Returns the index of the last value added to the flag.
*/
public int split(Object obj) {
checkExpired();
dList split = dList.valueOf(obj.toString());
if (split.size() > 0) {
value.mustBeList();
for (String val : split) {
if (val.length() > 0) {
value.values.add(val);
value.size++;
}
}
save();
rebuild();
}
return size();
}
public int splitNew(Object obj) {
checkExpired();
dList split = dList.valueOf(obj.toString());
if (split.size() > 0) {
value.mustBeList();
value.values.clear();
value.size = 0;
for (String val : split) {
if (val.length() > 0) {
value.values.add(val);
value.size++;
}
}
save();
rebuild();
}
else {
clear();
}
return size();
}
/**
* Removes a value from the Flag's current values. The first value that matches
* (values are checked as Double and String.equalsIgnoreCase) is removed. If
* no match, no removal is done.
*/
public void remove(Object obj) {
remove(obj, -1);
}
/**
* Removes a value from the Flag's current values. If an index is specified,
* that specific value is removed. If no index is specified (or -1 is
* specified as the index), the first value that matches (values are
* checked as Double and String.equalsIgnoreCase) is removed. If a positive
* index is specified that does not exist, no removal is done.
*/
public void remove(Object obj, int index) {
checkExpired();
boolean isDouble = aH.matchesDouble((String) obj);
value.mustBeList();
// No index? Match object and remove it.
if (index <= 0 && obj != null) {
int x = 0;
for (String val : value.values) {
// Evaluate as String
if (val.equalsIgnoreCase(String.valueOf(obj))) {
value.values.remove(x);
value.size--;
break;
}
// Evaluate as number
try {
if (isDouble && aH.matchesDouble(val) && Double.valueOf(val).equals(Double.valueOf((String) obj))) {
value.values.remove(x);
value.size--;
break;
}
}
catch (NumberFormatException e) {
// Ignore
}
x++;
}
// Else, remove specified index
}
else if (index <= size()) {
value.values.remove(index - 1);
value.size--;
}
valid = true;
save();
rebuild();
}
/**
* Used to give an expiration time for a flag. This is the same format
* as System.getCurrentTimeMillis(), which is the number of milliseconds
* since Jan 1, 1960. As an example, to get a valid expiration for a
* specific amount of seconds from the current time, use the code snippet
* Flag.setExpiration(System.getCurrentTimeMillis() + (delay * 1000))
* where 'delay' is the amount of seconds.
*/
public void setExpiration(Long expiration) {
valid = true;
this.expiration = expiration;
save();
}
/**
* Returns the number of items in the Flag. This directly corresponds
* with the indexes, since Flag Indexes start with 1, unlike Java Lists
* which start at 0.
*/
public int size() {
checkExpired();
return value.size();
}
// <--[event]
// @Events
// flag changed
// player flag changed
// player flag <flagname> changed
// npc flag changed
// npc flag <flagname> changed
// server flag changed
// server flag <flagname> changed
// entity flag changed
// entity flag <flagname> changed
//
// @Regex ^on (player |entity |npc |server )flag( [^\s]+)? changed$
//
// @Warning This event will fire rapidly and not exactly when you might expect it to fire. Generally, do not use this event unless you know what you're doing.
//
// @Triggers when a flag is changed
// @Context
// <context.owner> returns an Element of the flag owner's object.
// <context.name> returns an Element of the flag name.
// <context.type> returns an Element of the flag type.
// <context.old_value> returns an Element of the flag's previous value.
//
// -->
/**
* Saves the current values in this object to the Denizen saves.yml.
* This is called internally when needed, but might be useful to call
* if you are extending the usage of Flags yourself.
*/
public void save() {
String oldOwner = flagOwner;
String oldName = flagName;
dObject oldValue = null;
if (FlagSmartEvent.isActive()) {
dList oldValueList = value.asList();
oldValue = oldValueList.size() > 1 ? oldValueList
: oldValueList.size() == 1 ? new Element(oldValueList.get(0)) : new Element("null");
}
if (value.values != null) {
denizen.getSaves().set(flagPath, value.values);
}
else {
denizen.getSaves().set(flagPath, value.size == 0 ? null : value.firstValue);
}
denizen.getSaves().set(flagPath + "-expiration", (expiration > 0 ? expiration : null));
if (FlagSmartEvent.isActive()) {
List<String> world_script_events = new ArrayList<>();
Map<String, dObject> context = new HashMap<>();
dPlayer player = null;
if (dPlayer.matches(oldOwner)) {
player = dPlayer.valueOf(oldOwner);
}
dNPC npc = null;
if (Depends.citizens != null && dNPC.matches(oldOwner)) {
npc = dNPC.valueOf(oldOwner);
}
dEntity entity = null;
if (dEntity.matches(oldOwner)) {
entity = dEntity.valueOf(oldOwner);
}
String type;
if (player != null) {
type = "player";
}
else if (npc != null) {
type = "npc";
}
else if (entity != null) {
type = "entity";
}
else {
type = "server";
}
world_script_events.add(type + " flag changed");
world_script_events.add(type + " flag " + oldName + " changed");
context.put("owner", new Element(oldOwner));
context.put("name", new Element(oldName));
context.put("type", new Element(type));
context.put("old_value", oldValue);
world_script_events.add("flag changed");
OldEventManager.doEvents(world_script_events,
new BukkitScriptEntryData(player, npc), context);
}
}
@Override
public String toString() {
checkExpired();
return (flagOwner.equalsIgnoreCase("SERVER") ? "fl@" + flagName : "fl[" + flagOwner + "]@" + flagName);
}
// <--[event]
// @Events
// flag expires
// player flag expires
// player flag <flagname> expires
// npc flag expires
// npc flag <flagname> expires
// server flag expires
// server flag <flagname> expires
// entity flag expires
// entity flag <flagname> expires
//
// @Regex ^on (player |entity |npc |server )flag( [^\s]+)? expires$
//
// @Warning This event will fire rapidly and not exactly when you might expect it to fire. Generally, do not use this event unless you know what you're doing.
//
// @Triggers when a flag expires
// @Context
// <context.owner> returns an Element of the flag owner's object.
// <context.name> returns an Element of the flag name.
// <context.type> returns an Element of the flag type.
// <context.old_value> returns an Element of the flag's previous value.
//
// -->
/**
* Removes flag if expiration is found to be up. This is called when an action
* is done on the flag, such as get() or put(). If expired, the flag will be
* erased before moving on.
*/
public boolean checkExpired() {
rebuild();
if (denizen.getSaves().contains(flagPath + "-expiration")) {
if (expiration > 1 && expiration < DenizenCore.currentTimeMillis) {
String oldOwner = flagOwner;
String oldName = flagName;
dObject oldValue = FlagSmartEvent.isActive() ? (value.size() > 1
? value.asList()
: value.size() == 1 ? new Element(value.get(0).asString()) : new Element("null")) : null;
denizen.getSaves().set(flagPath + "-expiration", null);
denizen.getSaves().set(flagPath, null);
valid = false;
rebuild();
//dB.log('\'' + flagName + "' has expired! " + flagPath);
if (FlagSmartEvent.isActive()) {
List<String> world_script_events = new ArrayList<>();
Map<String, dObject> context = new HashMap<>();
dPlayer player = null;
if (dPlayer.matches(oldOwner)) {
player = dPlayer.valueOf(oldOwner);
}
dNPC npc = null;
if (Depends.citizens != null && dNPC.matches(oldOwner)) {
npc = dNPC.valueOf(oldOwner);
}
dEntity entity = null;
if (dEntity.matches(oldOwner)) {
entity = dEntity.valueOf(oldOwner);
}
String type;
if (player != null) {
type = "player";
}
else if (npc != null) {
type = "npc";
}
else if (entity != null) {
type = "entity";
}
else {
type = "server";
}
world_script_events.add(type + " flag expires");
world_script_events.add(type + " flag " + oldName + " expires");
context.put("owner", new Element(oldOwner));
context.put("name", new Element(oldName));
context.put("type", new Element(type));
context.put("old_value", oldValue);
world_script_events.add("flag expires");
OldEventManager.doEvents(world_script_events,
new BukkitScriptEntryData(player, npc), context);
}
return true;
}
}
return false;
}
public Duration expiration() {
return new Duration((expiration - DenizenCore.currentTimeMillis) / 1000.0);
}
/**
* Returns the time left before the flag will expire. 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.
*/
@Deprecated
public String expirationTime() {
rebuild();
long seconds = (expiration - DenizenCore.currentTimeMillis) / 1000;
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";
}
return timeString.trim();
}
/**
* Rebuilds the flag object with data from the saves.yml (in Memory)
* to ensure that data is current if updated outside of the scope
* of the plugin.
*/
public Flag rebuild() {
if (denizen.getSaves().contains(flagPath + "-expiration")) {
this.expiration = (denizen.getSaves().getLong(flagPath + "-expiration"));
}
Object obj = denizen.getSaves().get(flagPath);
if (obj instanceof List) {
ArrayList<String> val = new ArrayList<>(((List) obj).size());
for (Object subObj : (List) obj) {
val.add(String.valueOf(subObj));
}
value = new Value(val);
}
else {
value = new Value(String.valueOf(obj));
}
return this;
}
/**
* Determines if the flag is empty.
*/
public boolean isEmpty() {
return value.isEmpty();
}
/**
* Performs an action on the flag.
*
* @param action a valid Action enum
* @param value the value specified for the action
* @param index the flag index, null if none
*/
public void doAction(Action action, Element value, Integer index) {
String val = (value != null ? value.asString() : null);
if (index == null) {
index = -1;
}
if (action == null) {
return;
}
// Do flagAction
switch (action) {
case INCREASE:
case DECREASE:
case MULTIPLY:
case DIVIDE:
double currentValue = get(index).asDouble();
set(CoreUtilities.doubleToString(math(currentValue, value.asDouble(), action)), index);
break;
case SET_BOOLEAN:
set("true", index);
break;
case SET_VALUE:
set(val, index);
break;
case INSERT:
add(val);
break;
case REMOVE:
remove(val, index);
break;
case SPLIT:
split(val);
break;
case SPLIT_NEW:
splitNew(val);
break;
case DELETE:
clear();
break;
}
}
private double math(double currentValue, double value, Action flagAction) {
switch (flagAction) {
case INCREASE:
return currentValue + value;
case DECREASE:
return currentValue - value;
case MULTIPLY:
return currentValue * value;
case DIVIDE:
return currentValue / value;
default:
break;
}
return 0;
}
}
/**
* Value object that is in charge of holding values that belong to a flag.
* Also contains some methods for retrieving stored values as specific
* data types. Otherwise, this object is used internally and created/destroyed
* automatically when working with Flag objects.
*/
public class Value {
private String firstValue;
private List<String> values;
private int index;
private int size;
public Value() {
size = 0;
index = 0;
}
public void mustBeList() {
if (values == null) {
values = new ArrayList<>();
if (size != 0) {
values.add(firstValue);
}
}
}
public void fixSize() {
if (values != null) {
size = values.size();
}
}
public Value(String oneValue) {
this.firstValue = oneValue;
size = 1;
index = 1;
}
public Value(List<String> values) {
this.values = values;
if (values == null) {
size = 0;
index = 0;
}
else {
size = values.size();
index = values.size() - 1;
}
}
private String getValue() {
if (values == null) {
if (size == 0) {
return "";
}
if (index == 0) {
return firstValue;
}
return "";
}
return values.get(index);
}
/**
* Used internally to specify which value to work with, if multiple values
* exist. If value is less than 0, value is set to the last value added.
*/
private void adjustIndex() {
// -1 = last object.
if (index < 0) {
index = size() - 1;
}
}
/**
* Retrieves a boolean of the value. If the value is set to ANYTHING except
* 'FALSE' (equalsIgnoreCase), it will return true. Useful for determining
* whether a value exists, as FALSE is also returned if the value is not set.
*/
public boolean asBoolean() {
return !getValue().equalsIgnoreCase("false");
}
/**
* Retrieves a double value of the specified index. If value is not set,
* or the value is not convertible to a Double, 0 is returned.
*/
public double asDouble() {
try {
return Double.valueOf(getValue());
}
catch (NumberFormatException e) {
return 0;
}
}
/**
* Returns an Integer value of the specified index. If the value has
* decimal point information, it is rounded. If value is not set,
* or the value is not convertible to a Double, 0 is returned.
*/
public int asInteger() {
try {
return Double.valueOf(getValue()).intValue();
}
catch (NumberFormatException e) {
return 0;
}
}
/**
* Returns a String value of the entirety of the values
* contained as a comma-separated list. If the value doesn't
* exist, "" is returned.
*/
public String asCommaSeparatedList() {
if (values == null) {
if (size == 0) {
return "";
}
return firstValue;
}
return String.join(", ", values);
}
/**
* Returns a String value of the entirety of the values
* contained as a dScript list. If the value doesn't
* exist, "" is returned.
*/
public dList asList() {
if (values == null) {
dList toReturn = new dList();
if (size != 0) {
toReturn.add(firstValue);
}
return toReturn;
}
return new dList(values);
}
public dList asList(String prefix) {
if (values == null) {
dList toReturn = new dList();
toReturn.setPrefix(prefix);
if (size != 0) {
toReturn.add(firstValue);
}
return toReturn;
}
return new dList(values, prefix);
}
/**
* Returns a String value of the value in the specified index. If
* the value doesn't exist, "" is returned.
*/
public String asString() {
return getValue();