From 5df56a1baca6c8daa2679e0f9467865e8e35039c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 5 Aug 2018 12:39:36 +0100 Subject: [PATCH] Clean up some AnimatePacket boilerplate code --- src/pocketmine/Player.php | 27 +++++++++++---------------- src/pocketmine/entity/Entity.php | 8 ++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 7f8887907..03346f1c4 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1190,10 +1190,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->level->setSleepTicks(0); - $pk = new AnimatePacket(); - $pk->entityRuntimeId = $this->id; - $pk->action = AnimatePacket::ACTION_STOP_SLEEP; - $this->sendDataPacket($pk); + $this->broadcastAnimation([$this], AnimatePacket::ACTION_STOP_SLEEP); } } @@ -2349,13 +2346,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } if($ev->getModifier(EntityDamageEvent::MODIFIER_CRITICAL) > 0){ - $pk = new AnimatePacket(); - $pk->action = AnimatePacket::ACTION_CRITICAL_HIT; - $pk->entityRuntimeId = $entity->getId(); - $this->server->broadcastPacket($entity->getViewers(), $pk); - if($entity instanceof Player){ - $entity->sendDataPacket($pk); - } + $entity->broadcastAnimation(null, AnimatePacket::ACTION_CRITICAL_HIT); } foreach($meleeEnchantments as $enchantment){ @@ -2416,11 +2407,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ return true; } - $pk = new AnimatePacket(); - $pk->entityRuntimeId = $this->getId(); - $pk->action = $ev->getAnimationType(); - $this->server->broadcastPacket($this->getViewers(), $pk); - + $this->broadcastAnimation($this->getViewers(), $ev->getAnimationType()); return true; } @@ -3088,6 +3075,14 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ parent::broadcastEntityEvent($eventId, $eventData, $players); } + public function broadcastAnimation(?array $players, int $animationId) : void{ + if($this->spawned and $players === null){ + $players = $this->getViewers(); + $players[] = $this; + } + parent::broadcastAnimation($players, $animationId); + } + public function getOffsetPosition(Vector3 $vector3) : Vector3{ $result = parent::getOffsetPosition($vector3); $result->y += 0.001; //Hack for MCPE falling underground for no good reason (TODO: find out why it's doing this) diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 16ea36795..847182cc5 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -63,6 +63,7 @@ use pocketmine\nbt\tag\FloatTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\StringTag; use pocketmine\network\mcpe\protocol\AddEntityPacket; +use pocketmine\network\mcpe\protocol\AnimatePacket; use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\MoveEntityAbsolutePacket; use pocketmine\network\mcpe\protocol\RemoveEntityPacket; @@ -2118,6 +2119,13 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ $this->server->broadcastPacket($players ?? $this->getViewers(), $pk); } + public function broadcastAnimation(?array $players, int $animationId) : void{ + $pk = new AnimatePacket(); + $pk->entityRuntimeId = $this->id; + $pk->action = $animationId; + $this->server->broadcastPacket($players ?? $this->getViewers(), $pk); + } + public function __destruct(){ $this->close(); }