diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 860dec2b3..ce18440ab 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -96,9 +96,6 @@ abstract class Entity{ /** @var EntityMetadataCollection */ private $networkProperties; - /** @var Vector3 */ - public $worldLastKnownLocation; - /** @var EntityDamageEvent|null */ protected $lastDamageCause = null; @@ -229,7 +226,6 @@ abstract class Entity{ $this->server = $location->getWorld()->getServer(); $this->location = $location->asLocation(); - $this->worldLastKnownLocation = $this->location->asLocation(); assert( !is_nan($this->location->x) and !is_infinite($this->location->x) and !is_nan($this->location->y) and !is_infinite($this->location->y) and diff --git a/src/world/World.php b/src/world/World.php index 05513b8be..435e211aa 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -148,6 +148,11 @@ class World implements ChunkManager{ /** @var Entity[] */ private $entities = []; + /** + * @var Vector3[] + * @phpstan-var array + */ + private $entityLastKnownPositions = []; /** @var Entity[] */ public $updateEntities = []; @@ -2142,7 +2147,7 @@ class World implements ChunkManager{ throw new \InvalidArgumentException("Cannot add an Entity in an ungenerated chunk"); } $chunk->addEntity($entity); - $entity->worldLastKnownLocation = $pos; + $this->entityLastKnownPositions[$entity->getId()] = $pos; if($entity instanceof Player){ $this->players[$entity->getId()] = $entity; @@ -2159,11 +2164,12 @@ class World implements ChunkManager{ if($entity->getWorld() !== $this){ throw new \InvalidArgumentException("Invalid Entity world"); } - $pos = $entity->worldLastKnownLocation; + $pos = $this->entityLastKnownPositions[$entity->getId()]; $chunk = $this->getChunk($pos->getFloorX() >> 4, $pos->getFloorZ() >> 4); if($chunk !== null){ //we don't care if the chunk already went out of scope $chunk->removeEntity($entity); } + unset($this->entityLastKnownPositions[$entity->getId()]); if($entity instanceof Player){ unset($this->players[$entity->getId()]); @@ -2178,11 +2184,11 @@ class World implements ChunkManager{ * @internal */ public function onEntityMoved(Entity $entity) : void{ - if(!array_key_exists($entity->getId(), $this->entities)){ + if(!array_key_exists($entity->getId(), $this->entityLastKnownPositions)){ //this can happen if the entity was teleported before addEntity() was called return; } - $oldPosition = $entity->worldLastKnownLocation; + $oldPosition = $this->entityLastKnownPositions[$entity->getId()]; $newPosition = $entity->getPosition(); $oldChunkX = $oldPosition->getFloorX() >> 4; @@ -2219,9 +2225,9 @@ class World implements ChunkManager{ } $newChunk->addEntity($entity); - $entity->worldLastKnownLocation = $newPosition->asVector3(); } } + $this->entityLastKnownPositions[$entity->getId()] = $newPosition->asVector3(); } /**