/
WorldTag.java
1434 lines (1297 loc) · 52.4 KB
/
WorldTag.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 com.denizenscript.denizen.objects;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.flags.WorldFlagHandler;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.utilities.Settings;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.*;
import org.bukkit.boss.DragonBattle;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.world.TimeSkipEvent;
import org.bukkit.util.BoundingBox;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class WorldTag implements ObjectTag, Adjustable, FlaggableObject {
/////////////////////
// STATIC METHODS
/////////////////
public static WorldTag mirrorBukkitWorld(World world) {
if (world == null) {
return null;
}
return new WorldTag(world);
}
/////////////////////
// OBJECT FETCHER
/////////////////
// <--[ObjectType]
// @name WorldTag
// @prefix w
// @base ElementTag
// @implements FlaggableObject
// @ExampleTagBase player.location.world
// @ExampleValues <player.location.world>,space
// @ExampleForReturns
// - adjust %VALUE% destroy
// @ExampleForReturns
// - adjust %VALUE% full_time:0
// @format
// The identity format for worlds is the name of the world it should be associated with.
// For example, to reference the world named 'world1', use simply 'world1'.
// World names are case insensitive.
//
// @description
// A WorldTag represents a world on the server.
//
// This object type is flaggable.
// Flags on this object type will be stored in the world folder in a file named 'denizen_flags.dat', like "server/world/denizen_flags.dat".
//
// @Matchable
// WorldTag matchers, sometimes identified as "<world>":
// "world" plaintext: always matches.
// World name: matches if the world has the given world name, using advanced matchers.
// "world_flagged:<flag>": a Flag Matchable for WorldTag flags.
//
// -->
@Deprecated
public static WorldTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("w")
public static WorldTag valueOf(String string, TagContext context) {
return valueOf(string, context == null || context.showErrors());
}
public static WorldTag valueOf(String string, boolean announce) {
if (string == null) {
return null;
}
string = string.replace("w@", "");
World returnable = null;
for (World world : Bukkit.getWorlds()) {
if (world.getName().equalsIgnoreCase(string)) {
returnable = world;
}
}
if (returnable != null) {
return new WorldTag(returnable);
}
else if (announce) {
Debug.echoError("Invalid World! '" + string + "' could not be found.");
}
return null;
}
public static boolean matches(String arg) {
arg = arg.replace("w@", "");
World returnable = null;
for (World world : Bukkit.getWorlds()) {
if (world.getName().equalsIgnoreCase(arg)) {
returnable = world;
}
}
return returnable != null;
}
@Override
public AbstractFlagTracker getFlagTracker() {
return WorldFlagHandler.worldFlagTrackers.get(getName());
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
@Override
public String getReasonNotFlaggable() {
return "is the world loaded?";
}
public World getWorld() {
return Bukkit.getWorld(world_name);
}
public String getName() {
return world_name;
}
public List<Entity> getEntities() {
return getWorld().getEntities();
}
public List<Entity> getEntitiesForTag() {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
return getWorld().getEntities();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public final Collection<Entity> getPossibleEntitiesForBoundary(BoundingBox box) {
// Bork-prevention: getNearbyEntities loops over chunks, so for large boxes just get the direct entity list, as that's probably better than a loop over unloaded chunks
if (box.getWidthX() > 512 || box.getWidthZ() > 512) {
return getWorld().getEntities();
}
return getWorld().getNearbyEntities(box);
}
public Collection<Entity> getPossibleEntitiesForBoundaryForTag(BoundingBox box) {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
return getPossibleEntitiesForBoundary(box);
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public List<LivingEntity> getLivingEntitiesForTag() {
NMSHandler.chunkHelper.changeChunkServerThread(getWorld());
try {
return getWorld().getLivingEntities();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getWorld());
}
}
public <T> T getGameRuleOrDefault(GameRule<T> gameRule) {
World world = getWorld();
T value = world.getGameRuleValue(gameRule);
if (value == null) {
value = world.getGameRuleDefault(gameRule);
if (value == null) {
throw new IllegalStateException("World " + world_name + " contains no GameRule " + gameRule.getName());
}
}
return value;
}
private String prefix;
String world_name;
public WorldTag(World world) {
this(null, world);
}
public WorldTag(String worldName) {
prefix = "World";
this.world_name = worldName;
}
public WorldTag(String prefix, World world) {
if (prefix == null) {
this.prefix = "World";
}
else {
this.prefix = prefix;
}
this.world_name = world.getName();
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String identify() {
return "w@" + world_name;
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public Object getJavaObject() {
return getWorld();
}
@Override
public ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
/////////////////////
// ENTITY LIST ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.entities[(<matcher>)]>
// @returns ListTag(EntityTag)
// @description
// Returns a list of entities in this world.
// Optionally specify an entity type matcher to filter down to.
// -->
registerTag(ListTag.class, "entities", (attribute, object) -> {
ListTag entities = new ListTag();
String matcher = attribute.hasParam() ? attribute.getParam() : null;
for (Entity entity : object.getEntitiesForTag()) {
EntityTag current = new EntityTag(entity);
if (matcher == null || current.tryAdvancedMatcher(matcher)) {
entities.addObject(current.getDenizenObject());
}
}
return entities;
});
// <--[tag]
// @attribute <WorldTag.living_entities>
// @returns ListTag(EntityTag)
// @description
// Returns a list of living entities in this world.
// This includes Players, mobs, NPCs, etc., but excludes dropped items, experience orbs, etc.
// -->
registerTag(ListTag.class, "living_entities", (attribute, object) -> {
ListTag entities = new ListTag();
for (Entity entity : object.getLivingEntitiesForTag()) {
entities.addObject(new EntityTag(entity).getDenizenObject());
}
return entities;
});
// <--[tag]
// @attribute <WorldTag.players>
// @returns ListTag(PlayerTag)
// @description
// Returns a list of online players in this world.
// -->
registerTag(ListTag.class, "players", (attribute, object) -> {
ListTag players = new ListTag();
for (Player player : object.getWorld().getPlayers()) {
if (!EntityTag.isNPC(player)) {
players.addObject(new PlayerTag(player));
}
}
return players;
});
// <--[tag]
// @attribute <WorldTag.spawned_npcs>
// @returns ListTag(NPCTag)
// @description
// Returns a list of spawned NPCs in this world.
// -->
registerTag(ListTag.class, "spawned_npcs", (attribute, object) -> {
ListTag npcs = new ListTag();
World thisWorld = object.getWorld();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.isSpawned() && npc.getStoredLocation().getWorld().equals(thisWorld)) {
npcs.addObject(new NPCTag(npc));
}
}
return npcs;
});
// <--[tag]
// @attribute <WorldTag.npcs>
// @returns ListTag(NPCTag)
// @description
// Returns a list of all NPCs in this world.
// -->
registerTag(ListTag.class, "npcs", (attribute, object) -> {
ListTag npcs = new ListTag();
World thisWorld = object.getWorld();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
Location location = npc.getStoredLocation();
if (location != null) {
World world = location.getWorld();
if (world != null && world.equals(thisWorld)) {
npcs.addObject(new NPCTag(npc));
}
}
}
return npcs;
});
/////////////////////
// GEOGRAPHY ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.can_generate_structures>
// @returns ElementTag(Boolean)
// @description
// Returns whether the world will generate structures.
// -->
registerTag(ElementTag.class, "can_generate_structures", (attribute, object) -> {
return new ElementTag(object.getWorld().canGenerateStructures());
});
// <--[tag]
// @attribute <WorldTag.loaded_chunks>
// @returns ListTag(ChunkTag)
// @description
// Returns a list of all the currently loaded chunks.
// -->
registerTag(ListTag.class, "loaded_chunks", (attribute, object) -> {
ListTag chunks = new ListTag();
for (Chunk ent : object.getWorld().getLoadedChunks()) {
chunks.addObject(new ChunkTag(ent));
}
return chunks;
});
registerTag(ChunkTag.class, "random_loaded_chunk", (attribute, object) -> {
BukkitImplDeprecations.worldRandomLoadedChunkTag.warn(attribute.context);
int random = CoreUtilities.getRandom().nextInt(object.getWorld().getLoadedChunks().length);
return new ChunkTag(object.getWorld().getLoadedChunks()[random]);
});
// <--[tag]
// @attribute <WorldTag.sea_level>
// @returns ElementTag(Number)
// @description
// Returns the level of the sea.
// -->
registerTag(ElementTag.class, "sea_level", (attribute, object) -> {
return new ElementTag(object.getWorld().getSeaLevel());
});
// <--[tag]
// @attribute <WorldTag.max_height>
// @returns ElementTag(Number)
// @description
// Returns the maximum block height of the world.
// -->
registerTag(ElementTag.class, "max_height", (attribute, object) -> {
return new ElementTag(object.getWorld().getMaxHeight());
});
// <--[tag]
// @attribute <WorldTag.min_height>
// @returns ElementTag(Number)
// @description
// Returns the minimum block height of the world.
// -->
registerTag(ElementTag.class, "min_height", (attribute, object) -> {
return new ElementTag(object.getWorld().getMinHeight());
});
// <--[tag]
// @attribute <WorldTag.spawn_location>
// @returns LocationTag
// @mechanism WorldTag.spawn_location
// @description
// Returns the spawn location of the world.
// -->
registerTag(LocationTag.class, "spawn_location", (attribute, object) -> {
return new LocationTag(object.getWorld().getSpawnLocation());
});
// <--[tag]
// @attribute <WorldTag.world_type>
// @returns ElementTag
// @description
// Returns the world type of the world.
// Can return any enum from: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/WorldType.html>
// -->
registerTag(ElementTag.class, "world_type", (attribute, object) -> {
return new ElementTag(object.getWorld().getWorldType().getName());
});
/////////////////////
// IDENTIFICATION ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.name>
// @returns ElementTag
// @description
// Returns the name of the world.
// -->
tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
return new ElementTag(object.world_name);
});
// <--[tag]
// @attribute <WorldTag.seed>
// @returns ElementTag
// @description
// Returns the world seed.
// -->
registerTag(ElementTag.class, "seed", (attribute, object) -> {
return new ElementTag(object.getWorld().getSeed());
});
/////////////////////
// SETTINGS ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.allows_animals>
// @returns ElementTag(Boolean)
// @description
// Returns whether animals can spawn in this world.
// -->
registerTag(ElementTag.class, "allows_animals", (attribute, object) -> {
return new ElementTag(object.getWorld().getAllowAnimals());
});
// <--[tag]
// @attribute <WorldTag.allows_monsters>
// @returns ElementTag(Boolean)
// @description
// Returns whether monsters can spawn in this world.
// -->
registerTag(ElementTag.class, "allows_monsters", (attribute, object) -> {
return new ElementTag(object.getWorld().getAllowMonsters());
});
// <--[tag]
// @attribute <WorldTag.allows_pvp>
// @returns ElementTag(Boolean)
// @description
// Returns whether player versus player combat is allowed in this world.
// -->
registerTag(ElementTag.class, "allows_pvp", (attribute, object) -> {
return new ElementTag(object.getWorld().getPVP());
});
// <--[tag]
// @attribute <WorldTag.auto_save>
// @returns ElementTag(Boolean)
// @mechanism WorldTag.auto_save
// @description
// Returns whether the world automatically saves.
// -->
registerTag(ElementTag.class, "auto_save", (attribute, object) -> {
return new ElementTag(object.getWorld().isAutoSave());
});
// <--[tag]
// @attribute <WorldTag.ambient_spawn_limit>
// @returns ElementTag(Number)
// @mechanism WorldTag.ambient_spawn_limit
// @description
// Returns the number of ambient mobs that can spawn in a chunk in this world.
// -->
registerTag(ElementTag.class, "ambient_spawn_limit", (attribute, object) -> {
return new ElementTag(object.getWorld().getAmbientSpawnLimit());
});
// <--[tag]
// @attribute <WorldTag.animal_spawn_limit>
// @returns ElementTag(Number)
// @mechanism WorldTag.animal_spawn_limit
// @description
// Returns the number of animals that can spawn in a chunk in this world.
// -->
registerTag(ElementTag.class, "animal_spawn_limit", (attribute, object) -> {
return new ElementTag(object.getWorld().getAnimalSpawnLimit());
});
// <--[tag]
// @attribute <WorldTag.monster_spawn_limit>
// @returns ElementTag(Number)
// @mechanism WorldTag.monster_spawn_limit
// @description
// Returns the number of monsters that can spawn in a chunk in this world.
// -->
registerTag(ElementTag.class, "monster_spawn_limit", (attribute, object) -> {
return new ElementTag(object.getWorld().getMonsterSpawnLimit());
});
// <--[tag]
// @attribute <WorldTag.water_animal_spawn_limit>
// @returns ElementTag(Number)
// @mechanism WorldTag.water_animal_spawn_limit
// @description
// Returns the number of water animals that can spawn in a chunk in this world.
// -->
registerTag(ElementTag.class, "water_animal_spawn_limit", (attribute, object) -> {
return new ElementTag(object.getWorld().getWaterAnimalSpawnLimit());
});
// <--[tag]
// @attribute <WorldTag.difficulty>
// @returns ElementTag
// @mechanism WorldTag.difficulty
// @description
// Returns the name of the difficulty level.
// -->
registerTag(ElementTag.class, "difficulty", (attribute, object) -> {
return new ElementTag(object.getWorld().getDifficulty().name());
});
// <--[tag]
// @attribute <WorldTag.hardcore>
// @returns ElementTag(Boolean)
// @mechanism WorldTag.hardcore
// @description
// Returns whether the world is in hardcore mode.
// -->
registerTag(ElementTag.class, "hardcore", (attribute, object) -> {
return new ElementTag(object.getWorld().isHardcore());
});
// <--[tag]
// @attribute <WorldTag.keep_spawn>
// @returns ElementTag(Boolean)
// @mechanism WorldTag.keep_spawn
// @description
// Returns whether the world's spawn area should be kept loaded into memory.
// -->
registerTag(ElementTag.class, "keep_spawn", (attribute, object) -> {
return new ElementTag(object.getWorld().getKeepSpawnInMemory());
});
// <--[tag]
// @attribute <WorldTag.ticks_per_animal_spawn>
// @returns DurationTag
// @mechanism WorldTag.ticks_per_animal_spawns
// @description
// Returns the world's ticks per animal spawn value.
// -->
registerTag(DurationTag.class, "ticks_per_animal_spawn", (attribute, object) -> {
return new DurationTag(object.getWorld().getTicksPerAnimalSpawns());
});
// <--[tag]
// @attribute <WorldTag.ticks_per_monster_spawn>
// @returns DurationTag
// @mechanism WorldTag.ticks_per_monster_spawns
// @description
// Returns the world's ticks per monster spawn value.
// -->
registerTag(DurationTag.class, "ticks_per_monster_spawn", (attribute, object) -> {
return new DurationTag(object.getWorld().getTicksPerMonsterSpawns());
});
// <--[tag]
// @attribute <WorldTag.duration_since_created>
// @returns DurationTag
// @description
// Returns the total duration of time since this world was first created.
// -->
registerTag(DurationTag.class, "duration_since_created", (attribute, object) -> {
return new DurationTag(object.getWorld().getGameTime());
});
/////////////////////
// TIME ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.time>
// @returns ElementTag(Number)
// @mechanism WorldTag.time
// @description
// Returns the relative in-game time of this world.
// -->
registerTag(ObjectTag.class, "time", (attribute, object) -> {
// <--[tag]
// @attribute <WorldTag.time.duration>
// @returns DurationTag
// @description
// Returns the relative in-game time of this world as a duration.
// -->
if (attribute.startsWith("duration", 2)) {
attribute.fulfill(1);
return new DurationTag(object.getWorld().getTime());
}
// <--[tag]
// @attribute <WorldTag.time.full>
// @returns DurationTag
// @description
// Returns the in-game time of this world.
// -->
else if (attribute.startsWith("full", 2)) {
attribute.fulfill(1);
return new DurationTag(object.getWorld().getFullTime());
}
// <--[tag]
// @attribute <WorldTag.time.period>
// @returns ElementTag
// @description
// Returns the time as 'day', 'night', 'dawn', or 'dusk'.
// -->
else if (attribute.startsWith("period", 2)) {
attribute.fulfill(1);
long time = object.getWorld().getTime();
String period;
if (time >= 23000) {
period = "dawn";
}
else if (time >= 13500) {
period = "night";
}
else if (time >= 12500) {
period = "dusk";
}
else {
period = "day";
}
return new ElementTag(period);
}
else {
return new ElementTag(object.getWorld().getTime());
}
});
// <--[tag]
// @attribute <WorldTag.moon_phase>
// @returns ElementTag(Number)
// @description
// Returns the current phase of the moon, as a number from 1 to 8.
// -->
registerTag(ElementTag.class, "moon_phase", (attribute, object) -> {
return new ElementTag((int) ((object.getWorld().getFullTime() / 24000) % 8) + 1);
}, "moonphase");
/////////////////////
// WEATHER ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.has_storm>
// @returns ElementTag(Boolean)
// @description
// Returns whether there is currently a storm in this world.
// ie, whether it is raining. To check for thunder, use <@link tag WorldTag.thundering>.
// -->
registerTag(ElementTag.class, "has_storm", (attribute, object) -> {
return new ElementTag(object.getWorld().hasStorm());
});
// <--[tag]
// @attribute <WorldTag.thunder_duration>
// @returns DurationTag
// @mechanism WorldTag.thunder_duration
// @description
// Returns the duration of thunder.
// -->
registerTag(DurationTag.class, "thunder_duration", (attribute, object) -> {
return new DurationTag((long) object.getWorld().getThunderDuration());
});
// <--[tag]
// @attribute <WorldTag.thundering>
// @returns ElementTag(Boolean)
// @mechanism WorldTag.thundering
// @description
// Returns whether it is currently thundering in this world.
// -->
registerTag(ElementTag.class, "thundering", (attribute, object) -> {
return new ElementTag(object.getWorld().isThundering());
});
// <--[tag]
// @attribute <WorldTag.weather_duration>
// @returns DurationTag
// @mechanism WorldTag.weather_duration
// @description
// Returns the duration of storms.
// -->
registerTag(DurationTag.class, "weather_duration", (attribute, object) -> {
return new DurationTag((long) object.getWorld().getWeatherDuration());
});
// <--[tag]
// @attribute <WorldTag.environment>
// @returns ElementTag
// @description
// Returns the environment of the world: NORMAL, NETHER, or THE_END.
// -->
registerTag(ElementTag.class, "environment", (attribute, object) -> {
return new ElementTag(object.getWorld().getEnvironment().name());
});
/////////////////////
// WORLD BORDER ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <WorldTag.border_size>
// @returns ElementTag(Decimal)
// @description
// Returns the size of the world border in this world.
// -->
registerTag(ElementTag.class, "border_size", (attribute, object) -> {
return new ElementTag(object.getWorld().getWorldBorder().getSize());
});
// <--[tag]
// @attribute <WorldTag.border_center>
// @returns LocationTag
// @description
// Returns the center of the world border in this world.
// -->
registerTag(LocationTag.class, "border_center", (attribute, object) -> {
return new LocationTag(object.getWorld().getWorldBorder().getCenter());
});
// <--[tag]
// @attribute <WorldTag.border_damage>
// @returns ElementTag(Decimal)
// @description
// Returns the amount of damage caused by crossing the world border in this world.
// -->
registerTag(ElementTag.class, "border_damage", (attribute, object) -> {
return new ElementTag(object.getWorld().getWorldBorder().getDamageAmount());
});
// <--[tag]
// @attribute <WorldTag.border_damage_buffer>
// @returns ElementTag(Decimal)
// @description
// Returns the damage buffer of the world border in this world.
// -->
registerTag(ElementTag.class, "border_damage_buffer", (attribute, object) -> {
return new ElementTag(object.getWorld().getWorldBorder().getDamageBuffer());
});
// <--[tag]
// @attribute <WorldTag.border_warning_distance>
// @returns ElementTag(Number)
// @description
// Returns the warning distance of the world border in this world.
// -->
registerTag(ElementTag.class, "border_warning_distance", (attribute, object) -> {
return new ElementTag(object.getWorld().getWorldBorder().getWarningDistance());
});
// <--[tag]
// @attribute <WorldTag.border_warning_time>
// @returns DurationTag
// @description
// Returns warning time of the world border in this world as a duration.
// -->
registerTag(DurationTag.class, "border_warning_time", (attribute, object) -> {
return new DurationTag(object.getWorld().getWorldBorder().getWarningTime());
});
// <--[tag]
// @attribute <WorldTag.gamerule[<gamerule>]>
// @returns ElementTag
// @description
// Returns the current value of the specified gamerule in the world.
// Note that the name is case-sensitive... so "doFireTick" is correct, but "dofiretick" is not.
// -->
registerTag(ElementTag.class, "gamerule", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("The tag 'worldtag.gamerule[...]' must have an input value.");
return null;
}
GameRule rule = GameRule.getByName(attribute.getParam());
Object result = object.getWorld().getGameRuleValue(rule);
return new ElementTag(result == null ? "null" : result.toString());
});
// <--[tag]
// @attribute <WorldTag.gamerule_map>
// @returns MapTag
// @description
// Returns a map of all the current values of all gamerules in the world.
// -->
registerTag(MapTag.class, "gamerule_map", (attribute, object) -> {
MapTag map = new MapTag();
for (GameRule rule : GameRule.values()) {
Object result = object.getWorld().getGameRuleValue(rule);
if (result != null) {
map.putObject(rule.getName(), new ElementTag(result.toString()));
}
}
return map;
});
// <--[tag]
// @attribute <WorldTag.dragon_portal_location>
// @returns LocationTag
// @description
// Returns the location of the ender dragon exit portal, if any (only for end worlds).
// -->
registerTag(LocationTag.class, "dragon_portal_location", (attribute, object) -> {
DragonBattle battle = object.getWorld().getEnderDragonBattle();
if (battle == null) {
return null;
}
if (battle.getEndPortalLocation() == null) {
return null;
}
return new LocationTag(battle.getEndPortalLocation());
});
// <--[tag]
// @attribute <WorldTag.ender_dragon>
// @returns EntityTag
// @description
// Returns the ender dragon entity currently fighting in this world, if any (only for end worlds).
// -->
registerTag(EntityTag.class, "ender_dragon", (attribute, object) -> {
DragonBattle battle = object.getWorld().getEnderDragonBattle();
if (battle == null) {
return null;
}
if (battle.getEnderDragon() == null) {
return null;
}
return new EntityTag(battle.getEnderDragon());
});
// <--[tag]
// @attribute <WorldTag.gateway_locations>
// @returns ListTag(LocationTag)
// @description
// Returns a list of possible gateway portal locations, if any (only for end worlds).
// Not all of these will necessarily generate.
// In current implementation, this is a list of exactly 20 locations in a circle around the world origin (with radius of 96 blocks).
// -->
registerTag(ListTag.class, "gateway_locations", (attribute, object) -> {
DragonBattle battle = object.getWorld().getEnderDragonBattle();
if (battle == null) {
return null;
}
ListTag list = new ListTag();
for (int i = 0; i < 20; i++) {
// This math based on EndDragonFight#spawnNewGateway
int x = (int) Math.floor(96.0D * Math.cos(2.0D * (-Math.PI + (Math.PI / 20.0) * i)));
int z = (int) Math.floor(96.0D * Math.sin(2.0D * (-Math.PI + (Math.PI / 20.0) * i)));
list.addObject(new LocationTag(object.getWorld(), x, 75, z));
}
return list;
});
// <--[tag]
// @attribute <WorldTag.biomes>
// @returns ListTag(BiomeTag)
// @description
// Returns a list of all biomes in this world (including custom biomes).
// -->
registerTag(ListTag.class, "biomes", (attribute, object) -> {
ListTag output = new ListTag();
for (BiomeNMS biome : NMSHandler.instance.getBiomes(object.getWorld())) {
output.addObject(new BiomeTag(biome));
}
return output;
});
// <--[tag]
// @attribute <WorldTag.view_distance>
// @returns ElementTag(Number)
// @mechanism WorldTag.view_distance
// @group properties
// @description
// Returns the view distance of this world. Chunks are visible to players inside this radius.
// See also <@link tag WorldTag.simulation_distance>
// -->
registerTag(ElementTag.class, "view_distance", (attribute, world) -> {
// Note: mechanism is paper-only, in PaperWorldProperties
return new ElementTag(world.getWorld().getViewDistance());
});
// <--[tag]
// @attribute <WorldTag.simulation_distance>
// @returns ElementTag(Number)
// @mechanism WorldTag.simulation_distance
// @group properties
// @description
// Returns the simulation distance of this world. Chunks inside of this radius to players are ticked and processed.
// See also <@link tag WorldTag.view_distance>
// -->
registerTag(ElementTag.class, "simulation_distance", (attribute, world) -> {
// Note: mechanism is paper-only, in PaperWorldProperties
return new ElementTag(world.getWorld().getSimulationDistance());
});
// <--[tag]
// @attribute <WorldTag.enough_sleeping[(<#>)]>
// @returns ElementTag(Boolean)
// @description
// Returns whether enough players are sleeping to prepare for the night to advance.
// Typically used before checking <@link tag WorldTag.enough_deep_sleeping>
// By default, automatically checks the playersSleepingPercentage gamerule,
// but this can optionally be overridden by specifying a percentage integer.
// Any integer above 100 will always yield 'false'. Requires at least one player to be sleeping to return 'true'.
// NOTE: In 1.16, input is ignored and assumed to be 100%.
// -->
registerTag(ElementTag.class, "enough_sleeping", (attribute, world) -> {
int percentage = 100;
if (attribute.hasParam()) {
percentage = attribute.getIntParam();
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
percentage = world.getGameRuleOrDefault(GameRule.PLAYERS_SLEEPING_PERCENTAGE);
}
return new ElementTag(NMSHandler.worldHelper.areEnoughSleeping(world.getWorld(), percentage));
});
// <--[tag]
// @attribute <WorldTag.enough_deep_sleeping[(<#>)]>
// @returns ElementTag(Boolean)
// @description
// Returns whether enough players have been in bed long enough for the night to advance (generally 100 ticks).
// Loops through all online players, so is typically used after checking <@link tag WorldTag.enough_sleeping>
// By default, automatically checks the playersSleepingPercentage gamerule,
// but this can optionally be overridden by specifying a percentage integer.
// Any integer above 100 will always yield 'false'. Requires at least one player to be sleeping to return 'true'.
// NOTE: In 1.16, input is ignored and assumed to be 100%.
// -->
registerTag(ElementTag.class, "enough_deep_sleeping", (attribute, world) -> {
int percentage = 100;
if (attribute.hasParam()) {
percentage = attribute.getIntParam();
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
percentage = world.getGameRuleOrDefault(GameRule.PLAYERS_SLEEPING_PERCENTAGE);
}
return new ElementTag(NMSHandler.worldHelper.areEnoughDeepSleeping(world.getWorld(), percentage));
});
// <--[tag]
// @attribute <WorldTag.sky_darkness>
// @returns ElementTag(Number)