diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 70f5e3344..f80a91dd7 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2204,9 +2204,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{ //TODO: add events so plugins can change this - if($this->chunk !== null){ - $this->getLevel()->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $packet); - } + $this->getLevel()->broadcastPacketToViewers($this, $packet); return true; } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 5537b6f22..7a49ae904 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -1149,34 +1149,30 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ } protected function broadcastMovement(bool $teleport = false) : void{ - if($this->chunk !== null){ - $pk = new MoveEntityAbsolutePacket(); - $pk->entityRuntimeId = $this->id; - $pk->position = $this->getOffsetPosition($this); + $pk = new MoveEntityAbsolutePacket(); + $pk->entityRuntimeId = $this->id; + $pk->position = $this->getOffsetPosition($this); - //this looks very odd but is correct as of 1.5.0.7 - //for arrows this is actually x/y/z rotation - //for mobs x and z are used for pitch and yaw, and y is used for headyaw - $pk->xRot = $this->pitch; - $pk->yRot = $this->yaw; //TODO: head yaw - $pk->zRot = $this->yaw; + //this looks very odd but is correct as of 1.5.0.7 + //for arrows this is actually x/y/z rotation + //for mobs x and z are used for pitch and yaw, and y is used for headyaw + $pk->xRot = $this->pitch; + $pk->yRot = $this->yaw; //TODO: head yaw + $pk->zRot = $this->yaw; - if($teleport){ - $pk->flags |= MoveEntityAbsolutePacket::FLAG_TELEPORT; - } - - $this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk); + if($teleport){ + $pk->flags |= MoveEntityAbsolutePacket::FLAG_TELEPORT; } + + $this->level->broadcastPacketToViewers($this, $pk); } protected function broadcastMotion() : void{ - if($this->chunk !== null){ - $pk = new SetEntityMotionPacket(); - $pk->entityRuntimeId = $this->id; - $pk->motion = $this->getMotion(); + $pk = new SetEntityMotionPacket(); + $pk->entityRuntimeId = $this->id; + $pk->motion = $this->getMotion(); - $this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk); - } + $this->level->broadcastPacketToViewers($this, $pk); } protected function applyDragBeforeGravity() : bool{ diff --git a/src/pocketmine/inventory/ChestInventory.php b/src/pocketmine/inventory/ChestInventory.php index 70b6df590..6414550ec 100644 --- a/src/pocketmine/inventory/ChestInventory.php +++ b/src/pocketmine/inventory/ChestInventory.php @@ -87,6 +87,6 @@ class ChestInventory extends ContainerInventory{ $pk->z = (int) $holder->z; $pk->eventType = 1; //it's always 1 for a chest $pk->eventData = $isOpen ? 1 : 0; - $holder->getLevel()->addChunkPacket($holder->getFloorX() >> 4, $holder->getFloorZ() >> 4, $pk); + $holder->getLevel()->broadcastPacketToViewers($holder, $pk); } } diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index ea4fa95bb..f8da063ae 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -253,7 +253,7 @@ class Explosion{ $pk->position = $this->source->asVector3(); $pk->radius = $this->size; $pk->records = $send; - $this->level->addChunkPacket($source->getFloorX() >> 4, $source->getFloorZ() >> 4, $pk); + $this->level->broadcastPacketToViewers($source, $pk); $this->level->addParticle(new HugeExplodeSeedParticle($source)); $this->level->broadcastLevelSoundEvent($source, LevelSoundEventPacket::SOUND_EXPLODE); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index f652a29b4..2ae01834a 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -452,7 +452,7 @@ class Level implements ChunkManager, Metadatable{ if($players === null){ foreach($pk as $e){ - $this->addChunkPacket($sound->getFloorX() >> 4, $sound->getFloorZ() >> 4, $e); + $this->broadcastPacketToViewers($sound, $e); } }else{ $this->server->batchPackets($players, $pk, false); @@ -467,7 +467,7 @@ class Level implements ChunkManager, Metadatable{ if($players === null){ foreach($pk as $e){ - $this->addChunkPacket($particle->getFloorX() >> 4, $particle->getFloorZ() >> 4, $e); + $this->broadcastPacketToViewers($particle, $e); } }else{ $this->server->batchPackets($players, $pk, false); @@ -487,7 +487,7 @@ class Level implements ChunkManager, Metadatable{ $pk->data = $data; if($pos !== null){ $pk->position = $pos->asVector3(); - $this->addChunkPacket($pos->getFloorX() >> 4, $pos->getFloorZ() >> 4, $pk); + $this->broadcastPacketToViewers($pos, $pk); }else{ $pk->position = null; $this->addGlobalPacket($pk); @@ -512,7 +512,7 @@ class Level implements ChunkManager, Metadatable{ $pk->isBabyMob = $isBabyMob; $pk->disableRelativeVolume = $disableRelativeVolume; $pk->position = $pos->asVector3(); - $this->addChunkPacket($pos->getFloorX() >> 4, $pos->getFloorZ() >> 4, $pk); + $this->broadcastPacketToViewers($pos, $pk); } public function getAutoSave() : bool{ @@ -611,6 +611,16 @@ class Level implements ChunkManager, Metadatable{ } } + /** + * Broadcasts a packet to every player who has the target position within their view distance. + * + * @param Vector3 $pos + * @param DataPacket $packet + */ + public function broadcastPacketToViewers(Vector3 $pos, DataPacket $packet) : void{ + $this->addChunkPacket($pos->getFloorX() >> 4, $pos->getFloorZ() >> 4, $packet); + } + /** * Queues a DataPacket to be sent to everyone in the Level at the end of the current tick. * diff --git a/src/pocketmine/tile/Spawnable.php b/src/pocketmine/tile/Spawnable.php index 76c3f3c64..5f7802b31 100644 --- a/src/pocketmine/tile/Spawnable.php +++ b/src/pocketmine/tile/Spawnable.php @@ -67,8 +67,7 @@ abstract class Spawnable extends Tile{ return; } - $pk = $this->createSpawnPacket(); - $this->level->addChunkPacket($this->getFloorX() >> 4, $this->getFloorZ() >> 4, $pk); + $this->level->broadcastPacketToViewers($this, $this->createSpawnPacket()); } /**