diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index c515bca71..3cd91289e 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -58,6 +58,7 @@ use pocketmine\nbt\tag\FloatTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\StringTag; +use pocketmine\network\mcpe\protocol\AddEntityPacket; use pocketmine\network\mcpe\protocol\MoveEntityPacket; use pocketmine\network\mcpe\protocol\RemoveEntityPacket; use pocketmine\network\mcpe\protocol\SetEntityDataPacket; @@ -864,9 +865,29 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ public function spawnTo(Player $player){ if(!isset($this->hasSpawned[$player->getLoaderId()]) and isset($player->usedChunks[Level::chunkHash($this->chunk->getX(), $this->chunk->getZ())])){ $this->hasSpawned[$player->getLoaderId()] = $player; + + $this->sendSpawnPacket($player); } } + /** + * Called by spawnTo() to send whatever packets needed to spawn the entity to the client. + * @param Player $player + */ + protected function sendSpawnPacket(Player $player) : void{ + $pk = new AddEntityPacket(); + $pk->entityRuntimeId = $this->getId(); + $pk->type = static::NETWORK_ID; + $pk->position = $this->asVector3(); + $pk->motion = $this->getMotion(); + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->metadata = $this->dataProperties; + + $player->dataPacket($pk); + } + + /** * @deprecated * diff --git a/src/pocketmine/entity/FallingSand.php b/src/pocketmine/entity/FallingSand.php index 2773608df..efb11fe61 100644 --- a/src/pocketmine/entity/FallingSand.php +++ b/src/pocketmine/entity/FallingSand.php @@ -31,8 +31,6 @@ use pocketmine\item\ItemFactory; use pocketmine\level\Position; use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\IntTag; -use pocketmine\network\mcpe\protocol\AddEntityPacket; -use pocketmine\Player; class FallingSand extends Entity{ const NETWORK_ID = self::FALLING_BLOCK; @@ -136,18 +134,4 @@ class FallingSand extends Entity{ $this->namedtag->TileID = new IntTag("TileID", $this->block->getId()); $this->namedtag->Data = new ByteTag("Data", $this->block->getDamage()); } - - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->type = FallingSand::NETWORK_ID; - $pk->entityRuntimeId = $this->getId(); - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } } diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 01d36a3d8..689bf715d 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -512,30 +512,32 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ } public function spawnTo(Player $player){ - if($player !== $this and !isset($this->hasSpawned[$player->getLoaderId()])){ - $this->hasSpawned[$player->getLoaderId()] = $player; + if($player !== $this){ + parent::spawnTo($player); + } + } - if(!$this->skin->isValid()){ - throw new \InvalidStateException((new \ReflectionClass($this))->getShortName() . " must have a valid skin set"); - } + protected function sendSpawnPacket(Player $player) : void{ + if(!$this->skin->isValid()){ + throw new \InvalidStateException((new \ReflectionClass($this))->getShortName() . " must have a valid skin set"); + } - $pk = new AddPlayerPacket(); - $pk->uuid = $this->getUniqueId(); - $pk->username = $this->getName(); - $pk->entityRuntimeId = $this->getId(); - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->item = $this->getInventory()->getItemInHand(); - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); + $pk = new AddPlayerPacket(); + $pk->uuid = $this->getUniqueId(); + $pk->username = $this->getName(); + $pk->entityRuntimeId = $this->getId(); + $pk->position = $this->asVector3(); + $pk->motion = $this->getMotion(); + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->item = $this->getInventory()->getItemInHand(); + $pk->metadata = $this->dataProperties; + $player->dataPacket($pk); - $this->inventory->sendArmorContents($player); + $this->inventory->sendArmorContents($player); - if(!($this instanceof Player)){ - $this->sendSkin([$player]); - } + if(!($this instanceof Player)){ + $this->sendSkin([$player]); } } diff --git a/src/pocketmine/entity/Item.php b/src/pocketmine/entity/Item.php index 632e8b573..90c4ef0ab 100644 --- a/src/pocketmine/entity/Item.php +++ b/src/pocketmine/entity/Item.php @@ -203,15 +203,14 @@ class Item extends Entity{ $this->thrower = $thrower; } - public function spawnTo(Player $player){ + protected function sendSpawnPacket(Player $player) : void{ $pk = new AddItemEntityPacket(); $pk->entityRuntimeId = $this->getId(); $pk->position = $this->asVector3(); $pk->motion = $this->getMotion(); $pk->item = $this->getItem(); $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - parent::spawnTo($player); + $player->dataPacket($pk); } } diff --git a/src/pocketmine/entity/PrimedTNT.php b/src/pocketmine/entity/PrimedTNT.php index 5c54748e1..43ffc141d 100644 --- a/src/pocketmine/entity/PrimedTNT.php +++ b/src/pocketmine/entity/PrimedTNT.php @@ -27,9 +27,7 @@ use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\ExplosionPrimeEvent; use pocketmine\level\Explosion; use pocketmine\nbt\tag\ByteTag; -use pocketmine\network\mcpe\protocol\AddEntityPacket; use pocketmine\network\mcpe\protocol\LevelEventPacket; -use pocketmine\Player; class PrimedTNT extends Entity implements Explosive{ const NETWORK_ID = self::TNT; @@ -112,16 +110,4 @@ class PrimedTNT extends Entity implements Explosive{ $explosion->explodeB(); } } - - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->type = PrimedTNT::NETWORK_ID; - $pk->entityRuntimeId = $this->getId(); - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } } diff --git a/src/pocketmine/entity/Squid.php b/src/pocketmine/entity/Squid.php index e09069278..eb6257523 100644 --- a/src/pocketmine/entity/Squid.php +++ b/src/pocketmine/entity/Squid.php @@ -28,9 +28,7 @@ use pocketmine\event\entity\EntityDamageEvent; use pocketmine\item\Item as ItemItem; use pocketmine\item\ItemFactory; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\AddEntityPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket; -use pocketmine\Player; class Squid extends WaterAnimal{ const NETWORK_ID = self::SQUID; @@ -126,21 +124,6 @@ class Squid extends WaterAnimal{ } } - - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->entityRuntimeId = $this->getId(); - $pk->type = Squid::NETWORK_ID; - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } - public function getDrops() : array{ return [ ItemFactory::get(ItemItem::DYE, 0, mt_rand(1, 3)) diff --git a/src/pocketmine/entity/Villager.php b/src/pocketmine/entity/Villager.php index 6e65a603f..f50a6a979 100644 --- a/src/pocketmine/entity/Villager.php +++ b/src/pocketmine/entity/Villager.php @@ -24,8 +24,6 @@ declare(strict_types=1); namespace pocketmine\entity; use pocketmine\nbt\tag\IntTag; -use pocketmine\network\mcpe\protocol\AddEntityPacket; -use pocketmine\Player; class Villager extends Creature implements NPC, Ageable{ const PROFESSION_FARMER = 0; @@ -61,20 +59,6 @@ class Villager extends Creature implements NPC, Ageable{ $this->namedtag->Profession = new IntTag("Profession", $this->getProfession()); } - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->entityRuntimeId = $this->getId(); - $pk->type = Villager::NETWORK_ID; - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } - /** * Sets the villager profession * diff --git a/src/pocketmine/entity/Zombie.php b/src/pocketmine/entity/Zombie.php index c7da708b9..e94add842 100644 --- a/src/pocketmine/entity/Zombie.php +++ b/src/pocketmine/entity/Zombie.php @@ -26,7 +26,6 @@ namespace pocketmine\entity; use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\item\Item as ItemItem; use pocketmine\item\ItemFactory; -use pocketmine\network\mcpe\protocol\AddEntityPacket; use pocketmine\Player; class Zombie extends Monster{ @@ -39,20 +38,6 @@ class Zombie extends Monster{ return "Zombie"; } - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->entityRuntimeId = $this->getId(); - $pk->type = Zombie::NETWORK_ID; - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } - public function getDrops() : array{ $drops = [ ItemFactory::get(ItemItem::FEATHER, 0, 1) diff --git a/src/pocketmine/entity/projectile/Arrow.php b/src/pocketmine/entity/projectile/Arrow.php index 7f70d77b3..80c262943 100644 --- a/src/pocketmine/entity/projectile/Arrow.php +++ b/src/pocketmine/entity/projectile/Arrow.php @@ -26,8 +26,6 @@ namespace pocketmine\entity\projectile; use pocketmine\entity\Entity; use pocketmine\level\Level; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\network\mcpe\protocol\AddEntityPacket; -use pocketmine\Player; class Arrow extends Projectile{ const NETWORK_ID = self::ARROW; @@ -80,19 +78,4 @@ class Arrow extends Projectile{ return $hasUpdate; } - - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->type = Arrow::NETWORK_ID; - $pk->entityRuntimeId = $this->getId(); - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } } diff --git a/src/pocketmine/entity/projectile/Egg.php b/src/pocketmine/entity/projectile/Egg.php index 1e46988e0..6fdaab89c 100644 --- a/src/pocketmine/entity/projectile/Egg.php +++ b/src/pocketmine/entity/projectile/Egg.php @@ -23,23 +23,8 @@ declare(strict_types=1); namespace pocketmine\entity\projectile; -use pocketmine\network\mcpe\protocol\AddEntityPacket; -use pocketmine\Player; - class Egg extends Throwable{ const NETWORK_ID = self::EGG; - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->type = Egg::NETWORK_ID; - $pk->entityRuntimeId = $this->getId(); - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } - //TODO: spawn chickens on collision } \ No newline at end of file diff --git a/src/pocketmine/entity/projectile/Snowball.php b/src/pocketmine/entity/projectile/Snowball.php index 009c3da25..6caf543cc 100644 --- a/src/pocketmine/entity/projectile/Snowball.php +++ b/src/pocketmine/entity/projectile/Snowball.php @@ -23,21 +23,7 @@ declare(strict_types=1); namespace pocketmine\entity\projectile; -use pocketmine\network\mcpe\protocol\AddEntityPacket; -use pocketmine\Player; - class Snowball extends Throwable{ const NETWORK_ID = self::SNOWBALL; - public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); - $pk->type = Snowball::NETWORK_ID; - $pk->entityRuntimeId = $this->getId(); - $pk->position = $this->asVector3(); - $pk->motion = $this->getMotion(); - $pk->metadata = $this->dataProperties; - $player->dataPacket($pk); - - parent::spawnTo($player); - } }