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\ListTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\network\mcpe\protocol\MoveEntityPacket; use pocketmine\network\mcpe\protocol\MoveEntityPacket;
use pocketmine\network\mcpe\protocol\RemoveEntityPacket; use pocketmine\network\mcpe\protocol\RemoveEntityPacket;
use pocketmine\network\mcpe\protocol\SetEntityDataPacket; use pocketmine\network\mcpe\protocol\SetEntityDataPacket;
@ -864,9 +865,29 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
public function spawnTo(Player $player){ public function spawnTo(Player $player){
if(!isset($this->hasSpawned[$player->getLoaderId()]) and isset($player->usedChunks[Level::chunkHash($this->chunk->getX(), $this->chunk->getZ())])){ if(!isset($this->hasSpawned[$player->getLoaderId()]) and isset($player->usedChunks[Level::chunkHash($this->chunk->getX(), $this->chunk->getZ())])){
$this->hasSpawned[$player->getLoaderId()] = $player; $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 * @deprecated
* *

View File

@ -31,8 +31,6 @@ use pocketmine\item\ItemFactory;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\Player;
class FallingSand extends Entity{ class FallingSand extends Entity{
const NETWORK_ID = self::FALLING_BLOCK; 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->TileID = new IntTag("TileID", $this->block->getId());
$this->namedtag->Data = new ByteTag("Data", $this->block->getDamage()); $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,9 +512,12 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
} }
public function spawnTo(Player $player){ public function spawnTo(Player $player){
if($player !== $this and !isset($this->hasSpawned[$player->getLoaderId()])){ if($player !== $this){
$this->hasSpawned[$player->getLoaderId()] = $player; parent::spawnTo($player);
}
}
protected function sendSpawnPacket(Player $player) : void{
if(!$this->skin->isValid()){ if(!$this->skin->isValid()){
throw new \InvalidStateException((new \ReflectionClass($this))->getShortName() . " must have a valid skin set"); throw new \InvalidStateException((new \ReflectionClass($this))->getShortName() . " must have a valid skin set");
} }
@ -537,7 +540,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->sendSkin([$player]); $this->sendSkin([$player]);
} }
} }
}
public function close(){ public function close(){
if(!$this->closed){ if(!$this->closed){

View File

@ -203,15 +203,14 @@ class Item extends Entity{
$this->thrower = $thrower; $this->thrower = $thrower;
} }
public function spawnTo(Player $player){ protected function sendSpawnPacket(Player $player) : void{
$pk = new AddItemEntityPacket(); $pk = new AddItemEntityPacket();
$pk->entityRuntimeId = $this->getId(); $pk->entityRuntimeId = $this->getId();
$pk->position = $this->asVector3(); $pk->position = $this->asVector3();
$pk->motion = $this->getMotion(); $pk->motion = $this->getMotion();
$pk->item = $this->getItem(); $pk->item = $this->getItem();
$pk->metadata = $this->dataProperties; $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\event\entity\ExplosionPrimeEvent;
use pocketmine\level\Explosion; use pocketmine\level\Explosion;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\network\mcpe\protocol\LevelEventPacket; use pocketmine\network\mcpe\protocol\LevelEventPacket;
use pocketmine\Player;
class PrimedTNT extends Entity implements Explosive{ class PrimedTNT extends Entity implements Explosive{
const NETWORK_ID = self::TNT; const NETWORK_ID = self::TNT;
@ -112,16 +110,4 @@ class PrimedTNT extends Entity implements Explosive{
$explosion->explodeB(); $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\Item as ItemItem;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket;
use pocketmine\Player;
class Squid extends WaterAnimal{ class Squid extends WaterAnimal{
const NETWORK_ID = self::SQUID; 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{ public function getDrops() : array{
return [ return [
ItemFactory::get(ItemItem::DYE, 0, mt_rand(1, 3)) ItemFactory::get(ItemItem::DYE, 0, mt_rand(1, 3))

View File

@ -24,8 +24,6 @@ declare(strict_types=1);
namespace pocketmine\entity; namespace pocketmine\entity;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\Player;
class Villager extends Creature implements NPC, Ageable{ class Villager extends Creature implements NPC, Ageable{
const PROFESSION_FARMER = 0; const PROFESSION_FARMER = 0;
@ -61,20 +59,6 @@ class Villager extends Creature implements NPC, Ageable{
$this->namedtag->Profession = new IntTag("Profession", $this->getProfession()); $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 * Sets the villager profession
* *

View File

@ -26,7 +26,6 @@ namespace pocketmine\entity;
use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\item\Item as ItemItem; use pocketmine\item\Item as ItemItem;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\Player; use pocketmine\Player;
class Zombie extends Monster{ class Zombie extends Monster{
@ -39,20 +38,6 @@ class Zombie extends Monster{
return "Zombie"; 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{ public function getDrops() : array{
$drops = [ $drops = [
ItemFactory::get(ItemItem::FEATHER, 0, 1) ItemFactory::get(ItemItem::FEATHER, 0, 1)

View File

@ -26,8 +26,6 @@ namespace pocketmine\entity\projectile;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\Player;
class Arrow extends Projectile{ class Arrow extends Projectile{
const NETWORK_ID = self::ARROW; const NETWORK_ID = self::ARROW;
@ -80,19 +78,4 @@ class Arrow extends Projectile{
return $hasUpdate; 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; namespace pocketmine\entity\projectile;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\Player;
class Egg extends Throwable{ class Egg extends Throwable{
const NETWORK_ID = self::EGG; 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 //TODO: spawn chickens on collision
} }

View File

@ -23,21 +23,7 @@ declare(strict_types=1);
namespace pocketmine\entity\projectile; namespace pocketmine\entity\projectile;
use pocketmine\network\mcpe\protocol\AddEntityPacket;
use pocketmine\Player;
class Snowball extends Throwable{ class Snowball extends Throwable{
const NETWORK_ID = self::SNOWBALL; 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);
}
} }