-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
PlayerTag.java
3986 lines (3666 loc) · 163 KB
/
PlayerTag.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.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.abstracts.ImprovedOfflinePlayer;
import com.denizenscript.denizen.nms.abstracts.Sidebar;
import com.denizenscript.denizen.nms.interfaces.AdvancementHelper;
import com.denizenscript.denizen.objects.properties.entity.EntityHealth;
import com.denizenscript.denizen.scripts.commands.player.DisguiseCommand;
import com.denizenscript.denizen.scripts.commands.player.ExperienceCommand;
import com.denizenscript.denizen.scripts.commands.player.SidebarCommand;
import com.denizenscript.denizen.scripts.commands.server.BossBarCommand;
import com.denizenscript.denizen.tags.core.PlayerTagBase;
import com.denizenscript.denizen.utilities.AdvancedTextImpl;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.ScoreboardHelper;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.blocks.FakeBlock;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.entity.BossBarHelper;
import com.denizenscript.denizen.utilities.entity.FakeEntity;
import com.denizenscript.denizen.utilities.entity.HideEntitiesHelper;
import com.denizenscript.denizen.utilities.flags.PlayerFlagHandler;
import com.denizenscript.denizen.utilities.packets.DenizenPacketHandler;
import com.denizenscript.denizen.utilities.packets.HideParticles;
import com.denizenscript.denizen.utilities.packets.ItemChangeMessage;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.*;
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.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.NPCSelector;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.*;
import org.bukkit.advancement.Advancement;
import org.bukkit.advancement.AdvancementProgress;
import org.bukkit.block.banner.PatternType;
import org.bukkit.boss.BossBar;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.*;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.map.MapView;
import org.bukkit.util.RayTraceResult;
import java.util.*;
public class PlayerTag implements ObjectTag, Adjustable, EntityFormObject, FlaggableObject {
// <--[ObjectType]
// @name PlayerTag
// @prefix p
// @base EntityTag
// @implements FlaggableObject
// @ExampleTagBase player
// @ExampleValues <player>
// @ExampleForReturns
// - kill %VALUE%
// @ExampleForReturns
// - heal %VALUE%
// @ExampleForReturns
// - narrate "hello there!" targets:%VALUE%
// @format
// The identity format for players is the UUID of the relevant player.
//
// @description
// A PlayerTag represents a player in the game.
//
// This object type is flaggable.
// Flags on this object type will be stored in the file "plugins/Denizen/player_flags/(UUID).dat",
// with automatic loading only when the player is online and caching for interacting with offline player flags.
//
// -->
/////////////////////
// STATIC METHODS
/////////////////
public static PlayerTag mirrorBukkitPlayer(OfflinePlayer player) {
if (player == null) {
return null;
}
else {
return new PlayerTag(player);
}
}
static Map<String, UUID> playerNames = new HashMap<>();
/**
* Notes that the player exists, for easy PlayerTag valueOf handling.
*/
public static void notePlayer(OfflinePlayer player) {
if (player.getName() == null) {
Debug.echoError("Null named player " + player + " - may be file corruption, or player data imported from non-bukkit server?");
return;
}
if (!playerNames.containsKey(CoreUtilities.toLowerCase(player.getName()))) {
playerNames.put(CoreUtilities.toLowerCase(player.getName()), player.getUniqueId());
}
}
public static boolean isNoted(OfflinePlayer player) {
return playerNames.containsValue(player.getUniqueId());
}
public static Map<String, UUID> getAllPlayers() {
return playerNames;
}
/////////////////////
// OBJECT FETCHER
/////////////////
@Deprecated
public static PlayerTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("p")
public static PlayerTag valueOf(String string, TagContext context) {
return valueOfInternal(string, context, true);
}
public static String playerByNameMessage = BukkitImplDeprecations.playerByNameWarning.message;
public static PlayerTag valueOfInternal(String string, TagContext context, boolean defaultAnnounce) {
if (string == null) {
return null;
}
boolean announce = context == null ? defaultAnnounce : context.showErrors();
string = CoreUtilities.toLowerCase(string);
if (string.startsWith("p@")) {
string = string.substring("p@".length());
}
if (string.length() == 36 && string.indexOf('-') >= 0) {
try {
UUID uuid = UUID.fromString(string);
if (uuid != null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
if (player != null) {
return new PlayerTag(player);
}
}
}
catch (IllegalArgumentException e) {
// Nothing
}
}
// Match as a player name
if (string.length() <= 16 && playerNames.containsKey(string)) {
OfflinePlayer player = Bukkit.getOfflinePlayer(playerNames.get(string));
if (announce && (context == null || context.script != null)) { // 'script != null' check is to allow ex command usage silently
BukkitImplDeprecations.playerByNameWarning.message = playerByNameMessage + " Player named '" + player.getName() + "' has UUID: " + player.getUniqueId();
BukkitImplDeprecations.playerByNameWarning.warn(context);
}
return new PlayerTag(player);
}
if (announce) {
Debug.log("Minor: Invalid Player! '" + string + "' could not be found.");
}
return null;
}
public static boolean matches(String arg) {
if (arg == null) {
return false;
}
arg = CoreUtilities.toLowerCase(arg);
if (arg.startsWith("p@")) {
return true;
}
if (arg.length() == 36 && arg.indexOf('-') >= 0) {
try {
UUID uuid = UUID.fromString(arg);
if (uuid != null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
if (player != null && (player.isOnline() || player.hasPlayedBefore())) {
return true;
}
}
}
catch (IllegalArgumentException e) {
// Nothing
}
}
return false;
}
public static boolean playerNameIsValid(String name) {
return playerNames.containsKey(CoreUtilities.toLowerCase(name));
}
/////////////////////
// CONSTRUCTORS
/////////////////
public PlayerTag(OfflinePlayer player) {
this(player.getUniqueId());
}
public PlayerTag(UUID uuid) {
this.uuid = uuid;
}
public PlayerTag(Player player) {
this((OfflinePlayer) player);
if (EntityTag.isNPC(player)) {
throw new IllegalStateException("NPCs are not allowed as PlayerTag objects!");
}
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
@Override
public AbstractFlagTracker getFlagTracker() {
return PlayerFlagHandler.getTrackerFor(getUUID());
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
UUID uuid;
public boolean isValid() {
OfflinePlayer pl = getOfflinePlayer();
if (pl != null && pl.hasPlayedBefore()) {
return true;
}
return getPlayerEntity() != null;
}
public Player getPlayerEntity() {
return Bukkit.getPlayer(uuid);
}
public UUID getUUID() {
return uuid;
}
public OfflinePlayer getOfflinePlayer() {
return Bukkit.getOfflinePlayer(uuid);
}
public ImprovedOfflinePlayer getNBTEditor() {
return NMSHandler.playerHelper.getOfflineData(getOfflinePlayer());
}
@Override
public EntityTag getDenizenEntity() {
return new EntityTag(getPlayerEntity());
}
public NPCTag getSelectedNPC() {
if (Depends.citizens != null && CitizensAPI.hasImplementation()) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(getPlayerEntity());
if (npc != null) {
return new NPCTag(npc);
}
}
return null;
}
public String getName() {
return getOfflinePlayer().getName();
}
@Override
public LocationTag getLocation() {
if (isOnline()) {
return new LocationTag(getPlayerEntity().getLocation());
}
else {
return new LocationTag(getNBTEditor().getLocation());
}
}
public int getRemainingAir() {
if (isOnline()) {
return getPlayerEntity().getRemainingAir();
}
else {
return getNBTEditor().getRemainingAir();
}
}
public int getMaximumAir() {
if (isOnline()) {
return getPlayerEntity().getMaximumAir();
}
else {
return 300;
}
}
public double getHealth() {
if (isOnline()) {
return getPlayerEntity().getHealth();
}
else {
return getNBTEditor().getHealthFloat();
}
}
public double getMaxHealth() {
if (isOnline()) {
return getPlayerEntity().getMaxHealth();
}
else {
return getNBTEditor().getMaxHealth();
}
}
public int getFoodLevel() {
if (isOnline()) {
return getPlayerEntity().getFoodLevel();
}
else {
return getNBTEditor().getFoodLevel();
}
}
public LocationTag getEyeLocation() {
if (isOnline()) {
return new LocationTag(getPlayerEntity().getEyeLocation());
}
else {
return null;
}
}
public InventoryTag getInventory() {
if (isOnline()) {
return InventoryTag.mirrorBukkitInventory(getPlayerEntity().getInventory());
}
else {
return new InventoryTag(getNBTEditor());
}
}
public CraftingInventory getBukkitWorkbench() {
if (isOnline()) {
if (getPlayerEntity().getOpenInventory().getType() == InventoryType.WORKBENCH
|| getPlayerEntity().getOpenInventory().getType() == InventoryType.CRAFTING) {
return (CraftingInventory) getPlayerEntity().getOpenInventory().getTopInventory();
}
}
return null;
}
public InventoryTag getWorkbench() {
if (isOnline()) {
CraftingInventory workbench = getBukkitWorkbench();
if (workbench != null) {
return new InventoryTag(workbench, getPlayerEntity());
}
}
return null;
}
public InventoryTag getEnderChest() {
if (isOnline()) {
return new InventoryTag(getPlayerEntity().getEnderChest(), getPlayerEntity());
}
else {
return new InventoryTag(getNBTEditor(), true);
}
}
public WorldTag getWorldTag() {
if (isOnline()) {
return new WorldTag(getPlayerEntity().getWorld());
}
else {
return new WorldTag(getLocation().getWorldName());
}
}
public World getWorld() {
if (isOnline()) {
return getPlayerEntity().getWorld();
}
else {
return getLocation().getWorld();
}
}
public ItemTag getHeldItem() {
return new ItemTag(getPlayerEntity().getEquipment().getItemInMainHand());
}
public ItemTag getOffhandItem() {
return new ItemTag(getPlayerEntity().getEquipment().getItemInOffHand());
}
public void decrementStatistic(Statistic statistic, int amount) {
getOfflinePlayer().decrementStatistic(statistic, amount);
}
public void decrementStatistic(Statistic statistic, EntityType entity, int amount) {
if (statistic.getType() == Statistic.Type.ENTITY) {
getOfflinePlayer().decrementStatistic(statistic, entity, amount);
}
}
public void decrementStatistic(Statistic statistic, Material material, int amount) {
if (statistic.getType() == Statistic.Type.BLOCK || statistic.getType() == Statistic.Type.ITEM) {
getOfflinePlayer().decrementStatistic(statistic, material, amount);
}
}
public void incrementStatistic(Statistic statistic, int amount) {
getOfflinePlayer().incrementStatistic(statistic, amount);
}
public void incrementStatistic(Statistic statistic, EntityType entity, int amount) {
if (statistic.getType() == Statistic.Type.ENTITY) {
getOfflinePlayer().incrementStatistic(statistic, entity, amount);
}
}
public void incrementStatistic(Statistic statistic, Material material, int amount) {
if (statistic.getType() == Statistic.Type.BLOCK || statistic.getType() == Statistic.Type.ITEM) {
getOfflinePlayer().incrementStatistic(statistic, material, amount);
}
}
public void setStatistic(Statistic statistic, int amount) {
getOfflinePlayer().setStatistic(statistic, amount);
}
public void setStatistic(Statistic statistic, EntityType entity, int amount) {
if (statistic.getType() == Statistic.Type.ENTITY) {
getOfflinePlayer().setStatistic(statistic, entity, amount);
}
}
public void setStatistic(Statistic statistic, Material material, int amount) {
if (statistic.getType() == Statistic.Type.BLOCK || statistic.getType() == Statistic.Type.ITEM) {
getOfflinePlayer().setStatistic(statistic, material, amount);
}
}
public boolean isOnline() {
return getPlayerEntity() != null;
}
public void setBedSpawnLocation(Location location) {
if (isOnline()) {
getPlayerEntity().setBedSpawnLocation(location);
}
else {
getNBTEditor().setBedSpawnLocation(location, getNBTEditor().isSpawnForced());
}
}
public void setLocation(Location location) {
if (isOnline()) {
getPlayerEntity().teleport(location);
}
else {
getNBTEditor().setLocation(location);
}
}
public void setMaximumAir(int air) {
if (isOnline()) {
getPlayerEntity().setMaximumAir(air);
}
else {
Debug.echoError("Cannot set the maximum air of an offline player!");
}
}
public void setRemainingAir(int air) {
if (isOnline()) {
getPlayerEntity().setRemainingAir(air);
}
else {
getNBTEditor().setRemainingAir(air);
}
}
public void setHealth(double health) {
if (isOnline()) {
getPlayerEntity().setHealth(health);
}
else {
getNBTEditor().setHealthFloat((float) health);
}
}
public void setMaxHealth(double maxHealth) {
if (isOnline()) {
getPlayerEntity().setMaxHealth(maxHealth);
}
else {
getNBTEditor().setMaxHealth(maxHealth);
}
}
public void setFoodLevel(int foodLevel) {
if (isOnline()) {
getPlayerEntity().setFoodLevel(foodLevel);
}
else {
getNBTEditor().setFoodLevel(foodLevel);
}
}
public void setLevel(int level) {
if (isOnline()) {
getPlayerEntity().setLevel(level);
}
else {
getNBTEditor().setLevel(level);
}
}
public void setFlySpeed(float speed) {
if (isOnline()) {
getPlayerEntity().setFlySpeed(speed);
}
else {
getNBTEditor().setFlySpeed(speed);
}
}
public void setGameMode(GameMode mode) {
if (isOnline()) {
getPlayerEntity().setGameMode(mode);
}
else {
getNBTEditor().setGameMode(mode);
}
}
public boolean hasChunkLoaded(Chunk chunk) {
return NMSHandler.playerHelper.hasChunkLoaded(getPlayerEntity(), chunk);
}
/////////////////////
// ObjectTag Methods
/////////////////
private String prefix = "Player";
@Override
public String getPrefix() {
return prefix;
}
@Override
public PlayerTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debuggable() {
return "<LG>p@<Y>" + uuid + "<GR> (" + getOfflinePlayer().getName() + ")";
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String identify() {
return "p@" + uuid;
}
@Override
public String identifySimple() {
return "p@" + getOfflinePlayer().getName();
}
@Override
public String toString() {
return identify();
}
@Override
public Object getJavaObject() {
return getOfflinePlayer();
}
@Override
public int hashCode() {
return getUUID().hashCode();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof PlayerTag)) {
return false;
}
return getUUID().equals(((PlayerTag) other).getUUID());
}
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
/////////////////////
// OFFLINE ATTRIBUTES
/////////////////
// Defined in EntityTag
tagProcessor.registerTag(ElementTag.class, "is_player", (attribute, object) -> {
return new ElementTag(true);
});
/////////////////////
// DENIZEN ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <PlayerTag.chat_history_list>
// @returns ListTag
// @description
// Returns a list of the last 10 things the player has said, less if the player hasn't said all that much.
// Works with offline players, if the player previously joined and typed in chat since the last server restart.
// -->
tagProcessor.registerTag(ListTag.class, "chat_history_list", (attribute, object) -> {
List<String> history = PlayerTagBase.playerChatHistory.get(object.getUUID());
return history == null ? new ListTag() : new ListTag(history, true);
});
// <--[tag]
// @attribute <PlayerTag.chat_history[(<#>)]>
// @returns ElementTag
// @description
// Returns the last thing the player said.
// If a number is specified, returns an earlier thing the player said.
// Works with offline players.
// -->
tagProcessor.registerTag(ElementTag.class, "chat_history", (attribute, object) -> {
int x = 1;
if (attribute.hasParam() && ArgumentHelper.matchesInteger(attribute.getParam())) {
x = attribute.getIntParam();
}
List<String> messages = PlayerTagBase.playerChatHistory.get(object.getUUID());
if (messages == null || messages.size() < x || x < 1) {
return null;
}
return new ElementTag(messages.get(x - 1), true);
});
/////////////////////
// ECONOMY ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <PlayerTag.formatted_money>
// @returns ElementTag
// @plugin Vault
// @description
// Returns the formatted form of the player's money balance in the registered Economy system.
// -->
tagProcessor.registerTag(ElementTag.class, "formatted_money", (attribute, object) -> {
if (Depends.economy == null) {
if (!attribute.hasAlternative()) {
Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
return null;
}
return new ElementTag(Depends.economy.format(Depends.economy.getBalance(object.getOfflinePlayer())), true);
});
// <--[tag]
// @attribute <PlayerTag.money>
// @returns ElementTag(Decimal)
// @plugin Vault
// @description
// Returns the amount of money the player has with the registered Economy system.
// May work offline depending on economy provider.
// -->
tagProcessor.registerTag(ElementTag.class, "money", (attribute, object) -> {
if (Depends.economy == null) {
if (!attribute.hasAlternative()) {
Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
return null;
}
if (attribute.startsWith("formatted", 2)) {
attribute.fulfill(1);
BukkitImplDeprecations.playerMoneyFormatTag.warn(attribute.context);
return new ElementTag(Depends.economy.format(Depends.economy.getBalance(object.getOfflinePlayer())));
}
if (attribute.startsWith("currency_singular", 2)) {
attribute.fulfill(1);
BukkitImplDeprecations.oldEconomyTags.warn(attribute.context);
return new ElementTag(Depends.economy.currencyNameSingular());
}
if (attribute.startsWith("currency", 2)) {
attribute.fulfill(1);
BukkitImplDeprecations.oldEconomyTags.warn(attribute.context);
return new ElementTag(Depends.economy.currencyNamePlural());
}
return new ElementTag(Depends.economy.getBalance(object.getOfflinePlayer()));
});
/////////////////////
// ENTITY LIST ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <PlayerTag.target[(<matcher>)]>
// @returns EntityTag
// @description
// Returns the entity that the player is looking at, within a maximum range of 50 blocks,
// or null if the player is not looking at an entity.
// Optionally, specify an entity type matcher to only count matches as possible targets.
// -->
registerOnlineOnlyTag(ObjectTag.class, "target", (attribute, object) -> {
double range = 50;
String matcher = attribute.hasParam() ? attribute.getParam() : null;
// <--[tag]
// @attribute <PlayerTag.target[(<matcher>)].within[(<#.#>)]>
// @returns EntityTag
// @description
// Returns the living entity that the player is looking at within the specified range limit,
// or null if the player is not looking at an entity.
// Optionally, specify an entity type matcher to only count matches as possible targets.
// -->
if (attribute.startsWith("within", 2) && attribute.hasContext(2)) {
range = attribute.getDoubleContext(2);
attribute.fulfill(1);
}
Location eyeLoc = object.getEyeLocation();
RayTraceResult result = eyeLoc.getWorld().rayTrace(eyeLoc, eyeLoc.getDirection(), range, FluidCollisionMode.NEVER, true, 0.01, (e) -> {
if (e.getUniqueId().equals(object.getUUID())) {
return false;
}
if (matcher != null) {
return new EntityTag(e).tryAdvancedMatcher(matcher);
}
return true;
});
if (result == null || result.getHitEntity() == null) {
return null;
}
return new EntityTag(result.getHitEntity()).getDenizenObject();
});
/////////////////////
// LOCATION ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <PlayerTag.bed_spawn>
// @returns LocationTag
// @mechanism PlayerTag.bed_spawn_location
// @description
// Returns the location of the player's bed spawn location, null if
// it doesn't exist.
// Works with offline players.
// -->
registerOfflineTag(LocationTag.class, "bed_spawn", (attribute, object) -> {
try {
NMSHandler.chunkHelper.changeChunkServerThread(object.getWorld());
if (object.getOfflinePlayer().getBedSpawnLocation() == null) {
return null;
}
return new LocationTag(object.getOfflinePlayer().getBedSpawnLocation());
}
finally {
NMSHandler.chunkHelper.restoreServerThread(object.getWorld());
}
});
registerOfflineTag(ObjectTag.class, "location", (attribute, object) -> {
if (object.isOnline() && !object.getPlayerEntity().isDead()) {
return new EntityTag(object.getPlayerEntity()).doLocationTag(attribute);
}
return object.getLocation();
});
registerOfflineTag(WorldTag.class, "world", (attribute, object) -> {
return object.getWorldTag();
});
/////////////////////
// STATE ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <PlayerTag.item_cooldown[<material>]>
// @returns DurationTag
// @description
// Returns the cooldown duration remaining on player's material.
// -->
registerOnlineOnlyTag(DurationTag.class, "item_cooldown", (attribute, object) -> {
MaterialTag mat = new ElementTag(attribute.getParam()).asType(MaterialTag.class, attribute.context);
if (mat != null) {
return new DurationTag((long) object.getPlayerEntity().getCooldown(mat.getMaterial()));
}
return null;
});
// <--[tag]
// @attribute <PlayerTag.first_played_time>
// @returns TimeTag
// @description
// Returns the time of when the player first logged on to this server.
// Works with offline players.
// -->
tagProcessor.registerTag(TimeTag.class, "first_played_time", (attribute, object) -> {
return new TimeTag(object.getOfflinePlayer().getFirstPlayed());
});
tagProcessor.registerTag(DurationTag.class, "first_played", (attribute, object) -> {
BukkitImplDeprecations.playerTimePlayedTags.warn(attribute.context);
return new DurationTag(object.getOfflinePlayer().getFirstPlayed() / 50);
});
// <--[tag]
// @attribute <PlayerTag.has_played_before>
// @returns ElementTag(Boolean)
// @description
// Returns true if the player has played before, or is currently online. Returns false if this PlayerTag refers to a UUID that has never been on the server.
// Works with offline players.
// -->
tagProcessor.registerTag(ElementTag.class, "has_played_before", (attribute, object) -> {
return new ElementTag(object.isValid());
});
// <--[tag]
// @attribute <PlayerTag.exhaustion>
// @returns ElementTag(Decimal)
// @mechanism PlayerTag.exhaustion
// @description
// Returns the player's exhaustion value. Exhaustion is increased in vanilla when a player sprints or jumps, and is used to reduce food saturation over time.
// This can reach a maximum value of 40, and decreases by 4 every tick.
// Works with offline players.
// -->
registerOfflineTag(ElementTag.class, "exhaustion", (attribute, object) -> {
if (object.isOnline()) {
return new ElementTag(object.getPlayerEntity().getExhaustion());
}
else {
return new ElementTag(object.getNBTEditor().getExhaustion());
}
});
// Handle EntityTag oxygen tags here to allow getting them when the player is offline
registerOfflineTag(DurationTag.class, "max_oxygen", (attribute, object) -> {
return new DurationTag((long) object.getMaximumAir());
});
registerOfflineTag(DurationTag.class, "oxygen", (attribute, object) -> {
if (attribute.startsWith("max", 2)) {
BukkitImplDeprecations.entityMaxOxygenTag.warn(attribute.context);
attribute.fulfill(1);
return new DurationTag((long) object.getMaximumAir());
}
return new DurationTag((long) object.getRemainingAir());
});
// <--[tag]
// @attribute <PlayerTag.health_is_scaled>
// @returns ElementTag(Boolean)
// @description
// Returns whether the player's health bar is currently being scaled.
// -->
registerOfflineTag(ElementTag.class, "health_is_scaled", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().isHealthScaled());
});
// <--[tag]
// @attribute <PlayerTag.health_scale>
// @returns ElementTag(Decimal)
// @mechanism PlayerTag.health_scale
// @description
// Returns the current scale for the player's health bar
// -->
registerOfflineTag(ElementTag.class, "health_scale", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getHealthScale());
});
// Handle EntityTag health tags here to allow getting them when the player is offline
registerOfflineTag(ElementTag.class, "formatted_health", (attribute, object) -> {
Double maxHealth = attribute.hasParam() ? attribute.getDoubleParam() : null;
return EntityHealth.getHealthFormatted(new EntityTag(object.getPlayerEntity()), maxHealth);
});
registerOfflineTag(ElementTag.class, "health_percentage", (attribute, object) -> {
double maxHealth = object.getPlayerEntity().getMaxHealth();
if (attribute.hasParam()) {
maxHealth = attribute.getIntParam();
}
return new ElementTag((object.getPlayerEntity().getHealth() / maxHealth) * 100);
});
registerOfflineTag(ElementTag.class, "health_max", (attribute, object) -> {
return new ElementTag(object.getMaxHealth());
});
registerOfflineTag(ElementTag.class, "health", (attribute, object) -> {
if (attribute.startsWith("is_scaled", 2)) {
attribute.fulfill(1);
BukkitImplDeprecations.entityHealthTags.warn(attribute.context);
return new ElementTag(object.getPlayerEntity().isHealthScaled());
}
if (attribute.startsWith("scale", 2)) {
attribute.fulfill(1);
BukkitImplDeprecations.entityHealthTags.warn(attribute.context);
return new ElementTag(object.getPlayerEntity().getHealthScale());
}
if (attribute.startsWith("formatted", 2)) {
BukkitImplDeprecations.entityHealthTags.warn(attribute.context);
Double maxHealth = attribute.hasContext(2) ? attribute.getDoubleContext(2) : null;
attribute.fulfill(1);
return EntityHealth.getHealthFormatted(new EntityTag(object.getPlayerEntity()), maxHealth);
}
if (attribute.startsWith("percentage", 2)) {
BukkitImplDeprecations.entityHealthTags.warn(attribute.context);
attribute.fulfill(1);
double maxHealth = object.getPlayerEntity().getMaxHealth();
if (attribute.hasParam()) {
maxHealth = attribute.getIntParam();
}
return new ElementTag((object.getPlayerEntity().getHealth() / maxHealth) * 100);
}
if (attribute.startsWith("max", 2)) {
BukkitImplDeprecations.entityHealthTags.warn(attribute.context);
attribute.fulfill(1);
return new ElementTag(object.getMaxHealth());
}
return new ElementTag(object.getHealth());
});
// <--[tag]
// @attribute <PlayerTag.is_banned>
// @returns ElementTag(Boolean)
// @description
// Returns whether the player is banned.
// -->
tagProcessor.registerTag(ElementTag.class, "is_banned", (attribute, object) -> {
if (object.getName() == null) {
return new ElementTag(false);
}
BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(object.getName());
if (ban == null) {
return new ElementTag(false);
}
else if (ban.getExpiration() == null) {
return new ElementTag(true);
}
return new ElementTag(ban.getExpiration().after(new Date()));
});
// <--[tag]
// @attribute <PlayerTag.is_online>
// @returns ElementTag(Boolean)
// @description
// Returns whether the player is currently online.
// Works with offline players (returns false in that case).
// -->
tagProcessor.registerTag(ElementTag.class, "is_online", (attribute, object) -> {
return new ElementTag(object.isOnline());
});
// <--[tag]
// @attribute <PlayerTag.is_op>
// @returns ElementTag(Boolean)
// @mechanism PlayerTag.is_op
// @description
// Returns whether the player is a full server operator.
// Works with offline players.
// -->
tagProcessor.registerTag(ElementTag.class, "is_op", (attribute, object) -> {
return new ElementTag(object.getOfflinePlayer().isOp());
});
// <--[tag]