Entity: add abstract getNetworkTypeId(), remove NETWORK_ID constant

this now requires that subclasses supply a proper NETWORK_ID.
This commit is contained in:
Dylan K. Taylor 2020-05-16 16:08:12 +01:00
parent 67666db827
commit c30dd9f1b6
21 changed files with 34 additions and 32 deletions

View File

@ -80,8 +80,6 @@ abstract class Entity{
public const MOTION_THRESHOLD = 0.00001;
public const NETWORK_ID = -1;
/** @var Player[] */
protected $hasSpawned = [];
@ -1506,13 +1504,15 @@ abstract class Entity{
return $this->hasSpawned;
}
abstract public static function getNetworkTypeId() : int;
/**
* Called by spawnTo() to send whatever packets needed to spawn the entity to the client.
*/
protected function sendSpawnPacket(Player $player) : void{
$pk = new AddActorPacket();
$pk->entityRuntimeId = $this->getId();
$pk->type = LegacyEntityIdToStringIdMap::getInstance()->legacyToString(static::NETWORK_ID);
$pk->type = LegacyEntityIdToStringIdMap::getInstance()->legacyToString(static::getNetworkTypeId());
$pk->position = $this->location->asVector3();
$pk->motion = $this->getMotion();
$pk->yaw = $this->location->yaw;

View File

@ -66,6 +66,10 @@ use function strlen;
class Human extends Living implements ProjectileSource, InventoryHolder{
public static function getNetworkTypeId() : int{
return -1; //TODO: ideally we shouldn't have to specify this at all here ...
}
/** @var PlayerInventory */
protected $inventory;

View File

@ -36,7 +36,8 @@ use function sqrt;
use const M_PI;
class Squid extends WaterAnimal{
public const NETWORK_ID = EntityLegacyIds::SQUID;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::SQUID; }
public $width = 0.95;
public $height = 0.95;

View File

@ -35,7 +35,7 @@ class Villager extends Living implements Ageable{
public const PROFESSION_BLACKSMITH = 3;
public const PROFESSION_BUTCHER = 4;
public const NETWORK_ID = EntityLegacyIds::VILLAGER;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::VILLAGER; }
public $width = 0.6;
public $height = 1.8;

View File

@ -28,7 +28,8 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use function mt_rand;
class Zombie extends Living{
public const NETWORK_ID = EntityLegacyIds::ZOMBIE;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::ZOMBIE; }
public $width = 0.6;
public $height = 1.8;

View File

@ -31,10 +31,12 @@ use pocketmine\nbt\tag\ShortTag;
use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\player\Player;
use function realpath;
use function sqrt;
class ExperienceOrb extends Entity{
public const NETWORK_ID = EntityLegacyIds::XP_ORB;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::XP_ORB; }
public const TAG_VALUE_PC = "Value"; //short
public const TAG_VALUE_PE = "experience value"; //int (WTF?)

View File

@ -38,7 +38,8 @@ use function abs;
use function get_class;
class FallingBlock extends Entity{
public const NETWORK_ID = EntityLegacyIds::FALLING_BLOCK;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::FALLING_BLOCK; }
public $width = 0.98;
public $height = 0.98;

View File

@ -37,7 +37,8 @@ use function get_class;
use function max;
class ItemEntity extends Entity{
public const NETWORK_ID = EntityLegacyIds::ITEM;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::ITEM; }
public const DEFAULT_DESPAWN_DELAY = 6000; //5 minutes
public const NEVER_DESPAWN = -1;

View File

@ -40,7 +40,7 @@ use pocketmine\world\World;
use function ceil;
class Painting extends Entity{
public const NETWORK_ID = EntityLegacyIds::PAINTING;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::PAINTING; }
private const DATA_TO_FACING = [
0 => Facing::SOUTH,

View File

@ -36,7 +36,8 @@ use pocketmine\world\Position;
use pocketmine\world\sound\IgniteSound;
class PrimedTNT extends Entity implements Explosive{
public const NETWORK_ID = EntityLegacyIds::TNT;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::TNT; }
public $width = 0.98;
public $height = 0.98;

View File

@ -40,7 +40,8 @@ use function mt_rand;
use function sqrt;
class Arrow extends Projectile{
public const NETWORK_ID = EntityLegacyIds::ARROW;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::ARROW; }
public const PICKUP_NONE = 0;
public const PICKUP_ANY = 1;

View File

@ -29,7 +29,7 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\world\particle\ItemBreakParticle;
class Egg extends Throwable{
public const NETWORK_ID = EntityLegacyIds::EGG;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::EGG; }
//TODO: spawn chickens on collision

View File

@ -30,7 +30,7 @@ use pocketmine\world\particle\EndermanTeleportParticle;
use pocketmine\world\sound\EndermanTeleportSound;
class EnderPearl extends Throwable{
public const NETWORK_ID = EntityLegacyIds::ENDER_PEARL;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::ENDER_PEARL; }
protected function onHit(ProjectileHitEvent $event) : void{
$owner = $this->getOwningEntity();

View File

@ -30,7 +30,7 @@ use pocketmine\world\sound\PotionSplashSound;
use function mt_rand;
class ExperienceBottle extends Throwable{
public const NETWORK_ID = EntityLegacyIds::XP_BOTTLE;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::XP_BOTTLE; }
protected $gravity = 0.07;

View File

@ -28,7 +28,7 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds;
use pocketmine\world\particle\SnowballPoofParticle;
class Snowball extends Throwable{
public const NETWORK_ID = EntityLegacyIds::SNOWBALL;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::SNOWBALL; }
protected function onHit(ProjectileHitEvent $event) : void{
for($i = 0; $i < 6; ++$i){

View File

@ -45,7 +45,7 @@ use function sqrt;
class SplashPotion extends Throwable{
public const NETWORK_ID = EntityLegacyIds::SPLASH_POTION;
public static function getNetworkTypeId() : int{ return EntityLegacyIds::SPLASH_POTION; }
protected $gravity = 0.05;
protected $drag = 0.01;

View File

@ -252,8 +252,8 @@ class ItemFactory{
foreach(EntityFactory::getInstance()->getKnownTypes() as $className){
/** @var Living|string $className */
if(is_a($className, Living::class, true) and $className::NETWORK_ID !== -1){
$this->register(new SpawnEgg(ItemIds::SPAWN_EGG, $className::NETWORK_ID, "Spawn Egg", $className));
if(is_a($className, Living::class, true) and $className::getNetworkTypeId() !== -1){
$this->register(new SpawnEgg(ItemIds::SPAWN_EGG, $className::getNetworkTypeId(), "Spawn Egg", $className));
}
}

View File

@ -50,7 +50,7 @@ class EntityLandSound implements Sound{
LevelSoundEventPacket::SOUND_LAND,
$pos,
$this->blockLandedOn->getRuntimeId(),
$this->entity instanceof Player ? "minecraft:player" : LegacyEntityIdToStringIdMap::getInstance()->legacyToString($this->entity::NETWORK_ID) //TODO: bad hack, stuff depends on players having a -1 network ID :(
$this->entity instanceof Player ? "minecraft:player" : LegacyEntityIdToStringIdMap::getInstance()->legacyToString($this->entity::getNetworkTypeId()) //TODO: bad hack, stuff depends on players having a -1 network ID :(
//TODO: does isBaby have any relevance here?
);
}

View File

@ -47,7 +47,7 @@ class EntityLongFallSound implements Sound{
LevelSoundEventPacket::SOUND_FALL_BIG,
$pos,
-1,
$this->entity instanceof Player ? "minecraft:player" : LegacyEntityIdToStringIdMap::getInstance()->legacyToString($this->entity::NETWORK_ID) //TODO: bad hack, stuff depends on players having a -1 network ID :(
$this->entity instanceof Player ? "minecraft:player" : LegacyEntityIdToStringIdMap::getInstance()->legacyToString($this->entity::getNetworkTypeId()) //TODO: bad hack, stuff depends on players having a -1 network ID :(
//TODO: is isBaby relevant here?
);
}

View File

@ -46,7 +46,7 @@ class EntityShortFallSound implements Sound{
LevelSoundEventPacket::SOUND_FALL_SMALL,
$pos,
-1,
$this->entity instanceof Player ? "minecraft:player" : LegacyEntityIdToStringIdMap::getInstance()->legacyToString($this->entity::NETWORK_ID) //TODO: bad hack, stuff depends on players having a -1 network ID :(
$this->entity instanceof Player ? "minecraft:player" : LegacyEntityIdToStringIdMap::getInstance()->legacyToString($this->entity::getNetworkTypeId()) //TODO: bad hack, stuff depends on players having a -1 network ID :(
//TODO: does isBaby have any relevance here?
);
}

View File

@ -30,16 +30,6 @@ parameters:
count: 1
path: ../../../src/entity/projectile/Projectile.php
-
message: "#^If condition is always false\\.$#"
count: 1
path: ../../../src/item/ItemFactory.php
-
message: "#^Strict comparison using \\!\\=\\= between \\-1 and \\-1 will always evaluate to false\\.$#"
count: 1
path: ../../../src/item/ItemFactory.php
-
message: "#^If condition is always false\\.$#"
count: 1