From c30dd9f1b67999c45802f2c73bdc8ee667038a77 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 16 May 2020 16:08:12 +0100 Subject: [PATCH] Entity: add abstract getNetworkTypeId(), remove NETWORK_ID constant this now requires that subclasses supply a proper NETWORK_ID. --- src/entity/Entity.php | 6 +++--- src/entity/Human.php | 4 ++++ src/entity/Squid.php | 3 ++- src/entity/Villager.php | 2 +- src/entity/Zombie.php | 3 ++- src/entity/object/ExperienceOrb.php | 4 +++- src/entity/object/FallingBlock.php | 3 ++- src/entity/object/ItemEntity.php | 3 ++- src/entity/object/Painting.php | 2 +- src/entity/object/PrimedTNT.php | 3 ++- src/entity/projectile/Arrow.php | 3 ++- src/entity/projectile/Egg.php | 2 +- src/entity/projectile/EnderPearl.php | 2 +- src/entity/projectile/ExperienceBottle.php | 2 +- src/entity/projectile/Snowball.php | 2 +- src/entity/projectile/SplashPotion.php | 2 +- src/item/ItemFactory.php | 4 ++-- src/world/sound/EntityLandSound.php | 2 +- src/world/sound/EntityLongFallSound.php | 2 +- src/world/sound/EntityShortFallSound.php | 2 +- tests/phpstan/configs/phpstan-bugs.neon | 10 ---------- 21 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index ba098ed27..29113e209 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -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; diff --git a/src/entity/Human.php b/src/entity/Human.php index 55518fcf4..36762dcdf 100644 --- a/src/entity/Human.php +++ b/src/entity/Human.php @@ -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; diff --git a/src/entity/Squid.php b/src/entity/Squid.php index 29fe795e7..a2bed5329 100644 --- a/src/entity/Squid.php +++ b/src/entity/Squid.php @@ -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; diff --git a/src/entity/Villager.php b/src/entity/Villager.php index b76ff57fd..e784d0c57 100644 --- a/src/entity/Villager.php +++ b/src/entity/Villager.php @@ -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; diff --git a/src/entity/Zombie.php b/src/entity/Zombie.php index a33b191d7..307d9d574 100644 --- a/src/entity/Zombie.php +++ b/src/entity/Zombie.php @@ -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; diff --git a/src/entity/object/ExperienceOrb.php b/src/entity/object/ExperienceOrb.php index c87217795..3ae1888b3 100644 --- a/src/entity/object/ExperienceOrb.php +++ b/src/entity/object/ExperienceOrb.php @@ -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?) diff --git a/src/entity/object/FallingBlock.php b/src/entity/object/FallingBlock.php index 20c7403b1..94e85cf38 100644 --- a/src/entity/object/FallingBlock.php +++ b/src/entity/object/FallingBlock.php @@ -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; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index e4550c699..d933e978d 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -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; diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index 7a5e34102..f2d9f21e6 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -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, diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index e6d72c039..da6019b16 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -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; diff --git a/src/entity/projectile/Arrow.php b/src/entity/projectile/Arrow.php index cbf1e5ba7..bfddc295b 100644 --- a/src/entity/projectile/Arrow.php +++ b/src/entity/projectile/Arrow.php @@ -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; diff --git a/src/entity/projectile/Egg.php b/src/entity/projectile/Egg.php index d16f11d9c..a2ccbb41a 100644 --- a/src/entity/projectile/Egg.php +++ b/src/entity/projectile/Egg.php @@ -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 diff --git a/src/entity/projectile/EnderPearl.php b/src/entity/projectile/EnderPearl.php index 9d41d255a..378952521 100644 --- a/src/entity/projectile/EnderPearl.php +++ b/src/entity/projectile/EnderPearl.php @@ -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(); diff --git a/src/entity/projectile/ExperienceBottle.php b/src/entity/projectile/ExperienceBottle.php index fbb3666bf..0c787a04e 100644 --- a/src/entity/projectile/ExperienceBottle.php +++ b/src/entity/projectile/ExperienceBottle.php @@ -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; diff --git a/src/entity/projectile/Snowball.php b/src/entity/projectile/Snowball.php index 88fcc4223..b758505b6 100644 --- a/src/entity/projectile/Snowball.php +++ b/src/entity/projectile/Snowball.php @@ -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){ diff --git a/src/entity/projectile/SplashPotion.php b/src/entity/projectile/SplashPotion.php index 20a087dee..168dac90a 100644 --- a/src/entity/projectile/SplashPotion.php +++ b/src/entity/projectile/SplashPotion.php @@ -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; diff --git a/src/item/ItemFactory.php b/src/item/ItemFactory.php index e197fe24b..c42b825a8 100644 --- a/src/item/ItemFactory.php +++ b/src/item/ItemFactory.php @@ -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)); } } diff --git a/src/world/sound/EntityLandSound.php b/src/world/sound/EntityLandSound.php index ddd4c00ff..c163c07f1 100644 --- a/src/world/sound/EntityLandSound.php +++ b/src/world/sound/EntityLandSound.php @@ -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? ); } diff --git a/src/world/sound/EntityLongFallSound.php b/src/world/sound/EntityLongFallSound.php index d6f75b459..896cc8f0b 100644 --- a/src/world/sound/EntityLongFallSound.php +++ b/src/world/sound/EntityLongFallSound.php @@ -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? ); } diff --git a/src/world/sound/EntityShortFallSound.php b/src/world/sound/EntityShortFallSound.php index 9c743f4c9..ff1f1bbb1 100644 --- a/src/world/sound/EntityShortFallSound.php +++ b/src/world/sound/EntityShortFallSound.php @@ -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? ); } diff --git a/tests/phpstan/configs/phpstan-bugs.neon b/tests/phpstan/configs/phpstan-bugs.neon index bd2169ecf..7d4858d65 100644 --- a/tests/phpstan/configs/phpstan-bugs.neon +++ b/tests/phpstan/configs/phpstan-bugs.neon @@ -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