diff --git a/src/world/World.php b/src/world/World.php index c59d143a6..f8b9547eb 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -630,6 +630,28 @@ class World implements ChunkManager{ 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 $allowed + * + * @return array + */ + 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 */ @@ -641,7 +663,7 @@ class World implements ChunkManager{ $this->broadcastPacketToViewers($pos, $e); } }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); } }else{ - $this->server->broadcastPackets($players, $pk); + $this->server->broadcastPackets($this->filterViewersForPosition($pos, $players), $pk); } } }