-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathPlayer.java
5133 lines (4279 loc) · 201 KB
/
Player.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 cn.nukkit;
import cn.nukkit.AdventureSettings.Type;
import cn.nukkit.block.*;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntityItemFrame;
import cn.nukkit.blockentity.BlockEntitySpawnable;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandDataVersions;
import cn.nukkit.entity.*;
import cn.nukkit.entity.data.*;
import cn.nukkit.entity.item.*;
import cn.nukkit.entity.projectile.EntityArrow;
import cn.nukkit.entity.projectile.EntityThrownTrident;
import cn.nukkit.event.entity.EntityDamageByBlockEvent;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.event.entity.EntityDamageEvent.DamageModifier;
import cn.nukkit.event.entity.ProjectileLaunchEvent;
import cn.nukkit.event.inventory.InventoryCloseEvent;
import cn.nukkit.event.inventory.InventoryPickupArrowEvent;
import cn.nukkit.event.inventory.InventoryPickupItemEvent;
import cn.nukkit.event.inventory.InventoryPickupTridentEvent;
import cn.nukkit.event.player.*;
import cn.nukkit.event.player.PlayerAsyncPreLoginEvent.LoginResult;
import cn.nukkit.event.player.PlayerInteractEvent.Action;
import cn.nukkit.event.player.PlayerTeleportEvent.TeleportCause;
import cn.nukkit.event.server.DataPacketReceiveEvent;
import cn.nukkit.event.server.DataPacketSendEvent;
import cn.nukkit.form.window.FormWindow;
import cn.nukkit.form.window.FormWindowCustom;
import cn.nukkit.inventory.*;
import cn.nukkit.inventory.transaction.CraftingTransaction;
import cn.nukkit.inventory.transaction.EnchantTransaction;
import cn.nukkit.inventory.transaction.InventoryTransaction;
import cn.nukkit.inventory.transaction.RepairItemTransaction;
import cn.nukkit.inventory.transaction.action.InventoryAction;
import cn.nukkit.inventory.transaction.data.ReleaseItemData;
import cn.nukkit.inventory.transaction.data.UseItemData;
import cn.nukkit.inventory.transaction.data.UseItemOnEntityData;
import cn.nukkit.item.*;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.lang.TextContainer;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.level.*;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.level.format.generic.BaseFullChunk;
import cn.nukkit.level.particle.PunchBlockParticle;
import cn.nukkit.math.*;
import cn.nukkit.metadata.MetadataValue;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.*;
import cn.nukkit.network.Network;
import cn.nukkit.network.SourceInterface;
import cn.nukkit.network.protocol.*;
import cn.nukkit.network.protocol.types.ContainerIds;
import cn.nukkit.network.protocol.types.NetworkInventoryAction;
import cn.nukkit.permission.PermissibleBase;
import cn.nukkit.permission.Permission;
import cn.nukkit.permission.PermissionAttachment;
import cn.nukkit.permission.PermissionAttachmentInfo;
import cn.nukkit.plugin.Plugin;
import cn.nukkit.potion.Effect;
import cn.nukkit.resourcepacks.ResourcePack;
import cn.nukkit.scheduler.AsyncTask;
import cn.nukkit.utils.*;
import co.aikar.timings.Timing;
import co.aikar.timings.Timings;
import com.google.common.base.Strings;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongIterator;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteOrder;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
/**
* @author MagicDroidX & Box
* Nukkit Project
*/
@Log4j2
public class Player extends EntityHuman implements CommandSender, InventoryHolder, ChunkLoader, IPlayer {
public static final int SURVIVAL = 0;
public static final int CREATIVE = 1;
public static final int ADVENTURE = 2;
public static final int SPECTATOR = 3;
public static final int VIEW = SPECTATOR;
public static final int SURVIVAL_SLOTS = 36;
public static final int CREATIVE_SLOTS = 112;
public static final int CRAFTING_SMALL = 0;
public static final int CRAFTING_BIG = 1;
public static final int CRAFTING_ANVIL = 2;
public static final int CRAFTING_ENCHANT = 3;
public static final int CRAFTING_BEACON = 4;
public static final float DEFAULT_SPEED = 0.1f;
public static final float MAXIMUM_SPEED = 0.5f;
public static final int PERMISSION_CUSTOM = 3;
public static final int PERMISSION_OPERATOR = 2;
public static final int PERMISSION_MEMBER = 1;
public static final int PERMISSION_VISITOR = 0;
public static final int ANVIL_WINDOW_ID = 2;
public static final int ENCHANT_WINDOW_ID = 3;
public static final int BEACON_WINDOW_ID = 4;
protected final SourceInterface interfaz;
public boolean playedBefore;
public boolean spawned = false;
public boolean loggedIn = false;
public boolean locallyInitialized = false;
public int gamemode;
public long lastBreak;
private BlockVector3 lastBreakPosition = new BlockVector3();
protected int windowCnt = 4;
protected final BiMap<Inventory, Integer> windows = HashBiMap.create();
protected final BiMap<Integer, Inventory> windowIndex = windows.inverse();
protected final Set<Integer> permanentWindows = new IntOpenHashSet();
private boolean inventoryOpen;
protected int closingWindowId = Integer.MIN_VALUE;
protected int messageCounter = 2;
private String clientSecret;
public Vector3 speed = null;
public final HashSet<String> achievements = new HashSet<>();
public int craftingType = CRAFTING_SMALL;
protected PlayerUIInventory playerUIInventory;
protected CraftingGrid craftingGrid;
protected CraftingTransaction craftingTransaction;
protected EnchantTransaction enchantTransaction;
protected RepairItemTransaction repairItemTransaction;
public long creationTime = 0;
protected long randomClientId;
protected Vector3 forceMovement = null;
protected Vector3 teleportPosition = null;
protected boolean connected = true;
protected final InetSocketAddress socketAddress;
protected boolean removeFormat = true;
protected String username;
protected String iusername;
protected String displayName;
protected int startAction = -1;
protected Vector3 sleeping = null;
protected Long clientID = null;
private int loaderId;
protected float stepHeight = 0.6f;
public final Map<Long, Boolean> usedChunks = new Long2ObjectOpenHashMap<>();
protected int chunkLoadCount = 0;
protected final Long2ObjectLinkedOpenHashMap<Boolean> loadQueue = new Long2ObjectLinkedOpenHashMap<>();
protected int nextChunkOrderRun = 1;
protected final Map<UUID, Player> hiddenPlayers = new HashMap<>();
protected Vector3 newPosition = null;
protected int chunkRadius;
protected int viewDistance;
protected final int chunksPerTick;
protected final int spawnThreshold;
protected Position spawnPosition = null;
protected int inAirTicks = 0;
protected int startAirTicks = 5;
protected AdventureSettings adventureSettings;
protected boolean checkMovement = true;
private PermissibleBase perm = null;
private int exp = 0;
private int expLevel = 0;
protected PlayerFood foodData = null;
private Entity killer = null;
private final AtomicReference<Locale> locale = new AtomicReference<>(null);
private int hash;
private String buttonText = "Button";
protected boolean enableClientCommand = true;
private BlockEnderChest viewingEnderChest = null;
protected int lastEnderPearl = 20;
protected int lastChorusFruitTeleport = 20;
private LoginChainData loginChainData;
public Block breakingBlock = null;
public int pickedXPOrb = 0;
protected int formWindowCount = 0;
protected Map<Integer, FormWindow> formWindows = new Int2ObjectOpenHashMap<>();
protected Map<Integer, FormWindow> serverSettings = new Int2ObjectOpenHashMap<>();
protected Map<Long, DummyBossBar> dummyBossBars = new Long2ObjectLinkedOpenHashMap<>();
private AsyncTask preLoginEventTask = null;
protected boolean shouldLogin = false;
public EntityFishingHook fishing = null;
public long lastSkinChange;
protected double lastRightClickTime = 0.0;
protected Vector3 lastRightClickPos = null;
private int timeSinceRest;
public int getStartActionTick() {
return startAction;
}
public void startAction() {
this.startAction = this.server.getTick();
}
public void stopAction() {
this.startAction = -1;
}
public int getLastEnderPearlThrowingTick() {
return lastEnderPearl;
}
public void onThrowEnderPearl() {
this.lastEnderPearl = this.server.getTick();
}
public int getLastChorusFruitTeleport() {
return lastChorusFruitTeleport;
}
public void onChorusFruitTeleport() {
this.lastChorusFruitTeleport = this.server.getTick();
}
public BlockEnderChest getViewingEnderChest() {
return viewingEnderChest;
}
public void setViewingEnderChest(BlockEnderChest chest) {
if (chest == null && this.viewingEnderChest != null) {
this.viewingEnderChest.getViewers().remove(this);
} else if (chest != null) {
chest.getViewers().add(this);
}
this.viewingEnderChest = chest;
}
public TranslationContainer getLeaveMessage() {
return new TranslationContainer(TextFormat.YELLOW + "%multiplayer.player.left", this.getDisplayName());
}
public String getClientSecret() {
return clientSecret;
}
/**
* This might disappear in the future.
* Please use getUniqueId() instead (IP + clientId + name combo, in the future it'll change to real UUID for online auth)
* @return random client id
*/
@Deprecated
public Long getClientId() {
return randomClientId;
}
@Override
public boolean isBanned() {
return this.server.getNameBans().isBanned(this.getName());
}
@Override
public void setBanned(boolean value) {
if (value) {
this.server.getNameBans().addBan(this.getName(), null, null, null);
this.kick(PlayerKickEvent.Reason.NAME_BANNED, "Banned by admin");
} else {
this.server.getNameBans().remove(this.getName());
}
}
@Override
public boolean isWhitelisted() {
return this.server.isWhitelisted(this.getName().toLowerCase());
}
@Override
public void setWhitelisted(boolean value) {
if (value) {
this.server.addWhitelist(this.getName().toLowerCase());
} else {
this.server.removeWhitelist(this.getName().toLowerCase());
}
}
@Override
public Player getPlayer() {
return this;
}
@Override
public Long getFirstPlayed() {
return this.namedTag != null ? this.namedTag.getLong("firstPlayed") : null;
}
@Override
public Long getLastPlayed() {
return this.namedTag != null ? this.namedTag.getLong("lastPlayed") : null;
}
@Override
public boolean hasPlayedBefore() {
return this.playedBefore;
}
public AdventureSettings getAdventureSettings() {
return adventureSettings;
}
public void setAdventureSettings(AdventureSettings adventureSettings) {
this.adventureSettings = adventureSettings.clone(this);
this.adventureSettings.update();
}
public void resetInAirTicks() {
this.inAirTicks = 0;
}
@Deprecated
public void setAllowFlight(boolean value) {
this.getAdventureSettings().set(Type.ALLOW_FLIGHT, value);
this.getAdventureSettings().update();
}
@Deprecated
public boolean getAllowFlight() {
return this.getAdventureSettings().get(Type.ALLOW_FLIGHT);
}
public void setAllowModifyWorld(boolean value) {
this.getAdventureSettings().set(Type.WORLD_IMMUTABLE, !value);
this.getAdventureSettings().set(Type.BUILD_AND_MINE, value);
this.getAdventureSettings().set(Type.WORLD_BUILDER, value);
this.getAdventureSettings().update();
}
public void setAllowInteract(boolean value) {
setAllowInteract(value, value);
}
public void setAllowInteract(boolean value, boolean containers) {
this.getAdventureSettings().set(Type.WORLD_IMMUTABLE, !value);
this.getAdventureSettings().set(Type.DOORS_AND_SWITCHED, value);
this.getAdventureSettings().set(Type.OPEN_CONTAINERS, containers);
this.getAdventureSettings().update();
}
@Deprecated
public void setAutoJump(boolean value) {
this.getAdventureSettings().set(Type.AUTO_JUMP, value);
this.getAdventureSettings().update();
}
@Deprecated
public boolean hasAutoJump() {
return this.getAdventureSettings().get(Type.AUTO_JUMP);
}
@Override
public void spawnTo(Player player) {
if (this.spawned && player.spawned && this.isAlive() && player.getLevel() == this.level && player.canSee(this) && !this.isSpectator()) {
super.spawnTo(player);
}
}
@Override
public Server getServer() {
return this.server;
}
public boolean getRemoveFormat() {
return removeFormat;
}
public void setRemoveFormat() {
this.setRemoveFormat(true);
}
public void setRemoveFormat(boolean remove) {
this.removeFormat = remove;
}
public boolean canSee(Player player) {
return !this.hiddenPlayers.containsKey(player.getUniqueId());
}
public void hidePlayer(Player player) {
if (this == player) {
return;
}
this.hiddenPlayers.put(player.getUniqueId(), player);
player.despawnFrom(this);
}
public void showPlayer(Player player) {
if (this == player) {
return;
}
this.hiddenPlayers.remove(player.getUniqueId());
if (player.isOnline()) {
player.spawnTo(this);
}
}
@Override
public boolean canCollideWith(Entity entity) {
return false;
}
@Override
public void resetFallDistance() {
super.resetFallDistance();
if (this.inAirTicks != 0) {
this.startAirTicks = 5;
}
this.inAirTicks = 0;
this.highestPosition = this.y;
}
@Override
public boolean isOnline() {
return this.connected && this.loggedIn;
}
@Override
public boolean isOp() {
return this.server.isOp(this.getName());
}
@Override
public void setOp(boolean value) {
if (value == this.isOp()) {
return;
}
if (value) {
this.server.addOp(this.getName());
} else {
this.server.removeOp(this.getName());
}
this.recalculatePermissions();
this.getAdventureSettings().update();
this.sendCommandData();
}
@Override
public boolean isPermissionSet(String name) {
return this.perm.isPermissionSet(name);
}
@Override
public boolean isPermissionSet(Permission permission) {
return this.perm.isPermissionSet(permission);
}
@Override
public boolean hasPermission(String name) {
return this.perm != null && this.perm.hasPermission(name);
}
@Override
public boolean hasPermission(Permission permission) {
return this.perm.hasPermission(permission);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin) {
return this.addAttachment(plugin, null);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name) {
return this.addAttachment(plugin, name, null);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, Boolean value) {
return this.perm.addAttachment(plugin, name, value);
}
@Override
public void removeAttachment(PermissionAttachment attachment) {
this.perm.removeAttachment(attachment);
}
@Override
public void recalculatePermissions() {
this.server.getPluginManager().unsubscribeFromPermission(Server.BROADCAST_CHANNEL_USERS, this);
this.server.getPluginManager().unsubscribeFromPermission(Server.BROADCAST_CHANNEL_ADMINISTRATIVE, this);
if (this.perm == null) {
return;
}
this.perm.recalculatePermissions();
if (this.hasPermission(Server.BROADCAST_CHANNEL_USERS)) {
this.server.getPluginManager().subscribeToPermission(Server.BROADCAST_CHANNEL_USERS, this);
}
if (this.hasPermission(Server.BROADCAST_CHANNEL_ADMINISTRATIVE)) {
this.server.getPluginManager().subscribeToPermission(Server.BROADCAST_CHANNEL_ADMINISTRATIVE, this);
}
if (this.isEnableClientCommand() && spawned) this.sendCommandData();
}
public boolean isEnableClientCommand() {
return this.enableClientCommand;
}
public void setEnableClientCommand(boolean enable) {
this.enableClientCommand = enable;
SetCommandsEnabledPacket pk = new SetCommandsEnabledPacket();
pk.enabled = enable;
this.dataPacket(pk);
if (enable) this.sendCommandData();
}
public void sendCommandData() {
if (!spawned) {
return;
}
AvailableCommandsPacket pk = new AvailableCommandsPacket();
Map<String, CommandDataVersions> data = new HashMap<>();
int count = 0;
for (Command command : this.server.getCommandMap().getCommands().values()) {
if (!command.testPermissionSilent(this)) {
continue;
}
++count;
CommandDataVersions data0 = command.generateCustomCommandData(this);
data.put(command.getName(), data0);
}
if (count > 0) {
//TODO: structure checking
pk.commands = data;
this.dataPacket(pk);
}
}
@Override
public Map<String, PermissionAttachmentInfo> getEffectivePermissions() {
return this.perm.getEffectivePermissions();
}
public Player(SourceInterface interfaz, Long clientID, InetSocketAddress socketAddress) {
super(null, new CompoundTag());
this.interfaz = interfaz;
this.perm = new PermissibleBase(this);
this.server = Server.getInstance();
this.lastBreak = -1;
this.socketAddress = socketAddress;
this.clientID = clientID;
this.loaderId = Level.generateChunkLoaderId(this);
this.chunksPerTick = this.server.getConfig("chunk-sending.per-tick", 4);
this.spawnThreshold = this.server.getConfig("chunk-sending.spawn-threshold", 56);
this.spawnPosition = null;
this.gamemode = this.server.getGamemode();
this.setLevel(this.server.getDefaultLevel());
this.viewDistance = this.server.getViewDistance();
this.chunkRadius = viewDistance;
//this.newPosition = new Vector3(0, 0, 0);
this.boundingBox = new SimpleAxisAlignedBB(0, 0, 0, 0, 0, 0);
this.lastSkinChange = -1;
this.uuid = null;
this.rawUUID = null;
this.creationTime = System.currentTimeMillis();
}
@Override
protected void initEntity() {
super.initEntity();
this.addDefaultWindows();
}
public boolean isPlayer() {
return true;
}
public void removeAchievement(String achievementId) {
achievements.remove(achievementId);
}
public boolean hasAchievement(String achievementId) {
return achievements.contains(achievementId);
}
public boolean isConnected() {
return connected;
}
public String getDisplayName() {
return this.displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
if (this.spawned) {
this.server.updatePlayerListData(this.getUniqueId(), this.getId(), this.getDisplayName(), this.getSkin(), this.getLoginChainData().getXUID());
}
}
@Override
public void setSkin(Skin skin) {
super.setSkin(skin);
if (this.spawned) {
this.server.updatePlayerListData(this.getUniqueId(), this.getId(), this.getDisplayName(), skin, this.getLoginChainData().getXUID());
}
}
public String getAddress() {
return this.socketAddress.getAddress().getHostAddress();
}
public int getPort() {
return this.socketAddress.getPort();
}
public InetSocketAddress getSocketAddress() {
return this.socketAddress;
}
public Position getNextPosition() {
return this.newPosition != null ? new Position(this.newPosition.x, this.newPosition.y, this.newPosition.z, this.level) : this.getPosition();
}
public boolean isSleeping() {
return this.sleeping != null;
}
public int getInAirTicks() {
return this.inAirTicks;
}
/**
* Returns whether the player is currently using an item (right-click and hold).
*
* @return bool
*/
public boolean isUsingItem() {
return this.getDataFlag(DATA_FLAGS, DATA_FLAG_ACTION) && this.startAction > -1;
}
public void setUsingItem(boolean value) {
this.startAction = value ? this.server.getTick() : -1;
this.setDataFlag(DATA_FLAGS, DATA_FLAG_ACTION, value);
}
public String getButtonText() {
return this.buttonText;
}
public void setButtonText(String text) {
this.buttonText = text;
this.setDataProperty(new StringEntityData(Entity.DATA_INTERACTIVE_TAG, this.buttonText));
}
public void unloadChunk(int x, int z) {
this.unloadChunk(x, z, null);
}
public void unloadChunk(int x, int z, Level level) {
level = level == null ? this.level : level;
long index = Level.chunkHash(x, z);
if (this.usedChunks.containsKey(index)) {
for (Entity entity : level.getChunkEntities(x, z).values()) {
if (entity != this) {
entity.despawnFrom(this);
}
}
this.usedChunks.remove(index);
}
level.unregisterChunkLoader(this, x, z);
this.loadQueue.remove(index);
}
public Position getSpawn() {
if (this.spawnPosition != null && this.spawnPosition.getLevel() != null) {
return this.spawnPosition;
} else {
return this.server.getDefaultLevel().getSafeSpawn();
}
}
public void sendChunk(int x, int z, DataPacket packet) {
if (!this.connected) {
return;
}
this.usedChunks.put(Level.chunkHash(x, z), Boolean.TRUE);
this.chunkLoadCount++;
this.dataPacket(packet);
if (this.spawned) {
for (Entity entity : this.level.getChunkEntities(x, z).values()) {
if (this != entity && !entity.closed && entity.isAlive()) {
entity.spawnTo(this);
}
}
}
}
public void sendChunk(int x, int z, int subChunkCount, byte[] payload) {
if (!this.connected) {
return;
}
this.usedChunks.put(Level.chunkHash(x, z), true);
this.chunkLoadCount++;
LevelChunkPacket pk = new LevelChunkPacket();
pk.chunkX = x;
pk.chunkZ = z;
pk.subChunkCount = subChunkCount;
pk.data = payload;
this.dataPacket(pk);
if (this.spawned) {
for (Entity entity : this.level.getChunkEntities(x, z).values()) {
if (this != entity && !entity.closed && entity.isAlive()) {
entity.spawnTo(this);
}
}
}
}
protected void sendNextChunk() {
if (!this.connected) {
return;
}
Timings.playerChunkSendTimer.startTiming();
if (!loadQueue.isEmpty()) {
int count = 0;
ObjectIterator<Long2ObjectMap.Entry<Boolean>> iter = loadQueue.long2ObjectEntrySet().fastIterator();
while (iter.hasNext()) {
Long2ObjectMap.Entry<Boolean> entry = iter.next();
long index = entry.getLongKey();
if (count >= this.chunksPerTick) {
break;
}
int chunkX = Level.getHashX(index);
int chunkZ = Level.getHashZ(index);
++count;
this.usedChunks.put(index, false);
this.level.registerChunkLoader(this, chunkX, chunkZ, false);
if (!this.level.populateChunk(chunkX, chunkZ)) {
if (this.spawned && this.teleportPosition == null) {
continue;
} else {
break;
}
}
iter.remove();
PlayerChunkRequestEvent ev = new PlayerChunkRequestEvent(this, chunkX, chunkZ);
this.server.getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
this.level.requestChunk(chunkX, chunkZ, this);
}
}
}
if (this.chunkLoadCount >= this.spawnThreshold && !this.spawned && this.teleportPosition == null) {
this.doFirstSpawn();
}
Timings.playerChunkSendTimer.stopTiming();
}
protected void doFirstSpawn() {
this.spawned = true;
this.inventory.sendContents(this);
this.inventory.sendArmorContents(this);
this.offhandInventory.sendContents(this);
this.setEnableClientCommand(true);
SetTimePacket setTimePacket = new SetTimePacket();
setTimePacket.time = this.level.getTime();
this.dataPacket(setTimePacket);
Position pos = this.level.getSafeSpawn(this);
PlayerRespawnEvent respawnEvent = new PlayerRespawnEvent(this, pos, true);
this.server.getPluginManager().callEvent(respawnEvent);
pos = respawnEvent.getRespawnPosition();
this.sendPlayStatus(PlayStatusPacket.PLAYER_SPAWN);
PlayerJoinEvent playerJoinEvent = new PlayerJoinEvent(this,
new TranslationContainer(TextFormat.YELLOW + "%multiplayer.player.joined", new String[]{
this.getDisplayName()
})
);
this.server.getPluginManager().callEvent(playerJoinEvent);
if (playerJoinEvent.getJoinMessage().toString().trim().length() > 0) {
this.server.broadcastMessage(playerJoinEvent.getJoinMessage());
}
this.noDamageTicks = 60;
this.getServer().sendRecipeList(this);
for (long index : this.usedChunks.keySet()) {
int chunkX = Level.getHashX(index);
int chunkZ = Level.getHashZ(index);
for (Entity entity : this.level.getChunkEntities(chunkX, chunkZ).values()) {
if (this != entity && !entity.closed && entity.isAlive()) {
entity.spawnTo(this);
}
}
}
int experience = this.getExperience();
if (experience != 0) {
this.sendExperience(experience);
}
int level = this.getExperienceLevel();
if (level != 0) {
this.sendExperienceLevel(this.getExperienceLevel());
}
this.teleport(pos, null); // Prevent PlayerTeleportEvent during player spawn
if (!this.isSpectator()) {
this.spawnToAll();
}
//todo Updater
//Weather
this.getLevel().sendWeather(this);
//FoodLevel
PlayerFood food = this.getFoodData();
if (food.getLevel() != food.getMaxLevel()) {
food.sendFoodLevel();
}
if (this.getHealth() < 1) {
this.respawn();
}
}
protected boolean orderChunks() {
if (!this.connected) {
return false;
}
Timings.playerChunkOrderTimer.startTiming();
this.nextChunkOrderRun = 200;
loadQueue.clear();
Long2ObjectOpenHashMap<Boolean> lastChunk = new Long2ObjectOpenHashMap<>(this.usedChunks);
int centerX = (int) this.x >> 4;
int centerZ = (int) this.z >> 4;
int radius = spawned ? this.chunkRadius : (int) Math.ceil(Math.sqrt(spawnThreshold));
int radiusSqr = radius * radius;
long index;
for (int x = 0; x <= radius; x++) {
int xx = x * x;
for (int z = 0; z <= x; z++) {
int distanceSqr = xx + z * z;
if (distanceSqr > radiusSqr) continue;
/* Top right quadrant */
if(this.usedChunks.get(index = Level.chunkHash(centerX + x, centerZ + z)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
/* Top left quadrant */
if(this.usedChunks.get(index = Level.chunkHash(centerX - x - 1, centerZ + z)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
/* Bottom right quadrant */
if(this.usedChunks.get(index = Level.chunkHash(centerX + x, centerZ - z - 1)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
/* Bottom left quadrant */
if(this.usedChunks.get(index = Level.chunkHash(centerX - x - 1, centerZ - z - 1)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
if(x != z){
/* Top right quadrant mirror */
if(this.usedChunks.get(index = Level.chunkHash(centerX + z, centerZ + x)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
/* Top left quadrant mirror */
if(this.usedChunks.get(index = Level.chunkHash(centerX - z - 1, centerZ + x)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
/* Bottom right quadrant mirror */
if(this.usedChunks.get(index = Level.chunkHash(centerX + z, centerZ - x - 1)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
/* Bottom left quadrant mirror */
if(this.usedChunks.get(index = Level.chunkHash(centerX - z - 1, centerZ - x - 1)) != Boolean.TRUE) {
this.loadQueue.put(index, Boolean.TRUE);
}
lastChunk.remove(index);
}
}
}
LongIterator keys = lastChunk.keySet().iterator();
while (keys.hasNext()) {
index = keys.nextLong();
this.unloadChunk(Level.getHashX(index), Level.getHashZ(index));
}