From 81f982a8d9ee23283217624933fd54f0b0c801f1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 4 May 2020 13:47:39 +0100 Subject: [PATCH] remove hardcoded legacy entity type ID mapping, load from resources instead --- src/block/tile/MonsterSpawner.php | 4 +- src/entity/Entity.php | 3 +- src/network/mcpe/protocol/AddActorPacket.php | 110 ------------------- src/world/sound/EntityLandSound.php | 4 +- src/world/sound/EntityLongFallSound.php | 4 +- src/world/sound/EntityShortFallSound.php | 4 +- 6 files changed, 10 insertions(+), 119 deletions(-) diff --git a/src/block/tile/MonsterSpawner.php b/src/block/tile/MonsterSpawner.php index 601c15e2d..2ee3cf55c 100644 --- a/src/block/tile/MonsterSpawner.php +++ b/src/block/tile/MonsterSpawner.php @@ -23,11 +23,11 @@ declare(strict_types=1); namespace pocketmine\block\tile; +use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\StringTag; -use pocketmine\network\mcpe\protocol\AddActorPacket; /** * @deprecated @@ -97,7 +97,7 @@ class MonsterSpawner extends Spawnable{ public function readSaveData(CompoundTag $nbt) : void{ if($nbt->hasTag(self::TAG_LEGACY_ENTITY_TYPE_ID, IntTag::class)){ //TODO: this will cause unexpected results when there's no mapping for the entity - $this->entityTypeId = AddActorPacket::LEGACY_ID_MAP_BC[$nbt->getInt(self::TAG_LEGACY_ENTITY_TYPE_ID)] ?? ":"; + $this->entityTypeId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($nbt->getInt(self::TAG_LEGACY_ENTITY_TYPE_ID)) ?? ":"; }elseif($nbt->hasTag(self::TAG_ENTITY_TYPE_ID, StringTag::class)){ $this->entityTypeId = $nbt->getString(self::TAG_ENTITY_TYPE_ID); }else{ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 46e74f3b6..fdc034c04 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -28,6 +28,7 @@ namespace pocketmine\entity; use pocketmine\block\Block; use pocketmine\block\Water; +use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\entity\animation\Animation; use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDespawnEvent; @@ -1511,7 +1512,7 @@ abstract class Entity{ protected function sendSpawnPacket(Player $player) : void{ $pk = new AddActorPacket(); $pk->entityRuntimeId = $this->getId(); - $pk->type = AddActorPacket::LEGACY_ID_MAP_BC[static::NETWORK_ID]; + $pk->type = LegacyEntityIdToStringIdMap::getInstance()->legacyToString(static::NETWORK_ID); $pk->position = $this->location->asVector3(); $pk->motion = $this->getMotion(); $pk->yaw = $this->location->yaw; diff --git a/src/network/mcpe/protocol/AddActorPacket.php b/src/network/mcpe/protocol/AddActorPacket.php index 3f375ae93..a0fc3bcee 100644 --- a/src/network/mcpe/protocol/AddActorPacket.php +++ b/src/network/mcpe/protocol/AddActorPacket.php @@ -28,7 +28,6 @@ namespace pocketmine\network\mcpe\protocol; use pocketmine\math\Vector3; use pocketmine\network\mcpe\protocol\serializer\NetworkBinaryStream; use pocketmine\network\mcpe\protocol\types\entity\Attribute; -use pocketmine\network\mcpe\protocol\types\entity\EntityLegacyIds; use pocketmine\network\mcpe\protocol\types\entity\EntityLink; use pocketmine\network\mcpe\protocol\types\entity\MetadataProperty; use function count; @@ -36,115 +35,6 @@ use function count; class AddActorPacket extends DataPacket implements ClientboundPacket{ public const NETWORK_ID = ProtocolInfo::ADD_ACTOR_PACKET; - /* - * Really really really really really nasty hack, to preserve backwards compatibility. - * We can't transition to string IDs within 3.x because the network IDs (the integer ones) are exposed - * to the API in some places (for god's sake shoghi). - * - * TODO: remove this on 4.0 - */ - public const LEGACY_ID_MAP_BC = [ - -1 => ":", - EntityLegacyIds::NPC => "minecraft:npc", - EntityLegacyIds::PLAYER => "minecraft:player", - EntityLegacyIds::WITHER_SKELETON => "minecraft:wither_skeleton", - EntityLegacyIds::HUSK => "minecraft:husk", - EntityLegacyIds::STRAY => "minecraft:stray", - EntityLegacyIds::WITCH => "minecraft:witch", - EntityLegacyIds::ZOMBIE_VILLAGER => "minecraft:zombie_villager", - EntityLegacyIds::BLAZE => "minecraft:blaze", - EntityLegacyIds::MAGMA_CUBE => "minecraft:magma_cube", - EntityLegacyIds::GHAST => "minecraft:ghast", - EntityLegacyIds::CAVE_SPIDER => "minecraft:cave_spider", - EntityLegacyIds::SILVERFISH => "minecraft:silverfish", - EntityLegacyIds::ENDERMAN => "minecraft:enderman", - EntityLegacyIds::SLIME => "minecraft:slime", - EntityLegacyIds::ZOMBIE_PIGMAN => "minecraft:zombie_pigman", - EntityLegacyIds::SPIDER => "minecraft:spider", - EntityLegacyIds::SKELETON => "minecraft:skeleton", - EntityLegacyIds::CREEPER => "minecraft:creeper", - EntityLegacyIds::ZOMBIE => "minecraft:zombie", - EntityLegacyIds::SKELETON_HORSE => "minecraft:skeleton_horse", - EntityLegacyIds::MULE => "minecraft:mule", - EntityLegacyIds::DONKEY => "minecraft:donkey", - EntityLegacyIds::DOLPHIN => "minecraft:dolphin", - EntityLegacyIds::TROPICALFISH => "minecraft:tropicalfish", - EntityLegacyIds::WOLF => "minecraft:wolf", - EntityLegacyIds::SQUID => "minecraft:squid", - EntityLegacyIds::DROWNED => "minecraft:drowned", - EntityLegacyIds::SHEEP => "minecraft:sheep", - EntityLegacyIds::MOOSHROOM => "minecraft:mooshroom", - EntityLegacyIds::PANDA => "minecraft:panda", - EntityLegacyIds::SALMON => "minecraft:salmon", - EntityLegacyIds::PIG => "minecraft:pig", - EntityLegacyIds::VILLAGER => "minecraft:villager", - EntityLegacyIds::COD => "minecraft:cod", - EntityLegacyIds::PUFFERFISH => "minecraft:pufferfish", - EntityLegacyIds::COW => "minecraft:cow", - EntityLegacyIds::CHICKEN => "minecraft:chicken", - EntityLegacyIds::BALLOON => "minecraft:balloon", - EntityLegacyIds::LLAMA => "minecraft:llama", - EntityLegacyIds::IRON_GOLEM => "minecraft:iron_golem", - EntityLegacyIds::RABBIT => "minecraft:rabbit", - EntityLegacyIds::SNOW_GOLEM => "minecraft:snow_golem", - EntityLegacyIds::BAT => "minecraft:bat", - EntityLegacyIds::OCELOT => "minecraft:ocelot", - EntityLegacyIds::HORSE => "minecraft:horse", - EntityLegacyIds::CAT => "minecraft:cat", - EntityLegacyIds::POLAR_BEAR => "minecraft:polar_bear", - EntityLegacyIds::ZOMBIE_HORSE => "minecraft:zombie_horse", - EntityLegacyIds::TURTLE => "minecraft:turtle", - EntityLegacyIds::PARROT => "minecraft:parrot", - EntityLegacyIds::GUARDIAN => "minecraft:guardian", - EntityLegacyIds::ELDER_GUARDIAN => "minecraft:elder_guardian", - EntityLegacyIds::VINDICATOR => "minecraft:vindicator", - EntityLegacyIds::WITHER => "minecraft:wither", - EntityLegacyIds::ENDER_DRAGON => "minecraft:ender_dragon", - EntityLegacyIds::SHULKER => "minecraft:shulker", - EntityLegacyIds::ENDERMITE => "minecraft:endermite", - EntityLegacyIds::MINECART => "minecraft:minecart", - EntityLegacyIds::HOPPER_MINECART => "minecraft:hopper_minecart", - EntityLegacyIds::TNT_MINECART => "minecraft:tnt_minecart", - EntityLegacyIds::CHEST_MINECART => "minecraft:chest_minecart", - EntityLegacyIds::COMMAND_BLOCK_MINECART => "minecraft:command_block_minecart", - EntityLegacyIds::ARMOR_STAND => "minecraft:armor_stand", - EntityLegacyIds::ITEM => "minecraft:item", - EntityLegacyIds::TNT => "minecraft:tnt", - EntityLegacyIds::FALLING_BLOCK => "minecraft:falling_block", - EntityLegacyIds::XP_BOTTLE => "minecraft:xp_bottle", - EntityLegacyIds::XP_ORB => "minecraft:xp_orb", - EntityLegacyIds::EYE_OF_ENDER_SIGNAL => "minecraft:eye_of_ender_signal", - EntityLegacyIds::ENDER_CRYSTAL => "minecraft:ender_crystal", - EntityLegacyIds::SHULKER_BULLET => "minecraft:shulker_bullet", - EntityLegacyIds::FISHING_HOOK => "minecraft:fishing_hook", - EntityLegacyIds::DRAGON_FIREBALL => "minecraft:dragon_fireball", - EntityLegacyIds::ARROW => "minecraft:arrow", - EntityLegacyIds::SNOWBALL => "minecraft:snowball", - EntityLegacyIds::EGG => "minecraft:egg", - EntityLegacyIds::PAINTING => "minecraft:painting", - EntityLegacyIds::THROWN_TRIDENT => "minecraft:thrown_trident", - EntityLegacyIds::FIREBALL => "minecraft:fireball", - EntityLegacyIds::SPLASH_POTION => "minecraft:splash_potion", - EntityLegacyIds::ENDER_PEARL => "minecraft:ender_pearl", - EntityLegacyIds::LEASH_KNOT => "minecraft:leash_knot", - EntityLegacyIds::WITHER_SKULL => "minecraft:wither_skull", - EntityLegacyIds::WITHER_SKULL_DANGEROUS => "minecraft:wither_skull_dangerous", - EntityLegacyIds::BOAT => "minecraft:boat", - EntityLegacyIds::LIGHTNING_BOLT => "minecraft:lightning_bolt", - EntityLegacyIds::SMALL_FIREBALL => "minecraft:small_fireball", - EntityLegacyIds::LLAMA_SPIT => "minecraft:llama_spit", - EntityLegacyIds::AREA_EFFECT_CLOUD => "minecraft:area_effect_cloud", - EntityLegacyIds::LINGERING_POTION => "minecraft:lingering_potion", - EntityLegacyIds::FIREWORKS_ROCKET => "minecraft:fireworks_rocket", - EntityLegacyIds::EVOCATION_FANG => "minecraft:evocation_fang", - EntityLegacyIds::EVOCATION_ILLAGER => "minecraft:evocation_illager", - EntityLegacyIds::VEX => "minecraft:vex", - EntityLegacyIds::AGENT => "minecraft:agent", - EntityLegacyIds::ICE_BOMB => "minecraft:ice_bomb", - EntityLegacyIds::PHANTOM => "minecraft:phantom", - EntityLegacyIds::TRIPOD_CAMERA => "minecraft:tripod_camera" - ]; - /** @var int|null */ public $entityUniqueId = null; //TODO /** @var int */ diff --git a/src/world/sound/EntityLandSound.php b/src/world/sound/EntityLandSound.php index 6e47329e1..ddd4c00ff 100644 --- a/src/world/sound/EntityLandSound.php +++ b/src/world/sound/EntityLandSound.php @@ -24,9 +24,9 @@ declare(strict_types=1); namespace pocketmine\world\sound; use pocketmine\block\Block; +use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\entity\Entity; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\AddActorPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\player\Player; @@ -50,7 +50,7 @@ class EntityLandSound implements Sound{ LevelSoundEventPacket::SOUND_LAND, $pos, $this->blockLandedOn->getRuntimeId(), - $this->entity instanceof Player ? "minecraft:player" : AddActorPacket::LEGACY_ID_MAP_BC[$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::NETWORK_ID) //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 907f4ac2f..d6f75b459 100644 --- a/src/world/sound/EntityLongFallSound.php +++ b/src/world/sound/EntityLongFallSound.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\world\sound; +use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\entity\Entity; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\AddActorPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\player\Player; @@ -47,7 +47,7 @@ class EntityLongFallSound implements Sound{ LevelSoundEventPacket::SOUND_FALL_BIG, $pos, -1, - $this->entity instanceof Player ? "minecraft:player" : AddActorPacket::LEGACY_ID_MAP_BC[$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::NETWORK_ID) //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 130cecb43..9c743f4c9 100644 --- a/src/world/sound/EntityShortFallSound.php +++ b/src/world/sound/EntityShortFallSound.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\world\sound; +use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\entity\Entity; use pocketmine\math\Vector3; -use pocketmine\network\mcpe\protocol\AddActorPacket; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\player\Player; @@ -46,7 +46,7 @@ class EntityShortFallSound implements Sound{ LevelSoundEventPacket::SOUND_FALL_SMALL, $pos, -1, - $this->entity instanceof Player ? "minecraft:player" : AddActorPacket::LEGACY_ID_MAP_BC[$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::NETWORK_ID) //TODO: bad hack, stuff depends on players having a -1 network ID :( //TODO: does isBaby have any relevance here? ); }