/
ChunkTag.java
928 lines (850 loc) · 35.1 KB
/
ChunkTag.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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker;
import com.denizenscript.denizen.utilities.flags.LocationFlagSearchHelper;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizen.nms.NMSHandler;
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.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.*;
public class ChunkTag implements ObjectTag, Adjustable, FlaggableObject {
// <--[ObjectType]
// @name ChunkTag
// @prefix ch
// @base ElementTag
// @implements FlaggableObject
// @ExampleTagBase player.location.chunk
// @ExampleValues <player.location.chunk>
// @ExampleForReturns
// - chunkload %VALUE%
// @ExampleForReturns
// - adjust %VALUE% set_all_biomes:desert
// @format
// The identity format for chunks is <x>,<z>,<world>
// For example, 'ch@5,3,world'.
//
// @description
// A ChunkTag represents a chunk in the world.
//
// Note that the X/Z pair are chunk coordinates, not block coordinates.
// To convert from block coordinates to chunk coordinates, divide by 16 and round downward.
// Note that negative chunks are one unit lower than you might expect.
// To understand why, simply look at chunks on a number line...
// x x x x x
// -2 -1 b 0 a 1 2
// The block 'a' at block position '1' is in chunk '0', but the block 'b' at block position '-1' is in chunk '-1'.
// As otherwise (if 'b' was in chunk '0'), chunk '0' would be double-wide (32 blocks wide instead of the standard 16).
//
// For example, block at X,Z 32,67 is in the chunk at X,Z 2,4
// And the block at X,Z -32,-67 is in the chunk at X,Z -2,-5
//
// This object type is flaggable.
// Flags on this object type will be stored in the chunk file inside the world folder.
//
// -->
//////////////////
// OBJECT FETCHER
////////////////
@Deprecated
public static ChunkTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("ch")
public static ChunkTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
string = CoreUtilities.toLowerCase(string).replace("ch@", "");
String[] parts = string.split(",");
if (parts.length != 3) {
if (context == null || context.showErrors()) {
Debug.log("Minor: valueOf ChunkTag unable to handle malformed format: " + "ch@" + string);
}
return null;
}
try {
return new ChunkTag(new WorldTag(parts[2]), Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
}
catch (Exception e) {
if (context == null || context.showErrors()) {
Debug.log("Minor: valueOf ChunkTag returning null: " + "ch@" + string);
}
return null;
}
}
public static boolean matches(String string) {
return valueOf(string, CoreUtilities.noDebugContext) != null;
}
int chunkX, chunkZ;
WorldTag world;
Chunk cachedChunk;
public Chunk getChunkForTag(Attribute attribute) {
if (cachedChunk != null) {
return cachedChunk;
}
NMSHandler.chunkHelper.changeChunkServerThread(getBukkitWorld());
try {
if (!isLoaded()) {
if (!attribute.hasAlternative()) {
Debug.echoError("Cannot get chunk at " + chunkX + ", " + chunkZ + ": Chunk is not loaded. Use the 'chunkload' command to ensure the chunk is loaded.");
}
return null;
}
return getChunk();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getBukkitWorld());
}
}
public Chunk getChunk() {
if (cachedChunk == null) {
cachedChunk = getBukkitWorld().getChunkAt(chunkX, chunkZ);
}
return cachedChunk;
}
/**
* ChunkTag can be constructed with a Chunk
*
* @param chunk The chunk to use.
*/
public ChunkTag(Chunk chunk) {
this.cachedChunk = chunk;
world = new WorldTag(chunk.getWorld());
chunkX = chunk.getX();
chunkZ = chunk.getZ();
}
public ChunkTag(WorldTag world, int x, int z) {
this.world = world;
chunkX = x;
chunkZ = z;
}
/**
* ChunkTag can be constructed with a Location (or LocationTag)
*
* @param location The location of the chunk.
*/
public ChunkTag(Location location) {
world = location instanceof LocationTag ? new WorldTag(((LocationTag) location).getWorldName()) : new WorldTag(location.getWorld());
chunkX = location.getBlockX() >> 4;
chunkZ = location.getBlockZ() >> 4;
}
public LocationTag getCenter() {
return new LocationTag(getWorldName(), getX() * 16 + 8, 128, getZ() * 16 + 8, 0, 0);
}
public int getX() {
return chunkX;
}
public int getZ() {
return chunkZ;
}
public String getWorldName() {
return world.getName();
}
public World getBukkitWorld() {
return world.getWorld();
}
String prefix = "Chunk";
@Override
public String getPrefix() {
return prefix;
}
@Override
public ChunkTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String identify() {
return "ch@" + getX() + ',' + getZ() + ',' + getWorldName();
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public Object getJavaObject() {
return getChunk();
}
public boolean isLoaded() {
if (getBukkitWorld() == null) {
return false;
}
return getBukkitWorld().isChunkLoaded(chunkX, chunkZ);
}
public boolean isLoadedSafe() {
try {
NMSHandler.chunkHelper.changeChunkServerThread(getBukkitWorld());
return isLoaded();
}
finally {
NMSHandler.chunkHelper.restoreServerThread(getBukkitWorld());
}
}
@Override
public AbstractFlagTracker getFlagTracker() {
return new DataPersistenceFlagTracker(getChunk(), "flag_chunk_");
}
@Override
public AbstractFlagTracker getFlagTrackerForTag() {
if (!isLoadedSafe()) {
return null;
}
return getFlagTracker();
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
@Override
public String getReasonNotFlaggable() {
return "is the chunk loaded?";
}
public static void register() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
// <--[tag]
// @attribute <ChunkTag.add[<#>,<#>]>
// @returns ChunkTag
// @description
// Returns the chunk with the specified coordinates added to it.
// @example
// # Adds 10 to the X and Z coordinates of the player's current chunk and loads it.
// - chunkload <player.location.chunk.add[10,10]>
// -->
tagProcessor.registerTag(ChunkTag.class, ElementTag.class, "add", (attribute, object, addCoords) -> {
List<String> coords = CoreUtilities.split(addCoords.toString(), ',');
if (coords.size() < 2) {
attribute.echoError("The tag ChunkTag.add[<#>,<#>] requires two values!");
return null;
}
if (!ArgumentHelper.matchesInteger(coords.get(0)) || !ArgumentHelper.matchesInteger(coords.get(1))) {
attribute.echoError("Input to ChunkTag.add[x,z] is not a valid integer pair!");
return null;
}
int x = Integer.parseInt(coords.get(0));
int z = Integer.parseInt(coords.get(1));
return new ChunkTag(object.world, object.chunkX + x, object.chunkZ + z);
});
// <--[tag]
// @attribute <ChunkTag.sub[<#>,<#>]>
// @returns ChunkTag
// @description
// Returns the chunk with the specified coordinates subtracted from it.
// @example
// # Subtracts 10 from the X and Z coordinates of the player's current chunk and loads it.
// - chunkload <player.location.chunk.sub[10,10]>
// -->
tagProcessor.registerTag(ChunkTag.class, ElementTag.class, "sub", (attribute, object, subCoords) -> {
List<String> coords = CoreUtilities.split(subCoords.toString(), ',');
if (coords.size() < 2) {
attribute.echoError("The tag ChunkTag.sub[<#>,<#>] requires two values!");
return null;
}
if (!ArgumentHelper.matchesInteger(coords.get(0)) || !ArgumentHelper.matchesInteger(coords.get(1))) {
attribute.echoError("Input to ChunkTag.sub[x,z] is not a valid integer pair!");
return null;
}
int x = Integer.parseInt(coords.get(0));
int z = Integer.parseInt(coords.get(1));
return new ChunkTag(object.world, object.chunkX - x, object.chunkZ - z);
});
// <--[tag]
// @attribute <ChunkTag.is_generated>
// @returns ElementTag(Boolean)
// @description
// Returns true if the chunk has already been generated.
// @example
// # Loops though the chunks surrounding the player in a 20x20 radius and loads the chunk if it has not been generated yet.
// # Loading the chunk will generate it if it has not been generated already.
// - repeat 20 from:-10 as:x:
// - repeat 20 from:-10 as:z:
// - if !<player.location.chunk.add[<[x]>,<[z]>].is_generated>:
// - chunkload <player.location.chunk.add[<[x]>,<[z]>]>
// -->
tagProcessor.registerTag(ElementTag.class, "is_generated", (attribute, object) -> {
return new ElementTag(object.getBukkitWorld().isChunkGenerated(object.chunkX, object.chunkZ));
});
// <--[tag]
// @attribute <ChunkTag.is_loaded>
// @returns ElementTag(Boolean)
// @description
// Returns true if the chunk is currently loaded into memory.
// @example
// # Loops though the chunks surrounding the player in a 20x20 radius and loads the chunk if it is not already loaded.
// - repeat 20 from:-10 as:x:
// - repeat 20 from:-10 as:z:
// - if !<player.location.chunk.add[<[x]>,<[z]>].is_loaded>:
// - chunkload <player.location.chunk.add[<[x]>,<[z]>]>
// -->
tagProcessor.registerTag(ElementTag.class, "is_loaded", (attribute, object) -> {
return new ElementTag(object.isLoadedSafe());
});
// <--[tag]
// @attribute <ChunkTag.force_loaded>
// @returns ElementTag(Boolean)
// @mechanism ChunkTag.force_loaded
// @description
// Returns whether the chunk is forced to stay loaded at all times.
// @example
// - if <player.location.chunk.force_loaded>:
// - narrate "This chunk is being forced to stay loaded!"
// - else:
// - narrate "This chunk is NOT being forced to stay loaded!"
// -->
tagProcessor.registerTag(ElementTag.class, "force_loaded", (attribute, object) -> {
if (!object.isLoadedSafe()) {
return new ElementTag(false);
}
Chunk chunk = object.getChunkForTag(attribute);
return new ElementTag(chunk != null && chunk.isForceLoaded());
});
// <--[tag]
// @attribute <ChunkTag.plugin_tickets>
// @returns ListTag(PluginTag)
// @mechanism ChunkTag.clear_plugin_tickets
// @description
// Returns a list of plugins that are keeping this chunk loaded.
// This is related to the <@link command chunkload> command.
// @example
// # Narrates the list of plugin names keeping the player's chunk loaded formatted into readable format.
// # Example: "Plugins keeping your chunk loaded: Denizen and Citizens".
// - narrate "Plugins keeping your chunk loaded: <player.location.chunk.plugin_tickets.formatted>"
// -->
tagProcessor.registerTag(ListTag.class, "plugin_tickets", (attribute, object) -> {
if (!object.isLoadedSafe()) {
return new ListTag();
}
Chunk chunk = object.getChunkForTag(attribute);
ListTag result = new ListTag();
for (Plugin plugin : chunk.getPluginChunkTickets()) {
result.addObject(new PluginTag(plugin));
}
return result;
});
// <--[tag]
// @attribute <ChunkTag.x>
// @returns ElementTag(Number)
// @description
// Returns the x coordinate of the chunk.
// @example
// # Narrates the player's chunk's X coordinate.
// # For example, if the player was in <chunk[5,10,world]>, the X coordinate would be "5".
// - narrate "Your current X chunk coordinate is: <player.location.chunk.x>!"
// -->
tagProcessor.registerTag(ElementTag.class, "x", (attribute, object) -> {
return new ElementTag(object.chunkX);
});
// <--[tag]
// @attribute <ChunkTag.z>
// @returns ElementTag(Number)
// @description
// Returns the z coordinate of the chunk.
// @example
// # Narrates the player's chunk's Z coordinate.
// # For example, if the player was in <chunk[5,10,world]>, the Z coordinate would be "10".
// - narrate "Your current Z chunk coordinate is: <player.location.chunk.z>!"
// -->
tagProcessor.registerTag(ElementTag.class, "z", (attribute, object) -> {
return new ElementTag(object.chunkZ);
});
// <--[tag]
// @attribute <ChunkTag.world>
// @returns WorldTag
// @description
// Returns the world associated with the chunk.
// @example
// # Narrates the name of the player's chunk's associated world.
// # For example, if the player was in <chunk[5,10,world]>, the world the chunk is in would be "world".
// - narrate "The world your chunk is in is: <player.location.chunk.world.name>!"
// -->
tagProcessor.registerTag(WorldTag.class, "world", (attribute, object) -> {
return object.world;
});
// <--[tag]
// @attribute <ChunkTag.cuboid>
// @returns CuboidTag
// @description
// Returns a cuboid of this chunk.
// @example
// # Plays the "flame" effect at the player's chunk as a cuboid outlined along the player's Y coordinate.
// - playeffect effect:flame at:<player.location.chunk.cuboid.outline_2d[<player.location.y>]> offset:0.0
// -->
tagProcessor.registerTag(CuboidTag.class, "cuboid", (attribute, object) -> {
int yMin = 0, yMax = 255;
World world = object.getBukkitWorld();
if (world != null) {
yMin = world.getMinHeight();
yMax = world.getMaxHeight();
}
return new CuboidTag(new LocationTag(object.getWorldName(), object.getX() * 16, yMin, object.getZ() * 16, 0, 0),
new LocationTag(object.getWorldName(), object.getX() * 16 + 15, yMax, object.getZ() * 16 + 15, 0, 0));
});
// <--[tag]
// @attribute <ChunkTag.tile_entities>
// @returns ListTag(LocationTag)
// @description
// Returns a list of tile entity locations in the chunk.
// @example
// # Spawns a debugblock to highlight every tile entity in the chunk.
// - debugblock <player.location.chunk.tile_entities>
// -->
tagProcessor.registerTag(ListTag.class, "tile_entities", (attribute, object) -> {
ListTag tiles = new ListTag();
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
try {
NMSHandler.chunkHelper.changeChunkServerThread(object.getBukkitWorld());
for (BlockState block : chunk.getTileEntities()) {
tiles.addObject(new LocationTag(block.getLocation()));
}
}
finally {
NMSHandler.chunkHelper.restoreServerThread(object.getBukkitWorld());
}
return tiles;
});
// <--[tag]
// @attribute <ChunkTag.entities[(<entity>|...)]>
// @returns ListTag(EntityTag)
// @description
// Returns a list of entities in the chunk.
// Optionally specify entity types to filter down to.
// @example
// # Loops though the entities found in the player's chunk and narrates the name and location of it.
// - foreach <player.location.chunk.entities> as:entity:
// - narrate "Found a <[entity].name> at <[entity].location.simple>!"
// @example
// # Loops though the axolotls found in the player's chunk and narrates the name and location of it.
// - foreach <player.location.chunk.entities[axolotl]> as:entity:
// - narrate "Found an axolotl at <[entity].location.simple>!"
// -->
tagProcessor.registerTag(ListTag.class, "entities", (attribute, object) -> {
ListTag entities = new ListTag();
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
ListTag typeFilter = attribute.hasParam() ? attribute.paramAsType(ListTag.class) : null;
try {
NMSHandler.chunkHelper.changeChunkServerThread(object.getBukkitWorld());
for (Entity entity : chunk.getEntities()) {
EntityTag current = new EntityTag(entity);
if (typeFilter != null) {
for (String type : typeFilter) {
if (current.comparedTo(type)) {
entities.addObject(current.getDenizenObject());
break;
}
}
}
else {
entities.addObject(current.getDenizenObject());
}
}
}
finally {
NMSHandler.chunkHelper.restoreServerThread(object.getBukkitWorld());
}
return entities;
});
// <--[tag]
// @attribute <ChunkTag.living_entities>
// @returns ListTag(EntityTag)
// @description
// Returns a list of living entities in the chunk.
// This includes Players, mobs, NPCs, etc., but excludes dropped items, experience orbs, etc.
// @example
// # Loops though the living entities found in the player's chunk and narrates the name and location of it.
// - foreach <player.location.chunk.living_entities> as:entity:
// - narrate "Found a <[entity].name> at <[entity].location.simple>!"
// -->
tagProcessor.registerTag(ListTag.class, "living_entities", (attribute, object) -> {
ListTag entities = new ListTag();
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
try {
NMSHandler.chunkHelper.changeChunkServerThread(object.getBukkitWorld());
for (Entity ent : chunk.getEntities()) {
if (ent instanceof LivingEntity) {
entities.addObject(new EntityTag(ent).getDenizenObject());
}
}
}
finally {
NMSHandler.chunkHelper.restoreServerThread(object.getBukkitWorld());
}
return entities;
});
// <--[tag]
// @attribute <ChunkTag.players>
// @returns ListTag(PlayerTag)
// @description
// Returns a list of players in the chunk.
// @example
// # Narrates a list of players excluding the original player in the chunk formatted into a readable format.
// # For example, this can return: "steve, alex, john, and jane".
// - narrate "Wow! Look at all these players in the same chunk as you: <player.location.chunk.players.exclude[<player>].formatted>!"
// -->
tagProcessor.registerTag(ListTag.class, "players", (attribute, object) -> {
ListTag entities = new ListTag();
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
for (Entity ent : chunk.getEntities()) {
if (EntityTag.isPlayer(ent)) {
entities.addObject(new PlayerTag((Player) ent));
}
}
return entities;
});
// <--[tag]
// @attribute <ChunkTag.height_map>
// @returns ListTag
// @description
// Returns a list of the height of each block in the chunk.
// @example
// # Narrates the height as a number of the highest block in the chunk.
// - narrate "The block with the tallest height has a height of <player.location.chunk.height_map.highest>!"
// -->
tagProcessor.registerTag(ListTag.class, "height_map", (attribute, object) -> {
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
int[] heightMap = NMSHandler.chunkHelper.getHeightMap(chunk);
List<String> height_map = new ArrayList<>(heightMap.length);
for (int i : heightMap) {
height_map.add(String.valueOf(i));
}
return new ListTag(height_map);
});
// <--[tag]
// @attribute <ChunkTag.average_height>
// @returns ElementTag(Decimal)
// @description
// Returns the average height of the blocks in the chunk.
// @example
// # Narrates the average height of each block in the player's chunk rounded.
// - narrate "The average height of blocks in this chunk is <player.location.chunk.average_height.round>!"
// -->
tagProcessor.registerTag(ElementTag.class, "average_height", (attribute, object) -> {
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
int[] heightMap = NMSHandler.chunkHelper.getHeightMap(chunk);
int sum = 0;
for (int i : heightMap) {
sum += i;
}
return new ElementTag(((double) sum) / heightMap.length);
});
// <--[tag]
// @attribute <ChunkTag.is_flat[(<#>)]>
// @returns ElementTag(Boolean)
// @description
// Scans the heights of the blocks to check variance between them.
// If no number is supplied, is_flat will return true if all the blocks are less than 2 blocks apart in height.
// Specifying a number will modify the number criteria for determining if it is flat.
// @example
// - if <player.location.chunk.is_flat>:
// - narrate "Wow! That is a flat chunk!"
// - else:
// - narrate "Watch out! This chunk has a more rugged terrain!"
// @example
// - if <player.location.chunk.is_flat[4]>:
// - narrate "Wow! This chunk's blocks are all less than 4 blocks apart in height!"
// - else:
// - narrate "This chunk's blocks do not meet the criteria of being flat."
// -->
tagProcessor.registerTag(ElementTag.class, "is_flat", (attribute, object) -> {
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
int[] heightMap = NMSHandler.chunkHelper.getHeightMap(chunk);
int tolerance = 2;
if (attribute.hasParam() && ArgumentHelper.matchesInteger(attribute.getParam())) {
tolerance = attribute.getIntParam();
}
int x = heightMap[0];
for (int i : heightMap) {
if (Math.abs(x - i) > tolerance) {
return new ElementTag(false);
}
}
return new ElementTag(true);
});
// <--[tag]
// @attribute <ChunkTag.surface_blocks>
// @returns ListTag(LocationTag)
// @description
// Returns a list of the highest non-air surface blocks in the chunk.
// @example
// # Spawns a creeper above a random surface block to prevent the creeper from suffocating.
// - spawn creeper <player.location.chunk.surface_blocks.random.above>
// -->
tagProcessor.registerTag(ListTag.class, "surface_blocks", (attribute, object) -> {
ListTag surface_blocks = new ListTag();
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
ChunkSnapshot snapshot = chunk.getChunkSnapshot();
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
surface_blocks.addObject(new LocationTag(chunk.getWorld(), chunk.getX() << 4 | x, snapshot.getHighestBlockYAt(x, z) - 1, chunk.getZ() << 4 | z));
}
}
return surface_blocks;
});
// <--[tag]
// @attribute <ChunkTag.blocks_flagged[<flag_name>]>
// @returns ListTag(LocationTag)
// @description
// Gets a list of all block locations with a specified flag within the CuboidTag.
// Searches the internal flag lists, rather than through all possible blocks.
// @example
// # Spawns a debugblock to highlight every block in the chunk flagged "my_flag"
// - debugblock <player.location.chunk.blocks_flagged[my_flag]>
// -->
tagProcessor.registerTag(ListTag.class, ElementTag.class, "blocks_flagged", (attribute, object, flagNameInput) -> {
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
String flagName = CoreUtilities.toLowerCase(flagNameInput.toString());
ListTag blocks = new ListTag();
LocationFlagSearchHelper.getFlaggedLocations(chunk, flagName, (loc) -> {
blocks.addObject(new LocationTag(loc));
});
return blocks;
});
// <--[tag]
// @attribute <ChunkTag.spawn_slimes>
// @returns ElementTag(Boolean)
// @description
// Returns whether the chunk is a specially located 'slime spawner' chunk.
// @example
// - if <player.location.chunk.spawn_slimes>:
// - narrate "Watch out! Slimes can spawn here!"
// - else:
// - narrate "No slimes can spawn here! You are safe for now!"
// -->
tagProcessor.registerTag(ElementTag.class, "spawn_slimes", (attribute, object) -> {
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
return new ElementTag(chunk.isSlimeChunk());
});
// <--[tag]
// @attribute <ChunkTag.inhabited_time>
// @returns DurationTag
// @mechanism ChunkTag.inhabited_time
// @description
// Returns the total time the chunk has been inhabited for.
// This is a primary deciding factor in the "local difficulty" setting.
// @example
// # Narrates the time inhabited in the chunk by the player formatted into words.
// # For example: "You have been in this chunk for a total of 2 hours 26 minutes!"
// - narrate "You have been in this chunk for a total of <player.location.chunk.inhabited_time.formatted_words>!"
// -->
tagProcessor.registerTag(DurationTag.class, "inhabited_time", (attribute, object) -> {
Chunk chunk = object.getChunkForTag(attribute);
if (chunk == null) {
return null;
}
return new DurationTag(chunk.getInhabitedTime());
});
}
public static ObjectTagProcessor<ChunkTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to a chunk!");
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ChunkTag
// @name inhabited_time
// @input DurationTag
// @description
// Changes the amount of time the chunk has been inhabited for.
// This is a primary deciding factor in the "local difficulty" setting.
// @tags
// <ChunkTag.inhabited_time>
// @example
// - adjust <player.location.chunk> inhabited_time:5d
// -->
if (mechanism.matches("inhabited_time") && mechanism.requireObject(DurationTag.class)) {
getChunk().setInhabitedTime(mechanism.valueAsType(DurationTag.class).getTicks());
}
// <--[mechanism]
// @object ChunkTag
// @name unload
// @input None
// @description
// Removes a chunk from memory.
// @tags
// <ChunkTag.is_loaded>
// @example
// - adjust <player.location.chunk.add[20,20]> unload
// -->
if (mechanism.matches("unload")) {
getBukkitWorld().unloadChunk(getX(), getZ(), true);
}
// <--[mechanism]
// @object ChunkTag
// @name unload_without_saving
// @input None
// @description
// Removes a chunk from memory without saving any recent changes.
// @tags
// <chunk.is_loaded>
// @example
// - adjust <player.location.chunk.add[20,20]> unload_without_saving
// -->
if (mechanism.matches("unload_without_saving")) {
getBukkitWorld().unloadChunk(getX(), getZ(), false);
}
// <--[mechanism]
// @object ChunkTag
// @name force_loaded
// @input ElementTag(Boolean)
// @description
// Sets whether this plugin is force-loaded or not.
// Unless you have a specific reason to use this, prefer <@link command chunkload>.
// @tags
// <ChunkTag.force_loaded>
// @example
// - adjust <player.location.chunk> force_loaded:true
// -->
if (mechanism.matches("force_loaded") && mechanism.requireBoolean()) {
getChunk().setForceLoaded(mechanism.getValue().asBoolean());
}
// <--[mechanism]
// @object ChunkTag
// @name clear_plugin_tickets
// @input None
// @description
// Forcibly removes all plugin tickets from this chunk, usually allowing it to unload.
// This is usually a bad idea.
// @tags
// <ChunkTag.plugin_tickets>
// @example
// # Make sure you know what you are doing before using this mechanism.
// - adjust <player.location.chunk> clear_plugin_tickets
// -->
if (mechanism.matches("clear_plugin_tickets")) {
for (Plugin plugin : new ArrayList<>(getChunk().getPluginChunkTickets())) {
getChunk().removePluginChunkTicket(plugin);
}
}
// <--[mechanism]
// @object ChunkTag
// @name load
// @input None
// @description
// Loads a chunk into memory.
// @tags
// <ChunkTag.is_loaded>
// @example
// - adjust <player.location.chunk.add[100,0]> load
// -->
if (mechanism.matches("load")) {
getChunk().load(true);
}
// <--[mechanism]
// @object ChunkTag
// @name regenerate
// @input None
// @description
// Causes the chunk to be entirely deleted and reformed from the world's seed.
// At time of writing this method only works as expected on Paper, and will error on Spigot.
// @example
// - adjust <player.location.chunk> regenerate
// -->
if (mechanism.matches("regenerate")) {
getBukkitWorld().regenerateChunk(getX(), getZ());
}
// <--[mechanism]
// @object ChunkTag
// @name refresh_chunk
// @input None
// @description
// Refreshes the chunk, sending any changed properties to players.
// @example
// - adjust <player.location.chunk> refresh_chunk
// -->
if (mechanism.matches("refresh_chunk")) {
final int chunkX = getX();
final int chunkZ = getZ();
getBukkitWorld().refreshChunk(chunkX, chunkZ);
}
// <--[mechanism]
// @object ChunkTag
// @name refresh_chunk_sections
// @input None
// @description
// Refreshes all 16x16x16 chunk sections within the chunk.
// @example
// - adjust <player.location.chunk> refresh_chunk_sections
// -->
if (mechanism.matches("refresh_chunk_sections")) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
getBukkitWorld().regenerateChunk(chunkX, chunkZ);
}
else {
NMSHandler.chunkHelper.refreshChunkSections(getChunk());
}
}
// <--[mechanism]
// @object ChunkTag
// @name set_all_biomes
// @input BiomeTag
// @description
// Sets all biomes in the chunk to the given biome.
// @example
// - adjust <player.location.chunk> set_all_biomes:<biome[savanna]>
// # Allow players to see the biome change:
// - adjust <player.location.chunk> refresh_chunk
// -->
if (mechanism.matches("set_all_biomes") && mechanism.requireObject(BiomeTag.class)) {
NMSHandler.chunkHelper.setAllBiomes(getChunk(), mechanism.valueAsType(BiomeTag.class).getBiome());
}
tagProcessor.processMechanism(this, mechanism);
}
}