-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathCraftPlayer.java
More file actions
3370 lines (2827 loc) · 141 KB
/
CraftPlayer.java
File metadata and controls
3370 lines (2827 loc) · 141 KB
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 org.bukkit.craftbukkit.entity;
import com.destroystokyo.paper.event.player.PlayerSetSpawnEvent;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.BaseEncoding;
import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Pair;
import com.mojang.logging.LogUtils;
import io.papermc.paper.FeatureHooks;
import io.papermc.paper.connection.PlayerGameConnection;
import io.papermc.paper.connection.PluginMessageBridgeImpl;
import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.item.WrittenBookContent;
import io.papermc.paper.dialog.Dialog;
import io.papermc.paper.dialog.PaperDialog;
import io.papermc.paper.entity.LookAnchor;
import io.papermc.paper.entity.PaperPlayerGiveResult;
import io.papermc.paper.entity.PlayerGiveResult;
import io.papermc.paper.math.Position;
import io.papermc.paper.util.MCUtil;
import it.unimi.dsi.fastutil.shorts.ShortArraySet;
import it.unimi.dsi.fastutil.shorts.ShortSet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import net.kyori.adventure.dialog.DialogLike;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.pointer.PointersSupplier;
import net.kyori.adventure.util.TriState;
import net.md_5.bungee.api.chat.BaseComponent;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.SectionPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.network.chat.ResolutionContext;
import net.minecraft.network.protocol.common.ClientboundClearDialogPacket;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.ClientboundResourcePackPopPacket;
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket;
import net.minecraft.network.protocol.common.ClientboundServerLinksPacket;
import net.minecraft.network.protocol.common.custom.DiscardedPayload;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
import net.minecraft.network.protocol.game.ClientboundClearTitlesPacket;
import net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket;
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
import net.minecraft.network.protocol.game.ClientboundHurtAnimationPacket;
import net.minecraft.network.protocol.game.ClientboundLevelEventPacket;
import net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket;
import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket;
import net.minecraft.network.protocol.game.ClientboundOpenBookPacket;
import net.minecraft.network.protocol.game.ClientboundOpenSignEditorPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket;
import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundSetBorderCenterPacket;
import net.minecraft.network.protocol.game.ClientboundSetBorderLerpSizePacket;
import net.minecraft.network.protocol.game.ClientboundSetBorderSizePacket;
import net.minecraft.network.protocol.game.ClientboundSetBorderWarningDelayPacket;
import net.minecraft.network.protocol.game.ClientboundSetBorderWarningDistancePacket;
import net.minecraft.network.protocol.game.ClientboundSetDefaultSpawnPositionPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.protocol.game.ClientboundSetExperiencePacket;
import net.minecraft.network.protocol.game.ClientboundSetHealthPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerInventoryPacket;
import net.minecraft.network.protocol.game.ClientboundSetSubtitleTextPacket;
import net.minecraft.network.protocol.game.ClientboundSetTitleTextPacket;
import net.minecraft.network.protocol.game.ClientboundSetTitlesAnimationPacket;
import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
import net.minecraft.network.protocol.game.ClientboundTabListPacket;
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
import net.minecraft.resources.Identifier;
import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.server.permissions.LevelBasedPermissionSet;
import net.minecraft.server.permissions.PermissionLevel;
import net.minecraft.server.players.UserWhiteListEntry;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.util.ProblemReporter;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.food.FoodData;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SignText;
import net.minecraft.world.level.border.BorderChangeListener;
import net.minecraft.world.level.saveddata.maps.MapDecoration;
import net.minecraft.world.level.saveddata.maps.MapId;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.level.storage.LevelData;
import net.minecraft.world.level.storage.TagValueInput;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import org.bukkit.BanEntry;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Effect;
import org.bukkit.EntityEffect;
import org.bukkit.GameMode;
import org.bukkit.Input;
import org.bukkit.Instrument;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Note;
import org.bukkit.OfflinePlayer;
import org.bukkit.Particle;
import org.bukkit.ServerLinks;
import org.bukkit.Sound;
import org.bukkit.Statistic;
import org.bukkit.WeatherType;
import org.bukkit.WorldBorder;
import org.bukkit.ban.IpBanList;
import org.bukkit.ban.ProfileBanList;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.TileState;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.sign.Side;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ManuallyAbandonedConversationCanceller;
import org.bukkit.craftbukkit.CraftEffect;
import org.bukkit.craftbukkit.CraftEquipmentSlot;
import org.bukkit.craftbukkit.CraftInput;
import org.bukkit.craftbukkit.CraftOfflinePlayer;
import org.bukkit.craftbukkit.CraftParticle;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftServerLinks;
import org.bukkit.craftbukkit.CraftSound;
import org.bukkit.craftbukkit.CraftStatistic;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.CraftWorldBorder;
import org.bukkit.craftbukkit.advancement.CraftAdvancement;
import org.bukkit.craftbukkit.advancement.CraftAdvancementProgress;
import org.bukkit.craftbukkit.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.craftbukkit.block.CraftSign;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.bukkit.craftbukkit.conversations.ConversationTracker;
import org.bukkit.craftbukkit.event.CraftEventFactory;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.inventory.CraftRecipe;
import org.bukkit.craftbukkit.map.CraftMapCursor;
import org.bukkit.craftbukkit.map.CraftMapView;
import org.bukkit.craftbukkit.map.RenderData;
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
import org.bukkit.craftbukkit.potion.CraftPotionUtil;
import org.bukkit.craftbukkit.scoreboard.CraftScoreboard;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.EnderPearl;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent;
import org.bukkit.event.player.PlayerHideEntityEvent;
import org.bukkit.event.player.PlayerShowEntityEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.InventoryView.Property;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ItemType;
import org.bukkit.map.MapCursor;
import org.bukkit.map.MapView;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
@DelegateDeserialization(CraftOfflinePlayer.class)
public class CraftPlayer extends CraftHumanEntity implements Player, PluginMessageBridgeImpl {
private static final org.slf4j.Logger LOGGER = LogUtils.getClassLogger();
private static final PointersSupplier<Player> POINTERS_SUPPLIER = PointersSupplier.<Player>builder()
.parent(CraftEntity.POINTERS_SUPPLIER)
.resolving(Identity.NAME, Player::getName)
.resolving(Identity.DISPLAY_NAME, Player::displayName)
.resolving(Identity.LOCALE, Player::locale)
.build();
private long firstPlayed = 0;
private long lastPlayed = 0;
private boolean hasPlayedBefore = false;
private final ConversationTracker conversationTracker = new ConversationTracker();
private final Map<UUID, Set<WeakReference<Plugin>>> invertedVisibilityEntities = new HashMap<>();
private final Set<UUID> unlistedEntities = new HashSet<>(); // Paper - Add Listing API for Player
private static final WeakHashMap<Plugin, WeakReference<Plugin>> pluginWeakReferences = new WeakHashMap<>();
private int hash = 0;
private double health = 20;
private boolean scaledHealth = false;
private double healthScale = 20;
private CraftWorldBorder clientWorldBorder = null;
private BorderChangeListener clientWorldBorderListener = this.createWorldBorderListener();
private long lastSaveTime; // Paper - getLastPlayed replacement API
private @Nullable CraftScoreboard scoreboardOverride;
public CraftPlayer(CraftServer server, ServerPlayer entity) {
super(server, entity);
this.firstPlayed = System.currentTimeMillis();
}
@Override
public ServerPlayer getHandle() {
return (ServerPlayer) this.entity;
}
@Override
public boolean equals(Object obj) {
// Long-term, this should just use the super equals... for now, check the UUID
if (obj == this) return true;
if (!(obj instanceof OfflinePlayer other)) return false;
return this.getUniqueId().equals(other.getUniqueId());
}
@Override
public int hashCode() {
if (this.hash == 0 || this.hash == 485) {
this.hash = 97 * 5 + this.getUniqueId().hashCode();
}
return this.hash;
}
public GameProfile getProfile() {
return this.getHandle().getGameProfile();
}
@Override
public void remove() {
if (this.getHandle().getClass().equals(ServerPlayer.class)) { // special case for NMS plugins inheriting
// Will lead to an inconsistent player state if we remove the player as any other entity.
throw new UnsupportedOperationException(String.format("Cannot remove player %s, use Player#kickPlayer(String) instead.", this.getName()));
} else {
super.remove();
}
}
@Override
public boolean isOp() {
return this.server.getHandle().isOp(this.getHandle().nameAndId());
}
@Override
public void setOp(boolean value) {
if (value == this.isOp()) return;
if (value) {
this.server.getHandle().op(this.getHandle().nameAndId());
} else {
this.server.getHandle().deop(this.getHandle().nameAndId());
}
this.perm.recalculatePermissions();
}
@Override
public boolean isOnline() {
return this.server.getPlayer(this.getUniqueId()) != null;
}
// Paper start
@Override
public boolean isConnected() {
return !this.getHandle().hasDisconnected();
}
// Paper end
@Override
public InetSocketAddress getAddress() {
if (this.getHandle().connection == null) return null;
SocketAddress addr = this.getHandle().connection.getRemoteAddress();
if (addr instanceof InetSocketAddress) {
return (InetSocketAddress) addr;
} else {
return null;
}
}
// Paper start - Add API to get player's proxy address
@Override
public @Nullable InetSocketAddress getHAProxyAddress() {
if (this.getHandle().connection == null) return null;
return this.getHandle().connection.connection.haProxyAddress instanceof final InetSocketAddress inetSocketAddress ? inetSocketAddress : null;
}
// Paper end - Add API to get player's proxy address
@Override
public boolean isTransferred() {
return this.getHandle().connection.playerGameConnection.isTransferred();
}
@Override
public CompletableFuture<byte @Nullable []> retrieveCookie(final NamespacedKey key) {
return this.getHandle().connection.playerGameConnection.retrieveCookie(key);
}
@Override
public void storeCookie(NamespacedKey key, byte[] value) {
this.getHandle().connection.playerGameConnection.storeCookie(key, value);
}
@Override
public void transfer(String host, int port) {
this.getHandle().connection.playerGameConnection.transfer(host, port);
}
// Paper start - Implement NetworkClient
@Override
public int getProtocolVersion() {
if (this.getHandle().connection == null) return -1;
return this.getHandle().connection.connection.protocolVersion;
}
@Override
public InetSocketAddress getVirtualHost() {
if (this.getHandle().connection == null) return null;
return this.getHandle().connection.connection.virtualHost;
}
// Paper end
@Override
public double getEyeHeight(boolean ignorePose) {
if (ignorePose) {
return 1.62;
} else {
return this.getEyeHeight();
}
}
@Override
public void sendRawMessage(String message) {
this.sendRawMessage(null, message);
}
@Override
public void sendRawMessage(UUID sender, String message) {
Preconditions.checkArgument(message != null, "message cannot be null");
if (this.getHandle().connection == null) return;
for (Component component : CraftChatMessage.fromString(message)) {
this.getHandle().sendSystemMessage(component);
}
}
@Override
public void sendMessage(String message) {
if (!this.conversationTracker.isConversingModaly()) {
this.sendRawMessage(message);
}
}
@Override
public void sendMessage(String... messages) {
for (String message : messages) {
this.sendMessage(message);
}
}
@Override
public void sendMessage(UUID sender, String message) {
if (!this.conversationTracker.isConversingModaly()) {
this.sendRawMessage(sender, message);
}
}
@Override
public void sendMessage(UUID sender, String... messages) {
for (String message : messages) {
this.sendMessage(sender, message);
}
}
// Paper start
@Override
@Deprecated
public void sendActionBar(BaseComponent[] message) {
if (getHandle().connection == null || message == null) return;
net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket packet = new net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket(org.bukkit.craftbukkit.util.CraftChatMessage.bungeeToVanilla(message));
getHandle().connection.send(packet);
}
@Override
@Deprecated
public void sendActionBar(String message) {
if (getHandle().connection == null || message == null || message.isEmpty()) return;
getHandle().connection.send(new net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket(CraftChatMessage.fromStringOrNull(message)));
}
@Override
@Deprecated
public void sendActionBar(char alternateChar, String message) {
if (message == null || message.isEmpty()) return;
sendActionBar(org.bukkit.ChatColor.translateAlternateColorCodes(alternateChar, message));
}
@Override
public void setPlayerListHeaderFooter(BaseComponent @Nullable [] header, BaseComponent @Nullable [] footer) {
if (header != null) {
String headerJson = CraftChatMessage.bungeeToJson(header);
playerListHeader = net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson().deserialize(headerJson);
} else {
playerListHeader = null;
}
if (footer != null) {
String footerJson = CraftChatMessage.bungeeToJson(footer);
playerListFooter = net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson().deserialize(footerJson);
} else {
playerListFooter = null;
}
updatePlayerListHeaderFooter();
}
@Override
public void setPlayerListHeaderFooter(@Nullable BaseComponent header, @Nullable BaseComponent footer) {
this.setPlayerListHeaderFooter(header == null ? null : new BaseComponent[]{header},
footer == null ? null : new BaseComponent[]{footer});
}
@Override
public void setTitleTimes(int fadeInTicks, int stayTicks, int fadeOutTicks) {
getHandle().connection.send(new ClientboundSetTitlesAnimationPacket(fadeInTicks, stayTicks, fadeOutTicks));
}
@Override
public void setSubtitle(BaseComponent[] subtitle) {
final ClientboundSetSubtitleTextPacket packet = new ClientboundSetSubtitleTextPacket(org.bukkit.craftbukkit.util.CraftChatMessage.bungeeToVanilla(subtitle));
getHandle().connection.send(packet);
}
@Override
public void setSubtitle(BaseComponent subtitle) {
setSubtitle(new BaseComponent[]{subtitle});
}
@Override
public void showTitle(BaseComponent[] title) {
final ClientboundSetTitleTextPacket packet = new ClientboundSetTitleTextPacket(org.bukkit.craftbukkit.util.CraftChatMessage.bungeeToVanilla(title));
getHandle().connection.send(packet);
}
@Override
public void showTitle(BaseComponent title) {
showTitle(new BaseComponent[]{title});
}
@Override
public void showTitle(BaseComponent[] title, BaseComponent[] subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {
setTitleTimes(fadeInTicks, stayTicks, fadeOutTicks);
setSubtitle(subtitle);
showTitle(title);
}
@Override
public void showTitle(BaseComponent title, BaseComponent subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {
setTitleTimes(fadeInTicks, stayTicks, fadeOutTicks);
setSubtitle(subtitle);
showTitle(title);
}
@Override
public void sendTitle(com.destroystokyo.paper.Title title) {
Preconditions.checkNotNull(title, "Title is null");
setTitleTimes(title.getFadeIn(), title.getStay(), title.getFadeOut());
setSubtitle(title.getSubtitle() == null ? new BaseComponent[0] : title.getSubtitle());
showTitle(title.getTitle());
}
@Override
public void updateTitle(com.destroystokyo.paper.Title title) {
Preconditions.checkNotNull(title, "Title is null");
setTitleTimes(title.getFadeIn(), title.getStay(), title.getFadeOut());
if (title.getSubtitle() != null) {
setSubtitle(title.getSubtitle());
}
showTitle(title.getTitle());
}
@Override
public void hideTitle() {
getHandle().connection.send(new ClientboundClearTitlesPacket(false));
}
// Paper end
@Override
public String getDisplayName() {
if (true) return io.papermc.paper.adventure.DisplayNames.getLegacy(this); // Paper
return this.getHandle().displayName;
}
@Override
public void setDisplayName(final String name) {
this.getHandle().adventure$displayName = name != null ? net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(name) : net.kyori.adventure.text.Component.text(this.getName()); // Paper
this.getHandle().displayName = name == null ? this.getName() : name;
}
// Paper start
@Override
public void playerListName(net.kyori.adventure.text.Component name) {
getHandle().listName = name == null ? null : io.papermc.paper.adventure.PaperAdventure.asVanilla(name);
if (getHandle().connection == null) return; // Updates are possible before the player has fully joined
for (ServerPlayer player : server.getHandle().players) {
if (player.getBukkitEntity().canSee(this)) {
player.connection.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME, getHandle()));
}
}
}
@Override
public net.kyori.adventure.text.Component playerListName() {
return getHandle().listName == null ? net.kyori.adventure.text.Component.text(getName()) : io.papermc.paper.adventure.PaperAdventure.asAdventure(getHandle().listName);
}
@Override
public net.kyori.adventure.text.Component playerListHeader() {
return playerListHeader;
}
@Override
public net.kyori.adventure.text.Component playerListFooter() {
return playerListFooter;
}
// Paper end
@Override
public String getPlayerListName() {
return this.getHandle().listName == null ? this.getName() : CraftChatMessage.fromComponent(this.getHandle().listName);
}
@Override
public void setPlayerListName(String name) {
if (name == null) {
name = this.getName();
}
this.getHandle().listName = name.equals(this.getName()) ? null : CraftChatMessage.fromStringOrNull(name);
if (this.getHandle().connection == null) return; // Paper - Updates are possible before the player has fully joined
for (ServerPlayer player : this.server.getHandle().players) {
if (player.getBukkitEntity().canSee(this)) {
player.connection.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME, this.getHandle()));
}
}
}
@Override
public int getPlayerListOrder() {
return this.getHandle().listOrder;
}
@Override
public void setPlayerListOrder(int order) {
Preconditions.checkArgument(order >= 0, "order cannot be negative");
this.getHandle().listOrder = order;
// Paper start - Send update packet
if (getHandle().connection == null) return; // Updates are possible before the player has fully joined
for (ServerPlayer player : server.getHandle().players) {
if (player.getBukkitEntity().canSee(this)) {
player.connection.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LIST_ORDER, getHandle()));
}
}
// Paper end - Send update packet
}
private net.kyori.adventure.text.Component playerListHeader; // Paper - Adventure
private net.kyori.adventure.text.Component playerListFooter; // Paper - Adventure
@Override
public String getPlayerListHeader() {
return (this.playerListHeader == null) ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serialize(this.playerListHeader);
}
@Override
public String getPlayerListFooter() {
return (this.playerListFooter == null) ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().serialize(this.playerListFooter); // Paper - Adventure
}
@Override
public void setPlayerListHeader(String header) {
this.playerListHeader = header == null ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(header); // Paper - Adventure
this.updatePlayerListHeaderFooter();
}
@Override
public void setPlayerListFooter(String footer) {
this.playerListFooter = footer == null ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(footer); // Paper - Adventure
this.updatePlayerListHeaderFooter();
}
@Override
public void setPlayerListHeaderFooter(String header, String footer) {
this.playerListHeader = header == null ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(header); // Paper - Adventure
this.playerListFooter = footer == null ? null : net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(footer); // Paper - Adventure
this.updatePlayerListHeaderFooter();
}
private void updatePlayerListHeaderFooter() {
if (this.getHandle().connection == null) return;
ClientboundTabListPacket packet = new ClientboundTabListPacket(io.papermc.paper.adventure.PaperAdventure.asVanillaNullToEmpty(this.playerListHeader), io.papermc.paper.adventure.PaperAdventure.asVanillaNullToEmpty(this.playerListFooter)); // Paper - adventure
this.getHandle().connection.send(packet);
}
@Override
public void kickPlayer(String message) {
org.spigotmc.AsyncCatcher.catchOp("player kick"); // Spigot
this.getHandle().connection.disconnect(CraftChatMessage.fromStringOrEmpty(message, true), org.bukkit.event.player.PlayerKickEvent.Cause.PLUGIN); // Paper - kick event cause
}
@Override
public void kick(net.kyori.adventure.text.Component message, org.bukkit.event.player.PlayerKickEvent.Cause cause) {
org.spigotmc.AsyncCatcher.catchOp("player kick");
final ServerGamePacketListenerImpl connection = this.getHandle().connection;
if (connection != null) {
connection.disconnect(message == null ? net.kyori.adventure.text.Component.empty() : message, cause);
}
}
@Override
public <T> T getClientOption(com.destroystokyo.paper.ClientOption<T> type) {
if (com.destroystokyo.paper.ClientOption.SKIN_PARTS == type) {
return type.getType().cast(new com.destroystokyo.paper.PaperSkinParts(this.getHandle().getEntityData().get(net.minecraft.world.entity.player.Player.DATA_PLAYER_MODE_CUSTOMISATION)));
} else if (com.destroystokyo.paper.ClientOption.CHAT_COLORS_ENABLED == type) {
return type.getType().cast(this.getHandle().canChatInColor());
} else if (com.destroystokyo.paper.ClientOption.CHAT_VISIBILITY == type) {
return type.getType().cast(com.destroystokyo.paper.ClientOption.ChatVisibility.valueOf(this.getHandle().getChatVisibility().name()));
} else if (com.destroystokyo.paper.ClientOption.LOCALE == type) {
return type.getType().cast(this.getLocale());
} else if (com.destroystokyo.paper.ClientOption.MAIN_HAND == type) {
return type.getType().cast(this.getMainHand());
} else if (com.destroystokyo.paper.ClientOption.VIEW_DISTANCE == type) {
return type.getType().cast(this.getClientViewDistance());
} else if (com.destroystokyo.paper.ClientOption.TEXT_FILTERING_ENABLED == type) {
return type.getType().cast(this.getHandle().isTextFilteringEnabled());
} else if (com.destroystokyo.paper.ClientOption.ALLOW_SERVER_LISTINGS == type) {
return type.getType().cast(this.getHandle().allowsListing());
} else if (com.destroystokyo.paper.ClientOption.PARTICLE_VISIBILITY == type) {
return type.getType().cast(com.destroystokyo.paper.ClientOption.ParticleVisibility.valueOf(this.getHandle().particleStatus.name()));
}
throw new RuntimeException("Unknown settings type");
}
@Override
public void sendOpLevel(byte level) {
Preconditions.checkArgument(level >= PermissionLevel.ALL.id() && level <= PermissionLevel.OWNERS.id(), "Level must be within [%s, %s]", PermissionLevel.ALL.id(), PermissionLevel.OWNERS.id());
this.server.getServer().getPlayerList().sendPlayerPermissionLevel(this.getHandle(), LevelBasedPermissionSet.forLevel(PermissionLevel.byId(level)), false);
}
@Override
public void addAdditionalChatCompletions(@NonNull Collection<String> completions) {
this.getHandle().connection.send(new net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket(
net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket.Action.ADD,
new ArrayList<>(completions)
));
}
@Override
public void removeAdditionalChatCompletions(@NonNull Collection<String> completions) {
this.getHandle().connection.send(new net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket(
net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket.Action.REMOVE,
new ArrayList<>(completions)
));
}
@Override
public void setCompassTarget(Location loc) {
Preconditions.checkArgument(loc != null, "Location cannot be null");
if (this.getHandle().connection == null) return;
// Do not directly assign here, from the packethandler we'll assign it.
this.getHandle().connection.send(new ClientboundSetDefaultSpawnPositionPacket(
LevelData.RespawnData.of(
((CraftWorld) loc.getWorld()).getHandle().dimension(),
CraftLocation.toBlockPos(loc),
loc.getYaw(),
loc.getPitch()
)
));
}
@Override
public Location getCompassTarget() {
return this.getHandle().compassTarget;
}
@Override
public void chat(String msg) {
Preconditions.checkArgument(msg != null, "msg cannot be null");
if (this.getHandle().connection == null) return;
// Paper start - Improve chat handling
if (ServerGamePacketListenerImpl.isChatMessageIllegal(msg)) {
this.getHandle().connection.disconnect(Component.translatable("multiplayer.disconnect.illegal_characters"), org.bukkit.event.player.PlayerKickEvent.Cause.ILLEGAL_CHARACTERS); // Paper - kick event causes
} else {
if (msg.startsWith("/")) {
this.getHandle().connection.handleCommand(msg);
} else {
final PlayerChatMessage playerChatMessage = PlayerChatMessage.system(msg).withUnsignedContent(Component.literal(msg));
// TODO chat decorating
// TODO text filtering
this.getHandle().connection.chat(msg, playerChatMessage, false);
}
}
// Paper end - Improve chat handling
}
@Override
public boolean performCommand(String command) {
Preconditions.checkArgument(command != null, "command cannot be null");
return this.server.dispatchCommand(this, command);
}
@Override
public void playNote(Location loc, Instrument instrument, Note note) {
Preconditions.checkArgument(loc != null, "Location cannot be null");
Preconditions.checkArgument(instrument != null, "Instrument cannot be null");
Preconditions.checkArgument(note != null, "Note cannot be null");
if (this.getHandle().connection == null) return;
Sound instrumentSound = instrument.getSound();
if (instrumentSound == null) return;
// Paper start - use correct pitch (modeled off of NoteBlock)
final net.minecraft.world.level.block.state.properties.NoteBlockInstrument noteBlockInstrument = CraftBlockData.toVanilla(instrument, net.minecraft.world.level.block.state.properties.NoteBlockInstrument.class);
final float pitch = noteBlockInstrument.isTunable() ? note.getPitch() : 1.0f;
// Paper end
this.getHandle().connection.send(new ClientboundSoundPacket(CraftSound.bukkitToMinecraftHolder(instrumentSound), net.minecraft.sounds.SoundSource.RECORDS, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 3.0f, pitch, this.getHandle().getRandom().nextLong()));
}
@Override
public void playSound(Location loc, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch) {
this.playSound(loc, sound, category, volume, pitch, this.getHandle().random.nextLong());
}
@Override
public void playSound(Location loc, String sound, org.bukkit.SoundCategory category, float volume, float pitch) {
this.playSound(loc, sound, category, volume, pitch, this.getHandle().random.nextLong());
}
@Override
public void playSound(Location loc, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (loc == null || sound == null || category == null || this.getHandle().connection == null) return;
this.playSound0(loc, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundSource.valueOf(category.name()), volume, pitch, seed);
}
@Override
public void playSound(Location loc, String sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (loc == null || sound == null || category == null || this.getHandle().connection == null) return;
this.playSound0(loc, Holder.direct(SoundEvent.createVariableRangeEvent(Identifier.parse(sound))), net.minecraft.sounds.SoundSource.valueOf(category.name()), volume, pitch, seed);
}
private void playSound0(Location loc, Holder<SoundEvent> soundEffectHolder, net.minecraft.sounds.SoundSource categoryNMS, float volume, float pitch, long seed) {
Preconditions.checkArgument(loc != null, "Location cannot be null");
if (this.getHandle().connection == null) return;
ClientboundSoundPacket packet = new ClientboundSoundPacket(soundEffectHolder, categoryNMS, loc.getX(), loc.getY(), loc.getZ(), volume, pitch, seed);
this.getHandle().connection.send(packet);
}
@Override
public void playSound(org.bukkit.entity.Entity entity, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch) {
this.playSound(entity, sound, category, volume, pitch, this.getHandle().random.nextLong());
}
@Override
public void playSound(org.bukkit.entity.Entity entity, String sound, org.bukkit.SoundCategory category, float volume, float pitch) {
this.playSound(entity, sound, category, volume, pitch, this.getHandle().random.nextLong());
}
@Override
public void playSound(org.bukkit.entity.Entity entity, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (!(entity instanceof CraftEntity) || sound == null || category == null || this.getHandle().connection == null) return;
this.playSound0(entity, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundSource.valueOf(category.name()), volume, pitch, seed);
}
@Override
public void playSound(org.bukkit.entity.Entity entity, String sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (!(entity instanceof CraftEntity) || sound == null || category == null || this.getHandle().connection == null) return;
this.playSound0(entity, Holder.direct(SoundEvent.createVariableRangeEvent(Identifier.parse(sound))), net.minecraft.sounds.SoundSource.valueOf(category.name()), volume, pitch, seed);
}
private void playSound0(org.bukkit.entity.Entity entity, Holder<SoundEvent> soundEffectHolder, net.minecraft.sounds.SoundSource categoryNMS, float volume, float pitch, long seed) {
Preconditions.checkArgument(entity != null, "Entity cannot be null");
Preconditions.checkArgument(soundEffectHolder != null, "Holder of SoundEffect cannot be null");
Preconditions.checkArgument(categoryNMS != null, "SoundCategory cannot be null");
if (this.getHandle().connection == null) return;
if (!(entity instanceof CraftEntity craftEntity)) return;
ClientboundSoundEntityPacket packet = new ClientboundSoundEntityPacket(soundEffectHolder, categoryNMS, craftEntity.getHandle(), volume, pitch, seed);
this.getHandle().connection.send(packet);
}
@Override
public void stopSound(String sound, org.bukkit.SoundCategory category) {
if (this.getHandle().connection == null) return;
this.getHandle().connection.send(new ClientboundStopSoundPacket(Identifier.parse(sound), category == null ? net.minecraft.sounds.SoundSource.MASTER : net.minecraft.sounds.SoundSource.valueOf(category.name())));
}
@Override
public void stopSound(org.bukkit.SoundCategory category) {
if (this.getHandle().connection == null) return;
this.getHandle().connection.send(new ClientboundStopSoundPacket(null, net.minecraft.sounds.SoundSource.valueOf(category.name())));
}
@Override
public void stopAllSounds() {
if (this.getHandle().connection == null) return;
this.getHandle().connection.send(new ClientboundStopSoundPacket(null, null));
}
@Override
public void playEffect(Location loc, Effect effect, int data) {
Preconditions.checkArgument(effect != null, "Effect cannot be null");
Preconditions.checkArgument(loc != null, "Location cannot be null");
if (this.getHandle().connection == null) return;
int packetData = effect.getId();
ClientboundLevelEventPacket packet = new ClientboundLevelEventPacket(packetData, CraftLocation.toBlockPos(loc), data, false);
this.getHandle().connection.send(packet);
}
@Override
public <T> void playEffect(Location loc, Effect effect, T data) {
Preconditions.checkArgument(effect != null, "Effect cannot be null");
if (data != null) {
Preconditions.checkArgument(effect.getData() != null, "Effect.%s does not have a valid Data", effect);
Preconditions.checkArgument(effect.isApplicable(data), "%s data cannot be used for the %s effect", data.getClass().getName(), effect); // Paper
} else {
// Special case: the axis is optional for ELECTRIC_SPARK
Preconditions.checkArgument(effect.getData() == null || effect == Effect.ELECTRIC_SPARK, "Wrong kind of data for the %s effect", effect);
}
int datavalue = CraftEffect.getDataValue(effect, data);
this.playEffect(loc, effect, datavalue);
}
@Override
public boolean breakBlock(Block block) {
Preconditions.checkArgument(block != null, "Block cannot be null");
Preconditions.checkArgument(block.getWorld().equals(this.getWorld()), "Cannot break blocks across worlds");
return this.getHandle().gameMode.destroyBlock(new BlockPos(block.getX(), block.getY(), block.getZ()));
}
@Override
public void sendBlockChange(Location loc, Material material, byte data) {
if (this.getHandle().connection == null) return;
ClientboundBlockUpdatePacket packet = new ClientboundBlockUpdatePacket(CraftLocation.toBlockPos(loc), CraftMagicNumbers.getBlock(material, data));
this.getHandle().connection.send(packet);
}
@Override
public void sendBlockChange(Location loc, BlockData block) {
if (this.getHandle().connection == null) return;
ClientboundBlockUpdatePacket packet = new ClientboundBlockUpdatePacket(CraftLocation.toBlockPos(loc), ((CraftBlockData) block).getState());
this.getHandle().connection.send(packet);
}
@Override
public void sendMultiBlockChange(final Map<? extends io.papermc.paper.math.Position, BlockData> blockChanges) {
if (this.getHandle().connection == null) return;
Map<SectionPos, it.unimi.dsi.fastutil.shorts.Short2ObjectMap<net.minecraft.world.level.block.state.BlockState>> sectionMap = new HashMap<>();
for (Map.Entry<? extends io.papermc.paper.math.Position, BlockData> entry : blockChanges.entrySet()) {
BlockData blockData = entry.getValue();
BlockPos blockPos = io.papermc.paper.util.MCUtil.toBlockPos(entry.getKey());
SectionPos sectionPos = SectionPos.of(blockPos);
it.unimi.dsi.fastutil.shorts.Short2ObjectMap<net.minecraft.world.level.block.state.BlockState> sectionData = sectionMap.computeIfAbsent(sectionPos, key -> new it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap<>());
sectionData.put(SectionPos.sectionRelativePos(blockPos), ((CraftBlockData) blockData).getState());
}
for (Map.Entry<SectionPos, it.unimi.dsi.fastutil.shorts.Short2ObjectMap<net.minecraft.world.level.block.state.BlockState>> entry : sectionMap.entrySet()) {
SectionPos sectionPos = entry.getKey();
it.unimi.dsi.fastutil.shorts.Short2ObjectMap<net.minecraft.world.level.block.state.BlockState> blockData = entry.getValue();
net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket packet = new net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket(sectionPos, blockData);
this.getHandle().connection.send(packet);
}
}
@Override
public void sendBlockChanges(Collection<BlockState> blocks) {
Preconditions.checkArgument(blocks != null, "blocks must not be null");
if (this.getHandle().connection == null || blocks.isEmpty()) {
return;
}
Map<SectionPos, ChunkSectionChanges> changes = new HashMap<>();
for (BlockState state : blocks) {
CraftBlockState cstate = (CraftBlockState) state;
BlockPos pos = cstate.getPosition();
// The coordinates of the chunk section in which the block is located, aka chunk x, y, and z
SectionPos sectionPosition = SectionPos.of(pos);
// Push the block change position and block data to the final change map
ChunkSectionChanges sectionChanges = changes.computeIfAbsent(sectionPosition, (ignore) -> new ChunkSectionChanges());
sectionChanges.positions().add(SectionPos.sectionRelativePos(pos));
sectionChanges.blockData().add(cstate.getHandle());
}
// Construct the packets using the data allocated above and send then to the players
for (Map.Entry<SectionPos, ChunkSectionChanges> entry : changes.entrySet()) {
ChunkSectionChanges chunkChanges = entry.getValue();
ClientboundSectionBlocksUpdatePacket packet = new ClientboundSectionBlocksUpdatePacket(entry.getKey(), chunkChanges.positions(), chunkChanges.blockData().toArray(net.minecraft.world.level.block.state.BlockState[]::new));
this.getHandle().connection.send(packet);
}
}
private record ChunkSectionChanges(ShortSet positions, List<net.minecraft.world.level.block.state.BlockState> blockData) {
public ChunkSectionChanges() {
this(new ShortArraySet(), new ArrayList<>());
}
}
@Override
public void sendBlockDamage(Location loc, float progress, org.bukkit.entity.Entity source) {
Preconditions.checkArgument(source != null, "source must not be null");
this.sendBlockDamage(loc, progress, source.getEntityId());
}
@Override
public void sendBlockDamage(Location loc, float progress, int sourceId) {
Preconditions.checkArgument(loc != null, "loc must not be null");
Preconditions.checkArgument(progress >= 0.0 && progress <= 1.0, "progress must be between 0.0 and 1.0 (inclusive)");
if (this.getHandle().connection == null) return;