Cleaned up Entity->spawnTo()

This commit is contained in:
Dylan K. Taylor 2017-10-19 16:13:09 +01:00
parent 2b22d5d8cc
commit 9fb93985d6
11 changed files with 45 additions and 147 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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