PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
NetworkBroadcastUtils.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe;
25
29use function count;
30use function spl_object_id;
31
33
34 private function __construct(){
35 //NOOP
36 }
37
42 public static function broadcastPackets(array $recipients, array $packets) : bool{
43 if(count($packets) === 0){
44 throw new \InvalidArgumentException("Cannot broadcast empty list of packets");
45 }
46
47 return Timings::$broadcastPackets->time(function() use ($recipients, $packets) : bool{
49 $sessions = [];
50 foreach($recipients as $player){
51 if($player->isConnected()){
52 $sessions[] = $player->getNetworkSession();
53 }
54 }
55 if(count($sessions) === 0){
56 return false;
57 }
58
60 $uniqueBroadcasters = [];
62 $broadcasterTargets = [];
63 foreach($sessions as $recipient){
64 $broadcaster = $recipient->getBroadcaster();
65 $uniqueBroadcasters[spl_object_id($broadcaster)] = $broadcaster;
66 $broadcasterTargets[spl_object_id($broadcaster)][spl_object_id($recipient)] = $recipient;
67 }
68 foreach($uniqueBroadcasters as $broadcaster){
69 $broadcaster->broadcastPackets($broadcasterTargets[spl_object_id($broadcaster)], $packets);
70 }
71
72 return true;
73 });
74 }
75
80 public static function broadcastEntityEvent(array $recipients, \Closure $callback) : void{
81 $uniqueBroadcasters = [];
82 $broadcasterTargets = [];
83
84 foreach($recipients as $recipient){
85 $session = $recipient->getNetworkSession();
86 $broadcaster = $session->getEntityEventBroadcaster();
87 $uniqueBroadcasters[spl_object_id($broadcaster)] = $broadcaster;
88 $broadcasterTargets[spl_object_id($broadcaster)][spl_object_id($session)] = $session;
89 }
90
91 foreach($uniqueBroadcasters as $k => $broadcaster){
92 $callback($broadcaster, $broadcasterTargets[$k]);
93 }
94 }
95}
static broadcastEntityEvent(array $recipients, \Closure $callback)
static broadcastPackets(array $recipients, array $packets)