First look at shared EntityEventBroadcaster,

this improves performance in PvP servers and other areas where lots of players or entities exist in one space.

fixes #5622
This commit is contained in:
Dylan K. Taylor
2023-03-15 18:17:45 +00:00
parent e7e19abe85
commit cc8660629b
13 changed files with 393 additions and 170 deletions

View File

@ -50,6 +50,8 @@ use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\network\mcpe\EntityEventBroadcaster;
use pocketmine\network\mcpe\NetworkBroadcastUtils;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
@ -143,13 +145,10 @@ abstract class Living extends Entity{
$this->armorInventory = new ArmorInventory($this);
//TODO: load/save armor inventory contents
$this->armorInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(
function(Inventory $unused) : void{
foreach($this->getViewers() as $viewer){
$viewer->getNetworkSession()->onMobArmorChange($this);
}
}
));
$this->armorInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(fn() => NetworkBroadcastUtils::broadcastEntityEvent(
$this->getViewers(),
fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobArmorChange($recipients, $this)
)));
$health = $this->getMaxHealth();
@ -850,7 +849,8 @@ abstract class Living extends Entity{
protected function sendSpawnPacket(Player $player) : void{
parent::sendSpawnPacket($player);
$player->getNetworkSession()->onMobArmorChange($this);
$networkSession = $player->getNetworkSession();
$networkSession->getEntityEventBroadcaster()->onMobArmorChange([$networkSession], $this);
}
protected function syncNetworkData(EntityMetadataCollection $properties) : void{