From d1b28ce17a1a6b4bcbbdf7952469f612e3046a92 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 29 Apr 2020 16:45:09 +0100 Subject: [PATCH] NetworkSession: allow sending an arbitrary set of attributes for an entity --- src/network/mcpe/NetworkSession.php | 10 ++++++---- src/network/mcpe/handler/PreSpawnPacketHandler.php | 2 +- src/player/Player.php | 5 +++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/network/mcpe/NetworkSession.php b/src/network/mcpe/NetworkSession.php index 5c4075eac..0017f764e 100644 --- a/src/network/mcpe/NetworkSession.php +++ b/src/network/mcpe/NetworkSession.php @@ -680,12 +680,14 @@ class NetworkSession{ $this->sendDataPacket($pk); } - public function syncAttributes(Living $entity, bool $sendAll = false) : void{ - $entries = $sendAll ? $entity->getAttributeMap()->getAll() : $entity->getAttributeMap()->needSend(); - if(count($entries) > 0){ + /** + * @param Attribute[] $attributes + */ + public function syncAttributes(Living $entity, array $attributes) : void{ + if(count($attributes) > 0){ $this->sendDataPacket(UpdateAttributesPacket::create($entity->getId(), array_map(function(Attribute $attr) : NetworkAttribute{ return new NetworkAttribute($attr->getId(), $attr->getMinValue(), $attr->getMaxValue(), $attr->getValue(), $attr->getDefaultValue()); - }, $entries))); + }, $attributes))); } } diff --git a/src/network/mcpe/handler/PreSpawnPacketHandler.php b/src/network/mcpe/handler/PreSpawnPacketHandler.php index 726cdb7de..f475d5370 100644 --- a/src/network/mcpe/handler/PreSpawnPacketHandler.php +++ b/src/network/mcpe/handler/PreSpawnPacketHandler.php @@ -86,7 +86,7 @@ class PreSpawnPacketHandler extends PacketHandler{ $this->player->setImmobile(); //HACK: fix client-side falling pre-spawn - $this->session->syncAttributes($this->player, true); + $this->session->syncAttributes($this->player, $this->player->getAttributeMap()->getAll()); $this->session->syncAvailableCommands(); $this->session->syncAdventureSettings($this->player); foreach($this->player->getEffects()->all() as $effect){ diff --git a/src/player/Player.php b/src/player/Player.php index 1d4f8169a..0730fb164 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -1291,8 +1291,9 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, $this->lastUpdate = $currentTick; //TODO: move this to network session ticking (this is specifically related to net sync) - $this->networkSession->syncAttributes($this); - foreach($this->attributeMap->getAll() as $attribute){ + $dirtyAttributes = $this->attributeMap->needSend(); + $this->networkSession->syncAttributes($this, $dirtyAttributes); + foreach($dirtyAttributes as $attribute){ $attribute->markSynchronized(); }