World: ensure that addParticle/addSound don't send stuff to players who are not in range, even when an array of targets is given

closes #5347
This commit is contained in:
Dylan K. Taylor 2022-10-16 16:45:36 +01:00
parent 572def9245
commit cd04a3db2e
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -630,6 +630,28 @@ class World implements ChunkManager{
unset($this->unloadCallbacks[spl_object_id($callback)]); unset($this->unloadCallbacks[spl_object_id($callback)]);
} }
/**
* Returns a list of players who are in the given filter and also using the chunk containing the target position.
* Used for broadcasting sounds and particles with specific targets.
*
* @param Player[] $allowed
* @phpstan-param list<Player> $allowed
*
* @return array<int, Player>
*/
private function filterViewersForPosition(Vector3 $pos, array $allowed) : array{
$candidates = $this->getViewersForPosition($pos);
$filtered = [];
foreach($allowed as $player){
$k = spl_object_id($player);
if(isset($candidates[$k])){
$filtered[$k] = $candidates[$k];
}
}
return $filtered;
}
/** /**
* @param Player[]|null $players * @param Player[]|null $players
*/ */
@ -641,7 +663,7 @@ class World implements ChunkManager{
$this->broadcastPacketToViewers($pos, $e); $this->broadcastPacketToViewers($pos, $e);
} }
}else{ }else{
$this->server->broadcastPackets($players, $pk); $this->server->broadcastPackets($this->filterViewersForPosition($pos, $players), $pk);
} }
} }
} }
@ -657,7 +679,7 @@ class World implements ChunkManager{
$this->broadcastPacketToViewers($pos, $e); $this->broadcastPacketToViewers($pos, $e);
} }
}else{ }else{
$this->server->broadcastPackets($players, $pk); $this->server->broadcastPackets($this->filterViewersForPosition($pos, $players), $pk);
} }
} }
} }