Clean up some AnimatePacket boilerplate code

This commit is contained in:
Dylan K. Taylor 2018-08-05 12:39:36 +01:00
parent d81388f62c
commit 5df56a1bac
2 changed files with 19 additions and 16 deletions

View File

@ -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)

View File

@ -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();
}