From 306f492fc073ca1e5608787299cf4c8e318c2ebe Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 9 Dec 2014 01:36:46 +0100 Subject: [PATCH] Added Player death animations, improved spawning behavior to correct invisible players, fixed players getting stuck when dead, closes #2304 --- src/pocketmine/Player.php | 30 +++++++++++++---------------- src/pocketmine/entity/Entity.php | 21 +++++++++++++------- src/pocketmine/entity/Living.php | 9 +++++---- src/pocketmine/nbt/tag/Compound.php | 4 ++++ 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 227a51e1f..f56a4029e 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -251,20 +251,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ * @param Player $player */ public function spawnTo(Player $player){ - if($this->spawned === true and $this->dead !== true and $this !== $player and $player->getLevel() === $this->level and $player->canSee($this)){ + if($this->spawned === true and $this->dead !== true and $player->dead !== true and $player->getLevel() === $this->level and $player->canSee($this)){ parent::spawnTo($player); } } - /** - * @param Player $player - */ - public function despawnFrom(Player $player){ - if($this->spawned === true and $this->dead !== true){ - parent::despawnFrom($player); - } - } - /** * @return Server */ @@ -1198,8 +1189,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } public function onUpdate($currentTick){ - if($this->dead === true){ - return true; + if($this->dead === true and $this->spawned){ + ++$this->deadTicks; + if($this->deadTicks >= 10){ + $this->despawnFromAll(); + } + return $this->deadTicks < 10; } $this->timings->startTiming(); @@ -2001,11 +1996,14 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->server->getPluginManager()->callEvent($ev = new PlayerRespawnEvent($this, $this->getSpawn())); $this->teleport($ev->getRespawnPosition()); + $this->fireTicks = 0; $this->airTicks = 300; + $this->deadTicks = 0; $this->setHealth(20); $this->dead = false; + $this->sendMetadata($this->getViewers()); $this->sendMetadata($this); @@ -2013,9 +2011,10 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->inventory->sendContents($this); $this->inventory->sendArmorContents($this); - $this->spawnToAll(); - $this->blocked = false; + + $this->spawnToAll(); + $this->scheduleUpdate(); break; case ProtocolInfo::SET_HEALTH_PACKET: //Not used break; @@ -2565,9 +2564,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($ev->getDeathMessage() != ""){ $this->server->broadcast($ev->getDeathMessage(), Server::BROADCAST_CHANNEL_USERS); } - - $this->despawnFromAll(); - } public function setHealth($amount){ diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 38fcaca4d..0e76a38e2 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -111,6 +111,7 @@ abstract class Entity extends Location implements Metadatable{ public $positionChanged; public $motionChanged; public $dead; + public $deadTicks = 0; protected $age = 0; public $height; @@ -527,12 +528,16 @@ abstract class Entity extends Location implements Metadatable{ $isPlayer = $this instanceof Player; if($this->dead === true){ - $this->despawnFromAll(); - if(!$isPlayer){ - $this->close(); + ++$this->deadTicks; + if($this->deadTicks >= 10){ + $this->despawnFromAll(); + if(!$isPlayer){ + $this->close(); + } } + Timings::$tickEntityTimer->stopTiming(); - return $isPlayer; + return $this->deadTicks < 10; } $hasUpdate = false; @@ -1107,12 +1112,14 @@ abstract class Entity extends Location implements Metadatable{ $this->boundingBox->setBounds($pos->x - $radius, $pos->y + $this->ySize, $pos->z - $radius, $pos->x + $radius, $pos->y + $this->height + $this->ySize, $pos->z + $radius); - if($this->chunk === null or ($this->chunk->getX() !== ($this->x >> 4) and $this->chunk->getZ() !== ($this->z >> 4))){ + if($this->chunk === null or ($this->chunkX !== ($this->x >> 4) and $this->chunkZ !== ($this->z >> 4))){ if($this->chunk instanceof FullChunk){ $this->chunk->removeEntity($this); } $this->level->loadChunk($this->x >> 4, $this->z >> 4); $this->chunk = $this->level->getChunk($this->x >> 4, $this->z >> 4); + $this->chunkX = $this->chunk->getX(); + $this->chunkZ = $this->chunk->getZ(); if(!$this->justCreated){ $newChunk = $this->level->getUsingChunk($this->x >> 4, $this->z >> 4); @@ -1221,8 +1228,8 @@ abstract class Entity extends Location implements Metadatable{ } public function spawnToAll(){ - foreach($this->level->getUsingChunk($this->x >> 4, $this->z >> 4) as $player){ - if(isset($player->id) and $player->spawned === true){ + foreach($this->level->getUsingChunk($this->chunkX, $this->chunkZ) as $player){ + if($player->loggedIn === true){ $this->spawnTo($player); } } diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index bbfe94c5d..424be86f5 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -93,10 +93,6 @@ abstract class Living extends Entity implements Damageable{ } } - $pk = new EntityEventPacket(); - $pk->eid = $this->getId(); - $pk->event = 2; //Ouch! - Server::broadcastPacket($this->hasSpawned, $pk); $this->setLastDamageCause($source); if($source instanceof EntityDamageByEntityEvent){ @@ -109,6 +105,11 @@ abstract class Living extends Entity implements Damageable{ $this->setHealth($this->getHealth() - $damage); + $pk = new EntityEventPacket(); + $pk->eid = $this->getId(); + $pk->event = $this->getHealth() <= 0 ? 3 : 2; //Ouch! + Server::broadcastPacket($this->hasSpawned, $pk); + $this->attackTime = 10; //0.5 seconds cooldown } diff --git a/src/pocketmine/nbt/tag/Compound.php b/src/pocketmine/nbt/tag/Compound.php index 959a27491..a7074e5f0 100644 --- a/src/pocketmine/nbt/tag/Compound.php +++ b/src/pocketmine/nbt/tag/Compound.php @@ -27,6 +27,10 @@ use pocketmine\nbt\NBT; class Compound extends NamedTag implements \ArrayAccess{ + /** + * @param string $name + * @param NamedTag[] $value + */ public function __construct($name = "", $value = []){ $this->name = $name; foreach($value as $tag){