PocketMine-MP 5.27.2 git-d86943fa8c6384be3e2c1901ebf94f584b27e784
Loading...
Searching...
No Matches
Entity.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
27namespace pocketmine\entity;
28
71use function abs;
72use function array_map;
73use function assert;
74use function cos;
75use function count;
76use function deg2rad;
77use function floatval;
78use function floor;
79use function fmod;
80use function get_class;
81use function min;
82use function sin;
83use function spl_object_id;
84use const M_PI_2;
85
86abstract class Entity{
87
88 public const MOTION_THRESHOLD = 0.00001;
89 protected const STEP_CLIP_MULTIPLIER = 0.4;
90
91 private const TAG_FIRE = "Fire"; //TAG_Short
92 private const TAG_ON_GROUND = "OnGround"; //TAG_Byte
93 private const TAG_FALL_DISTANCE = "FallDistance"; //TAG_Float
94 private const TAG_CUSTOM_NAME = "CustomName"; //TAG_String
95 private const TAG_CUSTOM_NAME_VISIBLE = "CustomNameVisible"; //TAG_Byte
96 public const TAG_POS = "Pos"; //TAG_List<TAG_Double>|TAG_List<TAG_Float>
97 public const TAG_MOTION = "Motion"; //TAG_List<TAG_Double>|TAG_List<TAG_Float>
98 public const TAG_ROTATION = "Rotation"; //TAG_List<TAG_Float>
99
100 private static int $entityCount = 1;
101
105 public static function nextRuntimeId() : int{
106 return self::$entityCount++;
107 }
108
113 protected array $hasSpawned = [];
114
115 protected int $id;
116
117 private EntityMetadataCollection $networkProperties;
118
119 protected ?EntityDamageEvent $lastDamageCause = null;
120
122 protected ?array $blocksAround = null;
123
124 protected Location $location;
125 protected Location $lastLocation;
126 protected Vector3 $motion;
127 protected Vector3 $lastMotion;
128 protected bool $forceMovementUpdate = false;
129 private bool $checkBlockIntersectionsNextTick = true;
130
131 public AxisAlignedBB $boundingBox;
132 public bool $onGround = false;
133
134 public EntitySizeInfo $size;
135
136 private float $health = 20.0;
137 private int $maxHealth = 20;
138
139 protected float $ySize = 0.0;
140 protected float $stepHeight = 0.0;
141 public bool $keepMovement = false;
142
143 public float $fallDistance = 0.0;
144 public int $ticksLived = 0;
145 public int $lastUpdate;
146 protected int $fireTicks = 0;
147
148 private bool $savedWithChunk = true;
149
150 public bool $isCollided = false;
151 public bool $isCollidedHorizontally = false;
152 public bool $isCollidedVertically = false;
153
154 public int $noDamageTicks = 0;
155 protected bool $justCreated = true;
156
157 protected AttributeMap $attributeMap;
158
159 protected float $gravity;
160 protected float $drag;
161 protected bool $gravityEnabled = true;
162
163 protected Server $server;
164
165 protected bool $closed = false;
166 private bool $closeInFlight = false;
167 private bool $needsDespawn = false;
168
169 protected TimingsHandler $timings;
170
171 protected bool $networkPropertiesDirty = false;
172
173 protected string $nameTag = "";
174 protected bool $nameTagVisible = true;
175 protected bool $alwaysShowNameTag = false;
176 protected string $scoreTag = "";
177 protected float $scale = 1.0;
178
179 protected bool $canClimb = false;
180 protected bool $canClimbWalls = false;
181 protected bool $noClientPredictions = false;
182 protected bool $invisible = false;
183 protected bool $silent = false;
184
185 protected ?int $ownerId = null;
186 protected ?int $targetId = null;
187
188 private bool $constructorCalled = false;
189
190 public function __construct(Location $location, ?CompoundTag $nbt = null){
191 if($this->constructorCalled){
192 throw new \LogicException("Attempted to call constructor for an Entity multiple times");
193 }
194 $this->constructorCalled = true;
195 Utils::checkLocationNotInfOrNaN($location);
196
197 $this->timings = Timings::getEntityTimings($this);
198
199 $this->size = $this->getInitialSizeInfo();
200 $this->drag = $this->getInitialDragMultiplier();
201 $this->gravity = $this->getInitialGravity();
202
203 $this->id = self::nextRuntimeId();
204 $this->server = $location->getWorld()->getServer();
205
206 $this->location = $location->asLocation();
207
208 $this->boundingBox = new AxisAlignedBB(0, 0, 0, 0, 0, 0);
209 $this->recalculateBoundingBox();
210
211 if($nbt !== null){
212 $this->motion = EntityDataHelper::parseVec3($nbt, self::TAG_MOTION, true);
213 }else{
214 $this->motion = Vector3::zero();
215 }
216
217 $this->resetLastMovements();
218
219 $this->networkProperties = new EntityMetadataCollection();
220
221 $this->attributeMap = new AttributeMap();
222 $this->addAttributes();
223
224 $this->initEntity($nbt ?? new CompoundTag());
225
226 $this->getWorld()->addEntity($this);
227
228 $this->lastUpdate = $this->server->getTick();
229
230 $this->scheduleUpdate();
231 }
232
233 abstract protected function getInitialSizeInfo() : EntitySizeInfo;
234
241 abstract protected function getInitialDragMultiplier() : float;
242
248 abstract protected function getInitialGravity() : float;
249
250 public function getNameTag() : string{
251 return $this->nameTag;
252 }
253
254 public function isNameTagVisible() : bool{
255 return $this->nameTagVisible;
256 }
257
258 public function isNameTagAlwaysVisible() : bool{
259 return $this->alwaysShowNameTag;
260 }
261
266 public function canBeRenamed() : bool{
267 return false;
268 }
269
270 public function setNameTag(string $name) : void{
271 $this->nameTag = $name;
272 $this->networkPropertiesDirty = true;
273 }
274
275 public function setNameTagVisible(bool $value = true) : void{
276 $this->nameTagVisible = $value;
277 $this->networkPropertiesDirty = true;
278 }
279
280 public function setNameTagAlwaysVisible(bool $value = true) : void{
281 $this->alwaysShowNameTag = $value;
282 $this->networkPropertiesDirty = true;
283 }
284
285 public function getScoreTag() : ?string{
286 return $this->scoreTag; //TODO: maybe this shouldn't be nullable?
287 }
288
289 public function setScoreTag(string $score) : void{
290 $this->scoreTag = $score;
291 $this->networkPropertiesDirty = true;
292 }
293
294 public function getScale() : float{
295 return $this->scale;
296 }
297
298 public function setScale(float $value) : void{
299 if($value <= 0){
300 throw new \InvalidArgumentException("Scale must be greater than 0");
301 }
302 $this->scale = $value;
303 $this->setSize($this->getInitialSizeInfo()->scale($value));
304 }
305
306 public function getBoundingBox() : AxisAlignedBB{
307 return $this->boundingBox;
308 }
309
310 protected function recalculateBoundingBox() : void{
311 $halfWidth = $this->size->getWidth() / 2;
312
313 $this->boundingBox = new AxisAlignedBB(
314 $this->location->x - $halfWidth,
315 $this->location->y + $this->ySize,
316 $this->location->z - $halfWidth,
317 $this->location->x + $halfWidth,
318 $this->location->y + $this->size->getHeight() + $this->ySize,
319 $this->location->z + $halfWidth
320 );
321 }
322
323 public function getSize() : EntitySizeInfo{
324 return $this->size;
325 }
326
327 protected function setSize(EntitySizeInfo $size) : void{
328 $this->size = $size;
329 $this->recalculateBoundingBox();
330 $this->networkPropertiesDirty = true;
331 }
332
337 public function hasNoClientPredictions() : bool{
338 return $this->noClientPredictions;
339 }
340
349 public function setNoClientPredictions(bool $value = true) : void{
350 $this->noClientPredictions = $value;
351 $this->networkPropertiesDirty = true;
352 }
353
354 public function isInvisible() : bool{
355 return $this->invisible;
356 }
357
358 public function setInvisible(bool $value = true) : void{
359 $this->invisible = $value;
360 $this->networkPropertiesDirty = true;
361 }
362
363 public function isSilent() : bool{
364 return $this->silent;
365 }
366
367 public function setSilent(bool $value = true) : void{
368 $this->silent = $value;
369 $this->networkPropertiesDirty = true;
370 }
371
375 public function canClimb() : bool{
376 return $this->canClimb;
377 }
378
382 public function setCanClimb(bool $value = true) : void{
383 $this->canClimb = $value;
384 $this->networkPropertiesDirty = true;
385 }
386
390 public function canClimbWalls() : bool{
391 return $this->canClimbWalls;
392 }
393
397 public function setCanClimbWalls(bool $value = true) : void{
398 $this->canClimbWalls = $value;
399 $this->networkPropertiesDirty = true;
400 }
401
405 public function getOwningEntityId() : ?int{
406 return $this->ownerId;
407 }
408
412 public function getOwningEntity() : ?Entity{
413 return $this->ownerId !== null ? $this->server->getWorldManager()->findEntity($this->ownerId) : null;
414 }
415
421 public function setOwningEntity(?Entity $owner) : void{
422 if($owner === null){
423 $this->ownerId = null;
424 }elseif($owner->closed){
425 throw new \InvalidArgumentException("Supplied owning entity is garbage and cannot be used");
426 }else{
427 $this->ownerId = $owner->getId();
428 }
429 $this->networkPropertiesDirty = true;
430 }
431
435 public function getTargetEntityId() : ?int{
436 return $this->targetId;
437 }
438
443 public function getTargetEntity() : ?Entity{
444 return $this->targetId !== null ? $this->server->getWorldManager()->findEntity($this->targetId) : null;
445 }
446
452 public function setTargetEntity(?Entity $target) : void{
453 if($target === null){
454 $this->targetId = null;
455 }elseif($target->closed){
456 throw new \InvalidArgumentException("Supplied target entity is garbage and cannot be used");
457 }else{
458 $this->targetId = $target->getId();
459 }
460 $this->networkPropertiesDirty = true;
461 }
462
466 public function canSaveWithChunk() : bool{
467 return $this->savedWithChunk;
468 }
469
474 public function setCanSaveWithChunk(bool $value) : void{
475 $this->savedWithChunk = $value;
476 }
477
478 public function saveNBT() : CompoundTag{
479 $nbt = CompoundTag::create()
480 ->setTag(self::TAG_POS, new ListTag([
481 new DoubleTag($this->location->x),
482 new DoubleTag($this->location->y),
483 new DoubleTag($this->location->z)
484 ]))
485 ->setTag(self::TAG_MOTION, new ListTag([
486 new DoubleTag($this->motion->x),
487 new DoubleTag($this->motion->y),
488 new DoubleTag($this->motion->z)
489 ]))
490 ->setTag(self::TAG_ROTATION, new ListTag([
491 new FloatTag($this->location->yaw),
492 new FloatTag($this->location->pitch)
493 ]));
494
495 if(!($this instanceof Player)){
496 EntityFactory::getInstance()->injectSaveId(get_class($this), $nbt);
497
498 if($this->getNameTag() !== ""){
499 $nbt->setString(self::TAG_CUSTOM_NAME, $this->getNameTag());
500 $nbt->setByte(self::TAG_CUSTOM_NAME_VISIBLE, $this->isNameTagVisible() ? 1 : 0);
501 }
502 }
503
504 $nbt->setFloat(self::TAG_FALL_DISTANCE, $this->fallDistance);
505 $nbt->setShort(self::TAG_FIRE, $this->fireTicks);
506 $nbt->setByte(self::TAG_ON_GROUND, $this->onGround ? 1 : 0);
507
508 $nbt->setLong(VersionInfo::TAG_WORLD_DATA_VERSION, VersionInfo::WORLD_DATA_VERSION);
509
510 return $nbt;
511 }
512
513 protected function initEntity(CompoundTag $nbt) : void{
514 $this->fireTicks = $nbt->getShort(self::TAG_FIRE, 0);
515
516 $this->onGround = $nbt->getByte(self::TAG_ON_GROUND, 0) !== 0;
517
518 $this->fallDistance = $nbt->getFloat(self::TAG_FALL_DISTANCE, 0.0);
519
520 if(($customNameTag = $nbt->getTag(self::TAG_CUSTOM_NAME)) instanceof StringTag){
521 $this->setNameTag($customNameTag->getValue());
522
523 if(($customNameVisibleTag = $nbt->getTag(self::TAG_CUSTOM_NAME_VISIBLE)) instanceof StringTag){
524 //Older versions incorrectly saved this as a string (see 890f72dbf23a77f294169b79590770470041adc4)
525 $this->setNameTagVisible($customNameVisibleTag->getValue() !== "");
526 }else{
527 $this->setNameTagVisible($nbt->getByte(self::TAG_CUSTOM_NAME_VISIBLE, 1) !== 0);
528 }
529 }
530 }
531
532 protected function addAttributes() : void{
533
534 }
535
536 public function attack(EntityDamageEvent $source) : void{
537 if($this->isFireProof() && (
538 $source->getCause() === EntityDamageEvent::CAUSE_FIRE ||
539 $source->getCause() === EntityDamageEvent::CAUSE_FIRE_TICK ||
540 $source->getCause() === EntityDamageEvent::CAUSE_LAVA
541 )
542 ){
543 $source->cancel();
544 }
545 $source->call();
546 if($source->isCancelled()){
547 return;
548 }
549
550 $this->setLastDamageCause($source);
551
552 $this->setHealth($this->getHealth() - $source->getFinalDamage());
553 }
554
555 public function heal(EntityRegainHealthEvent $source) : void{
556 $source->call();
557 if($source->isCancelled()){
558 return;
559 }
560
561 $this->setHealth($this->getHealth() + $source->getAmount());
562 }
563
564 public function kill() : void{
565 if($this->isAlive()){
566 $this->health = 0;
567 $this->onDeath();
568 $this->scheduleUpdate();
569 }
570 }
571
575 protected function onDeath() : void{
576
577 }
578
582 protected function onDeathUpdate(int $tickDiff) : bool{
583 return true;
584 }
585
586 public function isAlive() : bool{
587 return $this->health > 0;
588 }
589
590 public function getHealth() : float{
591 return $this->health;
592 }
593
597 public function setHealth(float $amount) : void{
598 if($amount === $this->health){
599 return;
600 }
601
602 if($amount <= 0){
603 if($this->isAlive()){
604 if(!$this->justCreated){
605 $this->kill();
606 }else{
607 $this->health = 0;
608 }
609 }
610 }elseif($amount <= $this->getMaxHealth() || $amount < $this->health){
611 $this->health = $amount;
612 }else{
613 $this->health = $this->getMaxHealth();
614 }
615 }
616
617 public function getMaxHealth() : int{
618 return $this->maxHealth;
619 }
620
621 public function setMaxHealth(int $amount) : void{
622 $this->maxHealth = $amount;
623 }
624
625 public function setLastDamageCause(EntityDamageEvent $type) : void{
626 $this->lastDamageCause = $type;
627 }
628
629 public function getLastDamageCause() : ?EntityDamageEvent{
630 return $this->lastDamageCause;
631 }
632
633 public function getAttributeMap() : AttributeMap{
634 return $this->attributeMap;
635 }
636
637 public function getNetworkProperties() : EntityMetadataCollection{
638 return $this->networkProperties;
639 }
640
641 protected function entityBaseTick(int $tickDiff = 1) : bool{
642 //TODO: check vehicles
643
644 if($this->justCreated){
645 $this->justCreated = false;
646 if(!$this->isAlive()){
647 $this->kill();
648 }
649 }
650
651 $changedProperties = $this->getDirtyNetworkData();
652 if(count($changedProperties) > 0){
653 $this->sendData(null, $changedProperties);
654 $this->networkProperties->clearDirtyProperties();
655 }
656
657 $hasUpdate = false;
658
659 if($this->checkBlockIntersectionsNextTick){
660 $this->checkBlockIntersections();
661 }
662 $this->checkBlockIntersectionsNextTick = true;
663
664 if($this->location->y <= World::Y_MIN - 16 && $this->isAlive()){
665 $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_VOID, 10);
666 $this->attack($ev);
667 $hasUpdate = true;
668 }
669
670 if($this->isOnFire() && $this->doOnFireTick($tickDiff)){
671 $hasUpdate = true;
672 }
673
674 if($this->noDamageTicks > 0){
675 $this->noDamageTicks -= $tickDiff;
676 if($this->noDamageTicks < 0){
677 $this->noDamageTicks = 0;
678 }
679 }
680
681 $this->ticksLived += $tickDiff;
682
683 return $hasUpdate;
684 }
685
686 public function isOnFire() : bool{
687 return $this->fireTicks > 0;
688 }
689
690 public function setOnFire(int $seconds) : void{
691 $ticks = $seconds * 20;
692 if($ticks > $this->getFireTicks()){
693 $this->setFireTicks($ticks);
694 }
695 $this->networkPropertiesDirty = true;
696 }
697
698 public function getFireTicks() : int{
699 return $this->fireTicks;
700 }
701
705 public function setFireTicks(int $fireTicks) : void{
706 if($fireTicks < 0){
707 throw new \InvalidArgumentException("Fire ticks cannot be negative");
708 }
709
710 //Since the max value is not externally obvious or intuitive, many plugins use this without being aware that
711 //reasonably large values are not accepted. We even have such usages within PM itself. It doesn't make sense
712 //to force all those calls to be aware of this limitation, as it's not a functional limit but a limitation of
713 //the Mojang save format. Truncating this to the max acceptable value is the next best thing we can do.
714 $fireTicks = min($fireTicks, Limits::INT16_MAX);
715
716 if(!$this->isFireProof()){
717 $this->fireTicks = $fireTicks;
718 $this->networkPropertiesDirty = true;
719 }
720 }
721
722 public function extinguish(int $cause = EntityExtinguishEvent::CAUSE_CUSTOM) : void{
723 $ev = new EntityExtinguishEvent($this, $cause);
724 $ev->call();
725
726 $this->fireTicks = 0;
727 $this->networkPropertiesDirty = true;
728 }
729
730 public function isFireProof() : bool{
731 return false;
732 }
733
734 protected function doOnFireTick(int $tickDiff = 1) : bool{
735 if($this->isFireProof() && $this->isOnFire()){
736 $this->extinguish(EntityExtinguishEvent::CAUSE_FIRE_PROOF);
737 return false;
738 }
739
740 $this->fireTicks -= $tickDiff;
741
742 if(($this->fireTicks % 20 === 0) || $tickDiff > 20){
743 $this->dealFireDamage();
744 }
745
746 if(!$this->isOnFire()){
747 $this->extinguish(EntityExtinguishEvent::CAUSE_TICKING);
748 }else{
749 return true;
750 }
751
752 return false;
753 }
754
758 protected function dealFireDamage() : void{
759 $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_FIRE_TICK, 1);
760 $this->attack($ev);
761 }
762
763 public function canCollideWith(Entity $entity) : bool{
764 return !$this->justCreated && $entity !== $this;
765 }
766
767 public function canBeCollidedWith() : bool{
768 return $this->isAlive();
769 }
770
771 protected function updateMovement(bool $teleport = false) : void{
772 $diffPosition = $this->location->distanceSquared($this->lastLocation);
773 $diffRotation = ($this->location->yaw - $this->lastLocation->yaw) ** 2 + ($this->location->pitch - $this->lastLocation->pitch) ** 2;
774
775 $diffMotion = $this->motion->subtractVector($this->lastMotion)->lengthSquared();
776
777 $still = $this->motion->lengthSquared() === 0.0;
778 $wasStill = $this->lastMotion->lengthSquared() === 0.0;
779 if($wasStill !== $still){
780 //TODO: hack for client-side AI interference: prevent client sided movement when motion is 0
781 $this->setNoClientPredictions($still);
782 }
783
784 if($teleport || $diffPosition > 0.0001 || $diffRotation > 1.0 || (!$wasStill && $still)){
785 $this->lastLocation = $this->location->asLocation();
786
787 $this->broadcastMovement($teleport);
788 }
789
790 if($diffMotion > 0.0025 || $wasStill !== $still){ //0.05 ** 2
791 $this->lastMotion = clone $this->motion;
792
793 $this->broadcastMotion();
794 }
795 }
796
797 public function getOffsetPosition(Vector3 $vector3) : Vector3{
798 return $vector3;
799 }
800
801 protected function broadcastMovement(bool $teleport = false) : void{
802 NetworkBroadcastUtils::broadcastPackets($this->hasSpawned, [MoveActorAbsolutePacket::create(
803 $this->id,
804 $this->getOffsetPosition($this->location),
805 $this->location->pitch,
806 $this->location->yaw,
807 $this->location->yaw,
808 (
809 //TODO: We should be setting FLAG_TELEPORT here to disable client-side movement interpolation, but it
810 //breaks player teleporting (observers see the player rubberband back to the pre-teleport position while
811 //the teleported player sees themselves at the correct position), and does nothing whatsoever for
812 //non-player entities (movement is still interpolated). Both of these are client bugs.
813 //See https://github.com/pmmp/PocketMine-MP/issues/4394
814 ($this->onGround ? MoveActorAbsolutePacket::FLAG_GROUND : 0)
815 )
816 )]);
817 }
818
819 protected function broadcastMotion() : void{
820 NetworkBroadcastUtils::broadcastPackets($this->hasSpawned, [SetActorMotionPacket::create($this->id, $this->getMotion(), tick: 0)]);
821 }
822
823 public function getGravity() : float{
824 return $this->gravity;
825 }
826
827 public function setGravity(float $gravity) : void{
828 Utils::checkFloatNotInfOrNaN("gravity", $gravity);
829 $this->gravity = $gravity;
830 }
831
832 public function hasGravity() : bool{
833 return $this->gravityEnabled;
834 }
835
836 public function setHasGravity(bool $v = true) : void{
837 $this->gravityEnabled = $v;
838 }
839
840 protected function applyDragBeforeGravity() : bool{
841 return false;
842 }
843
844 protected function tryChangeMovement() : void{
845 $friction = 1 - $this->drag;
846
847 $mY = $this->motion->y;
848
849 if($this->applyDragBeforeGravity()){
850 $mY *= $friction;
851 }
852
853 if($this->gravityEnabled){
854 $mY -= $this->gravity;
855 }
856
857 if(!$this->applyDragBeforeGravity()){
858 $mY *= $friction;
859 }
860
861 if($this->onGround){
862 $friction *= $this->getWorld()->getBlockAt((int) floor($this->location->x), (int) floor($this->location->y - 1), (int) floor($this->location->z))->getFrictionFactor();
863 }
864
865 $this->motion = new Vector3($this->motion->x * $friction, $mY, $this->motion->z * $friction);
866 }
867
868 protected function checkObstruction(float $x, float $y, float $z) : bool{
869 $world = $this->getWorld();
870 if(count($world->getBlockCollisionBoxes($this->boundingBox)) === 0){
871 return false;
872 }
873
874 $floorX = (int) floor($x);
875 $floorY = (int) floor($y);
876 $floorZ = (int) floor($z);
877
878 $diffX = $x - $floorX;
879 $diffY = $y - $floorY;
880 $diffZ = $z - $floorZ;
881
882 if($world->getBlockAt($floorX, $floorY, $floorZ)->isSolid()){
883 $westNonSolid = !$world->getBlockAt($floorX - 1, $floorY, $floorZ)->isSolid();
884 $eastNonSolid = !$world->getBlockAt($floorX + 1, $floorY, $floorZ)->isSolid();
885 $downNonSolid = !$world->getBlockAt($floorX, $floorY - 1, $floorZ)->isSolid();
886 $upNonSolid = !$world->getBlockAt($floorX, $floorY + 1, $floorZ)->isSolid();
887 $northNonSolid = !$world->getBlockAt($floorX, $floorY, $floorZ - 1)->isSolid();
888 $southNonSolid = !$world->getBlockAt($floorX, $floorY, $floorZ + 1)->isSolid();
889
890 $direction = -1;
891 $limit = 9999;
892
893 if($westNonSolid){
894 $limit = $diffX;
895 $direction = Facing::WEST;
896 }
897
898 if($eastNonSolid && 1 - $diffX < $limit){
899 $limit = 1 - $diffX;
900 $direction = Facing::EAST;
901 }
902
903 if($downNonSolid && $diffY < $limit){
904 $limit = $diffY;
905 $direction = Facing::DOWN;
906 }
907
908 if($upNonSolid && 1 - $diffY < $limit){
909 $limit = 1 - $diffY;
910 $direction = Facing::UP;
911 }
912
913 if($northNonSolid && $diffZ < $limit){
914 $limit = $diffZ;
915 $direction = Facing::NORTH;
916 }
917
918 if($southNonSolid && 1 - $diffZ < $limit){
919 $direction = Facing::SOUTH;
920 }
921
922 if($direction === -1){
923 return false;
924 }
925
926 $force = Utils::getRandomFloat() * 0.2 + 0.1;
927
928 $this->motion = match($direction){
929 Facing::WEST => $this->motion->withComponents(-$force, null, null),
930 Facing::EAST => $this->motion->withComponents($force, null, null),
931 Facing::DOWN => $this->motion->withComponents(null, -$force, null),
932 Facing::UP => $this->motion->withComponents(null, $force, null),
933 Facing::NORTH => $this->motion->withComponents(null, null, -$force),
934 Facing::SOUTH => $this->motion->withComponents(null, null, $force),
935 };
936 return true;
937 }
938
939 return false;
940 }
941
942 public function getHorizontalFacing() : int{
943 $angle = fmod($this->location->yaw, 360);
944 if($angle < 0){
945 $angle += 360.0;
946 }
947
948 if((0 <= $angle && $angle < 45) || (315 <= $angle && $angle < 360)){
949 return Facing::SOUTH;
950 }
951 if(45 <= $angle && $angle < 135){
952 return Facing::WEST;
953 }
954 if(135 <= $angle && $angle < 225){
955 return Facing::NORTH;
956 }
957
958 return Facing::EAST;
959 }
960
961 public function getDirectionVector() : Vector3{
962 $y = -sin(deg2rad($this->location->pitch));
963 $xz = cos(deg2rad($this->location->pitch));
964 $x = -$xz * sin(deg2rad($this->location->yaw));
965 $z = $xz * cos(deg2rad($this->location->yaw));
966
967 return (new Vector3($x, $y, $z))->normalize();
968 }
969
970 public function getDirectionPlane() : Vector2{
971 return (new Vector2(-cos(deg2rad($this->location->yaw) - M_PI_2), -sin(deg2rad($this->location->yaw) - M_PI_2)))->normalize();
972 }
973
978 protected function onFirstUpdate(int $currentTick) : void{
979 (new EntitySpawnEvent($this))->call();
980 }
981
982 public function onUpdate(int $currentTick) : bool{
983 if($this->closed){
984 return false;
985 }
986
987 $tickDiff = $currentTick - $this->lastUpdate;
988 if($tickDiff <= 0){
989 if(!$this->justCreated){
990 $this->server->getLogger()->debug("Expected tick difference of at least 1, got $tickDiff for " . get_class($this));
991 }
992
993 return true;
994 }
995
996 $this->lastUpdate = $currentTick;
997
998 if($this->justCreated){
999 $this->onFirstUpdate($currentTick);
1000 }
1001
1002 if(!$this->isAlive()){
1003 if($this->onDeathUpdate($tickDiff)){
1004 $this->flagForDespawn();
1005 }
1006
1007 return true;
1008 }
1009
1010 $this->timings->startTiming();
1011
1012 if($this->hasMovementUpdate()){
1013 $this->tryChangeMovement();
1014
1015 $this->motion = $this->motion->withComponents(
1016 abs($this->motion->x) <= self::MOTION_THRESHOLD ? 0 : null,
1017 abs($this->motion->y) <= self::MOTION_THRESHOLD ? 0 : null,
1018 abs($this->motion->z) <= self::MOTION_THRESHOLD ? 0 : null
1019 );
1020
1021 if(floatval($this->motion->x) !== 0.0 || floatval($this->motion->y) !== 0.0 || floatval($this->motion->z) !== 0.0){
1022 $this->move($this->motion->x, $this->motion->y, $this->motion->z);
1023 }
1024
1025 $this->forceMovementUpdate = false;
1026 }
1027
1028 $this->updateMovement();
1029
1030 Timings::$entityBaseTick->startTiming();
1031 $hasUpdate = $this->entityBaseTick($tickDiff);
1032 Timings::$entityBaseTick->stopTiming();
1033
1034 $this->timings->stopTiming();
1035
1036 return ($hasUpdate || $this->hasMovementUpdate());
1037 }
1038
1039 final public function scheduleUpdate() : void{
1040 if($this->closed){
1041 throw new \LogicException("Cannot schedule update on garbage entity " . get_class($this));
1042 }
1043 $this->getWorld()->updateEntities[$this->id] = $this;
1044 }
1045
1046 public function onNearbyBlockChange() : void{
1047 $this->setForceMovementUpdate();
1048 $this->scheduleUpdate();
1049 }
1050
1055 public function onRandomUpdate() : void{
1056 $this->scheduleUpdate();
1057 }
1058
1063 final public function setForceMovementUpdate(bool $value = true) : void{
1064 $this->forceMovementUpdate = $value;
1065
1066 $this->blocksAround = null;
1067 }
1068
1072 public function hasMovementUpdate() : bool{
1073 return (
1074 $this->forceMovementUpdate ||
1075 floatval($this->motion->x) !== 0.0 ||
1076 floatval($this->motion->y) !== 0.0 ||
1077 floatval($this->motion->z) !== 0.0 ||
1078 !$this->onGround
1079 );
1080 }
1081
1082 public function getFallDistance() : float{ return $this->fallDistance; }
1083
1084 public function setFallDistance(float $fallDistance) : void{
1085 $this->fallDistance = $fallDistance;
1086 }
1087
1088 public function resetFallDistance() : void{
1089 $this->fallDistance = 0.0;
1090 }
1091
1092 protected function updateFallState(float $distanceThisTick, bool $onGround) : ?float{
1093 if($distanceThisTick < $this->fallDistance){
1094 //we've fallen some distance (distanceThisTick is negative)
1095 //or we ascended back towards where fall distance was measured from initially (distanceThisTick is positive but less than existing fallDistance)
1096 $this->fallDistance -= $distanceThisTick;
1097 }else{
1098 //we ascended past the apex where fall distance was originally being measured from
1099 //reset it so it will be measured starting from the new, higher position
1100 $this->fallDistance = 0;
1101 }
1102 if($onGround && $this->fallDistance > 0){
1103 $newVerticalVelocity = $this->onHitGround();
1104 $this->resetFallDistance();
1105 return $newVerticalVelocity;
1106 }
1107 return null;
1108 }
1109
1113 protected function onHitGround() : ?float{
1114 return null;
1115 }
1116
1117 public function getEyeHeight() : float{
1118 return $this->size->getEyeHeight();
1119 }
1120
1121 public function getEyePos() : Vector3{
1122 return new Vector3($this->location->x, $this->location->y + $this->getEyeHeight(), $this->location->z);
1123 }
1124
1125 public function onCollideWithPlayer(Player $player) : void{
1126
1127 }
1128
1132 public function onInteract(Player $player, Vector3 $clickPos) : bool{
1133 return false;
1134 }
1135
1136 public function isUnderwater() : bool{
1137 $block = $this->getWorld()->getBlockAt((int) floor($this->location->x), $blockY = (int) floor($y = ($this->location->y + $this->getEyeHeight())), (int) floor($this->location->z));
1138
1139 if($block instanceof Water){
1140 $f = ($blockY + 1) - ($block->getFluidHeightPercent() - 0.1111111);
1141 return $y < $f;
1142 }
1143
1144 return false;
1145 }
1146
1147 public function isInsideOfSolid() : bool{
1148 $block = $this->getWorld()->getBlockAt((int) floor($this->location->x), (int) floor($y = ($this->location->y + $this->getEyeHeight())), (int) floor($this->location->z));
1149
1150 return $block->isSolid() && !$block->isTransparent() && $block->collidesWithBB($this->getBoundingBox());
1151 }
1152
1153 protected function move(float $dx, float $dy, float $dz) : void{
1154 $this->blocksAround = null;
1155
1156 Timings::$entityMove->startTiming();
1157 Timings::$entityMoveCollision->startTiming();
1158
1159 $wantedX = $dx;
1160 $wantedY = $dy;
1161 $wantedZ = $dz;
1162
1163 if($this->keepMovement){
1164 $this->boundingBox->offset($dx, $dy, $dz);
1165 }else{
1166 $this->ySize *= self::STEP_CLIP_MULTIPLIER;
1167
1168 $moveBB = clone $this->boundingBox;
1169
1170 assert(abs($dx) <= 20 && abs($dy) <= 20 && abs($dz) <= 20, "Movement distance is excessive: dx=$dx, dy=$dy, dz=$dz");
1171
1172 $list = $this->getWorld()->getBlockCollisionBoxes($moveBB->addCoord($dx, $dy, $dz));
1173
1174 foreach($list as $bb){
1175 $dy = $bb->calculateYOffset($moveBB, $dy);
1176 }
1177
1178 $moveBB->offset(0, $dy, 0);
1179
1180 $fallingFlag = ($this->onGround || ($dy !== $wantedY && $wantedY < 0));
1181
1182 foreach($list as $bb){
1183 $dx = $bb->calculateXOffset($moveBB, $dx);
1184 }
1185
1186 $moveBB->offset($dx, 0, 0);
1187
1188 foreach($list as $bb){
1189 $dz = $bb->calculateZOffset($moveBB, $dz);
1190 }
1191
1192 $moveBB->offset(0, 0, $dz);
1193
1194 if($this->stepHeight > 0 && $fallingFlag && ($wantedX !== $dx || $wantedZ !== $dz)){
1195 $cx = $dx;
1196 $cy = $dy;
1197 $cz = $dz;
1198 $dx = $wantedX;
1199 $dy = $this->stepHeight;
1200 $dz = $wantedZ;
1201
1202 $stepBB = clone $this->boundingBox;
1203
1204 $list = $this->getWorld()->getBlockCollisionBoxes($stepBB->addCoord($dx, $dy, $dz));
1205 foreach($list as $bb){
1206 $dy = $bb->calculateYOffset($stepBB, $dy);
1207 }
1208
1209 $stepBB->offset(0, $dy, 0);
1210
1211 foreach($list as $bb){
1212 $dx = $bb->calculateXOffset($stepBB, $dx);
1213 }
1214
1215 $stepBB->offset($dx, 0, 0);
1216
1217 foreach($list as $bb){
1218 $dz = $bb->calculateZOffset($stepBB, $dz);
1219 }
1220
1221 $stepBB->offset(0, 0, $dz);
1222
1223 $reverseDY = -$dy;
1224 foreach($list as $bb){
1225 $reverseDY = $bb->calculateYOffset($stepBB, $reverseDY);
1226 }
1227 $dy += $reverseDY;
1228 $stepBB->offset(0, $reverseDY, 0);
1229
1230 if(($cx ** 2 + $cz ** 2) >= ($dx ** 2 + $dz ** 2)){
1231 $dx = $cx;
1232 $dy = $cy;
1233 $dz = $cz;
1234 }else{
1235 $moveBB = $stepBB;
1236 $this->ySize += $dy;
1237 }
1238 }
1239
1240 $this->boundingBox = $moveBB;
1241 }
1242 Timings::$entityMoveCollision->stopTiming();
1243
1244 $this->location = new Location(
1245 ($this->boundingBox->minX + $this->boundingBox->maxX) / 2,
1246 $this->boundingBox->minY - $this->ySize,
1247 ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2,
1248 $this->location->world,
1249 $this->location->yaw,
1250 $this->location->pitch
1251 );
1252
1253 $this->getWorld()->onEntityMoved($this);
1254 $this->checkBlockIntersections();
1255 $this->checkGroundState($wantedX, $wantedY, $wantedZ, $dx, $dy, $dz);
1256 $postFallVerticalVelocity = $this->updateFallState($dy, $this->onGround);
1257
1258 $this->motion = $this->motion->withComponents(
1259 $wantedX !== $dx ? 0 : null,
1260 $postFallVerticalVelocity ?? ($wantedY !== $dy ? 0 : null),
1261 $wantedZ !== $dz ? 0 : null
1262 );
1263
1264 //TODO: vehicle collision events (first we need to spawn them!)
1265
1266 Timings::$entityMove->stopTiming();
1267 }
1268
1269 protected function checkGroundState(float $wantedX, float $wantedY, float $wantedZ, float $dx, float $dy, float $dz) : void{
1270 $this->isCollidedVertically = $wantedY !== $dy;
1271 $this->isCollidedHorizontally = ($wantedX !== $dx || $wantedZ !== $dz);
1272 $this->isCollided = ($this->isCollidedHorizontally || $this->isCollidedVertically);
1273 $this->onGround = ($wantedY !== $dy && $wantedY < 0);
1274 }
1275
1281 protected function getBlocksIntersected(float $inset) : \Generator{
1282 $minX = (int) floor($this->boundingBox->minX + $inset);
1283 $minY = (int) floor($this->boundingBox->minY + $inset);
1284 $minZ = (int) floor($this->boundingBox->minZ + $inset);
1285 $maxX = (int) floor($this->boundingBox->maxX - $inset);
1286 $maxY = (int) floor($this->boundingBox->maxY - $inset);
1287 $maxZ = (int) floor($this->boundingBox->maxZ - $inset);
1288
1289 $world = $this->getWorld();
1290
1291 for($z = $minZ; $z <= $maxZ; ++$z){
1292 for($x = $minX; $x <= $maxX; ++$x){
1293 for($y = $minY; $y <= $maxY; ++$y){
1294 yield $world->getBlockAt($x, $y, $z);
1295 }
1296 }
1297 }
1298 }
1299
1303 protected function getBlocksAroundWithEntityInsideActions() : array{
1304 if($this->blocksAround === null){
1305 $this->blocksAround = [];
1306
1307 $inset = 0.001; //Offset against floating-point errors
1308 foreach($this->getBlocksIntersected($inset) as $block){
1309 if($block->hasEntityCollision()){
1310 $this->blocksAround[] = $block;
1311 }
1312 }
1313 }
1314
1315 return $this->blocksAround;
1316 }
1317
1321 public function canBeMovedByCurrents() : bool{
1322 return true;
1323 }
1324
1325 protected function checkBlockIntersections() : void{
1326 $this->checkBlockIntersectionsNextTick = false;
1327 $vectors = [];
1328
1329 foreach($this->getBlocksAroundWithEntityInsideActions() as $block){
1330 if(!$block->onEntityInside($this)){
1331 $this->blocksAround = null;
1332 }
1333 if(($v = $block->addVelocityToEntity($this)) !== null){
1334 $vectors[] = $v;
1335 }
1336 }
1337
1338 if(count($vectors) > 0){
1339 $vector = Vector3::sum(...$vectors);
1340 if($vector->lengthSquared() > 0){
1341 $d = 0.014;
1342 $this->motion = $this->motion->addVector($vector->normalize()->multiply($d));
1343 }
1344 }
1345 }
1346
1347 public function getPosition() : Position{
1348 return $this->location->asPosition();
1349 }
1350
1351 public function getLocation() : Location{
1352 return $this->location->asLocation();
1353 }
1354
1355 public function getWorld() : World{
1356 return $this->location->getWorld();
1357 }
1358
1359 protected function setPosition(Vector3 $pos) : bool{
1360 if($this->closed){
1361 return false;
1362 }
1363
1364 $oldWorld = $this->getWorld();
1365 $newWorld = $pos instanceof Position ? $pos->getWorld() : $oldWorld;
1366 if($oldWorld !== $newWorld){
1367 $this->despawnFromAll();
1368 $oldWorld->removeEntity($this);
1369 }
1370
1371 $this->location = Location::fromObject(
1372 $pos,
1373 $newWorld,
1374 $this->location->yaw,
1375 $this->location->pitch
1376 );
1377
1378 $this->recalculateBoundingBox();
1379
1380 $this->blocksAround = null;
1381
1382 if($oldWorld !== $newWorld){
1383 $newWorld->addEntity($this);
1384 }else{
1385 $newWorld->onEntityMoved($this);
1386 }
1387
1388 return true;
1389 }
1390
1391 public function setRotation(float $yaw, float $pitch) : void{
1392 Utils::checkFloatNotInfOrNaN("yaw", $yaw);
1393 Utils::checkFloatNotInfOrNaN("pitch", $pitch);
1394 $this->location->yaw = $yaw;
1395 $this->location->pitch = $pitch;
1396 $this->scheduleUpdate();
1397 }
1398
1399 protected function setPositionAndRotation(Vector3 $pos, float $yaw, float $pitch) : bool{
1400 if($this->setPosition($pos)){
1401 $this->setRotation($yaw, $pitch);
1402
1403 return true;
1404 }
1405
1406 return false;
1407 }
1408
1409 protected function resetLastMovements() : void{
1410 $this->lastLocation = $this->location->asLocation();
1411 $this->lastMotion = clone $this->motion;
1412 }
1413
1414 public function getMotion() : Vector3{
1415 return clone $this->motion;
1416 }
1417
1418 public function setMotion(Vector3 $motion) : bool{
1419 Utils::checkVector3NotInfOrNaN($motion);
1420 if(!$this->justCreated){
1421 $ev = new EntityMotionEvent($this, $motion);
1422 $ev->call();
1423 if($ev->isCancelled()){
1424 return false;
1425 }
1426 }
1427
1428 $this->motion = clone $motion;
1429
1430 if(!$this->justCreated){
1431 $this->updateMovement();
1432 }
1433
1434 return true;
1435 }
1436
1440 public function addMotion(float $x, float $y, float $z) : void{
1441 Utils::checkFloatNotInfOrNaN("x", $x);
1442 Utils::checkFloatNotInfOrNaN("y", $y);
1443 Utils::checkFloatNotInfOrNaN("z", $z);
1444 $this->motion = $this->motion->add($x, $y, $z);
1445 }
1446
1447 public function isOnGround() : bool{
1448 return $this->onGround;
1449 }
1450
1454 public function teleport(Vector3 $pos, ?float $yaw = null, ?float $pitch = null) : bool{
1455 Utils::checkVector3NotInfOrNaN($pos);
1456 if($pos instanceof Location){
1457 $yaw = $yaw ?? $pos->yaw;
1458 $pitch = $pitch ?? $pos->pitch;
1459 }
1460 if($yaw !== null){
1461 Utils::checkFloatNotInfOrNaN("yaw", $yaw);
1462 }
1463 if($pitch !== null){
1464 Utils::checkFloatNotInfOrNaN("pitch", $pitch);
1465 }
1466
1467 $from = $this->location->asPosition();
1468 $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getWorld() : $this->getWorld());
1469 $ev = new EntityTeleportEvent($this, $from, $to);
1470 $ev->call();
1471 if($ev->isCancelled()){
1472 return false;
1473 }
1474 $this->ySize = 0;
1475 $pos = $ev->getTo();
1476
1477 $this->setMotion(new Vector3(0, 0, 0));
1478 if($this->setPositionAndRotation($pos, $yaw ?? $this->location->yaw, $pitch ?? $this->location->pitch)){
1479 $this->resetFallDistance();
1480 $this->setForceMovementUpdate();
1481
1482 $this->updateMovement(true);
1483
1484 return true;
1485 }
1486
1487 return false;
1488 }
1489
1490 public function getId() : int{
1491 return $this->id;
1492 }
1493
1497 public function getViewers() : array{
1498 return $this->hasSpawned;
1499 }
1500
1501 abstract public function getNetworkTypeId() : string;
1502
1506 protected function sendSpawnPacket(Player $player) : void{
1507 $player->getNetworkSession()->sendDataPacket(AddActorPacket::create(
1508 $this->getId(), //TODO: actor unique ID
1509 $this->getId(),
1510 $this->getNetworkTypeId(),
1511 $this->getOffsetPosition($this->location->asVector3()),
1512 $this->getMotion(),
1513 $this->location->pitch,
1514 $this->location->yaw,
1515 $this->location->yaw, //TODO: head yaw
1516 $this->location->yaw, //TODO: body yaw (wtf mojang?)
1517 array_map(function(Attribute $attr) : NetworkAttribute{
1518 return new NetworkAttribute($attr->getId(), $attr->getMinValue(), $attr->getMaxValue(), $attr->getValue(), $attr->getDefaultValue(), []);
1519 }, $this->attributeMap->getAll()),
1520 $this->getAllNetworkData(),
1521 new PropertySyncData([], []),
1522 [] //TODO: entity links
1523 ));
1524 }
1525
1526 public function spawnTo(Player $player) : void{
1527 $id = spl_object_id($player);
1528 //TODO: this will cause some visible lag during chunk resends; if the player uses a spawn egg in a chunk, the
1529 //created entity won't be visible until after the resend arrives. However, this is better than possibly crashing
1530 //the player by sending them entities too early.
1531 if(!isset($this->hasSpawned[$id]) && $player->getWorld() === $this->getWorld() && $player->hasReceivedChunk($this->location->getFloorX() >> Chunk::COORD_BIT_SIZE, $this->location->getFloorZ() >> Chunk::COORD_BIT_SIZE)){
1532 $this->hasSpawned[$id] = $player;
1533
1534 $this->sendSpawnPacket($player);
1535 }
1536 }
1537
1538 public function spawnToAll() : void{
1539 if($this->closed){
1540 return;
1541 }
1542 foreach($this->getWorld()->getViewersForPosition($this->location) as $player){
1543 $this->spawnTo($player);
1544 }
1545 }
1546
1547 public function respawnToAll() : void{
1548 foreach($this->hasSpawned as $key => $player){
1549 unset($this->hasSpawned[$key]);
1550 $this->spawnTo($player);
1551 }
1552 }
1553
1558 public function despawnFrom(Player $player, bool $send = true) : void{
1559 $id = spl_object_id($player);
1560 if(isset($this->hasSpawned[$id])){
1561 if($send){
1562 $player->getNetworkSession()->getEntityEventBroadcaster()->onEntityRemoved([$player->getNetworkSession()], $this);
1563 }
1564 unset($this->hasSpawned[$id]);
1565 }
1566 }
1567
1572 public function despawnFromAll() : void{
1573 NetworkBroadcastUtils::broadcastEntityEvent(
1574 $this->hasSpawned,
1575 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onEntityRemoved($recipients, $this)
1576 );
1577 $this->hasSpawned = [];
1578 }
1579
1583 public function getPickedItem() : ?Item{
1584 return null;
1585 }
1586
1590 public function flagForDespawn() : void{
1591 $this->needsDespawn = true;
1592 $this->scheduleUpdate();
1593 }
1594
1595 public function isFlaggedForDespawn() : bool{
1596 return $this->needsDespawn;
1597 }
1598
1602 public function isClosed() : bool{
1603 return $this->closed;
1604 }
1605
1611 final public function close() : void{
1612 if($this->closeInFlight){
1613 return;
1614 }
1615
1616 if(!$this->closed){
1617 $this->closeInFlight = true;
1618 (new EntityDespawnEvent($this))->call();
1619
1620 $this->onDispose();
1621 $this->closed = true;
1622 $this->destroyCycles();
1623 $this->closeInFlight = false;
1624 }
1625 }
1626
1631 protected function onDispose() : void{
1632 $this->despawnFromAll();
1633 if($this->location->isValid()){
1634 $this->getWorld()->removeEntity($this);
1635 }
1636 }
1637
1644 protected function destroyCycles() : void{
1645 $this->lastDamageCause = null;
1646 }
1647
1654 public function sendData(?array $targets, ?array $data = null) : void{
1655 $targets = $targets ?? $this->hasSpawned;
1656 $data = $data ?? $this->getAllNetworkData();
1657
1658 NetworkBroadcastUtils::broadcastEntityEvent($targets, fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->syncActorData($recipients, $this, $data));
1659 }
1660
1665 final protected function getDirtyNetworkData() : array{
1666 if($this->networkPropertiesDirty){
1667 $this->syncNetworkData($this->networkProperties);
1668 $this->networkPropertiesDirty = false;
1669 }
1670 return $this->networkProperties->getDirty();
1671 }
1672
1677 final protected function getAllNetworkData() : array{
1678 if($this->networkPropertiesDirty){
1679 $this->syncNetworkData($this->networkProperties);
1680 $this->networkPropertiesDirty = false;
1681 }
1682 return $this->networkProperties->getAll();
1683 }
1684
1685 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
1686 $properties->setByte(EntityMetadataProperties::ALWAYS_SHOW_NAMETAG, $this->alwaysShowNameTag ? 1 : 0);
1687 $properties->setFloat(EntityMetadataProperties::BOUNDING_BOX_HEIGHT, $this->size->getHeight() / $this->scale);
1688 $properties->setFloat(EntityMetadataProperties::BOUNDING_BOX_WIDTH, $this->size->getWidth() / $this->scale);
1689 $properties->setFloat(EntityMetadataProperties::SCALE, $this->scale);
1690 $properties->setLong(EntityMetadataProperties::LEAD_HOLDER_EID, -1);
1691 $properties->setLong(EntityMetadataProperties::OWNER_EID, $this->ownerId ?? -1);
1692 $properties->setLong(EntityMetadataProperties::TARGET_EID, $this->targetId ?? 0);
1693 $properties->setString(EntityMetadataProperties::NAMETAG, $this->nameTag);
1694 $properties->setString(EntityMetadataProperties::SCORE_TAG, $this->scoreTag);
1695 $properties->setByte(EntityMetadataProperties::COLOR, 0);
1696
1697 $properties->setGenericFlag(EntityMetadataFlags::AFFECTED_BY_GRAVITY, $this->gravityEnabled);
1698 $properties->setGenericFlag(EntityMetadataFlags::CAN_CLIMB, $this->canClimb);
1699 $properties->setGenericFlag(EntityMetadataFlags::CAN_SHOW_NAMETAG, $this->nameTagVisible);
1700 $properties->setGenericFlag(EntityMetadataFlags::HAS_COLLISION, true);
1701 $properties->setGenericFlag(EntityMetadataFlags::NO_AI, $this->noClientPredictions);
1702 $properties->setGenericFlag(EntityMetadataFlags::INVISIBLE, $this->invisible);
1703 $properties->setGenericFlag(EntityMetadataFlags::SILENT, $this->silent);
1704 $properties->setGenericFlag(EntityMetadataFlags::ONFIRE, $this->isOnFire());
1705 $properties->setGenericFlag(EntityMetadataFlags::WALLCLIMBING, $this->canClimbWalls);
1706 }
1707
1711 public function broadcastAnimation(Animation $animation, ?array $targets = null) : void{
1712 NetworkBroadcastUtils::broadcastPackets($targets ?? $this->getViewers(), $animation->encode());
1713 }
1714
1719 public function broadcastSound(Sound $sound, ?array $targets = null) : void{
1720 if(!$this->silent){
1721 $this->getWorld()->addSound($this->location->asVector3(), $sound, $targets ?? $this->getViewers());
1722 }
1723 }
1724
1725 public function __destruct(){
1726 $this->close();
1727 }
1728
1729 public function __toString(){
1730 return (new \ReflectionClass($this))->getShortName() . "(" . $this->getId() . ")";
1731 }
1732}
static parseVec3(CompoundTag $nbt, string $tagName, bool $optional)
setNoClientPredictions(bool $value=true)
Definition Entity.php:349
getBlocksIntersected(float $inset)
Definition Entity.php:1281
sendData(?array $targets, ?array $data=null)
Definition Entity.php:1654
setCanClimbWalls(bool $value=true)
Definition Entity.php:397
despawnFrom(Player $player, bool $send=true)
Definition Entity.php:1558
setCanClimb(bool $value=true)
Definition Entity.php:382
teleport(Vector3 $pos, ?float $yaw=null, ?float $pitch=null)
Definition Entity.php:1454
broadcastSound(Sound $sound, ?array $targets=null)
Definition Entity.php:1719
setCanSaveWithChunk(bool $value)
Definition Entity.php:474
broadcastAnimation(Animation $animation, ?array $targets=null)
Definition Entity.php:1711
sendSpawnPacket(Player $player)
Definition Entity.php:1506
setForceMovementUpdate(bool $value=true)
Definition Entity.php:1063
setOwningEntity(?Entity $owner)
Definition Entity.php:421
setFireTicks(int $fireTicks)
Definition Entity.php:705
addMotion(float $x, float $y, float $z)
Definition Entity.php:1440
onFirstUpdate(int $currentTick)
Definition Entity.php:978
setTargetEntity(?Entity $target)
Definition Entity.php:452
setHealth(float $amount)
Definition Entity.php:597
setFloat(string $name, float $value)
hasReceivedChunk(int $chunkX, int $chunkZ)
Definition Player.php:1069
syncActorData(array $recipients, Entity $entity, array $properties)