Level: Added API method broadcastPacketToViewers()

This supersedes addChunkPacket() in most cases, and has a more clear name. It broadcasts the given packet to every player who has the target position within their chunk load radius.
This commit is contained in:
Dylan K. Taylor 2018-10-20 15:14:41 +01:00
parent 7c44eea625
commit d563b9e31b
6 changed files with 35 additions and 32 deletions

View File

@ -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;
}

View File

@ -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{

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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.
*

View File

@ -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());
}
/**