diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4f971bd55..adfb5b756 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -473,7 +473,7 @@ abstract class Entity{ $nbt = EntityDataHelper::createBaseNBT($this->location, $this->motion, $this->location->yaw, $this->location->pitch); if(!($this instanceof Player)){ - $nbt->setString("id", EntityFactory::getInstance()->getSaveId(get_class($this))); + EntityFactory::getInstance()->injectSaveId(get_class($this), $nbt); if($this->getNameTag() !== ""){ $nbt->setString("CustomName", $this->getNameTag()); diff --git a/src/entity/EntityFactory.php b/src/entity/EntityFactory.php index ad9c2e510..ee0456692 100644 --- a/src/entity/EntityFactory.php +++ b/src/entity/EntityFactory.php @@ -227,6 +227,14 @@ final class EntityFactory{ return $entity; } + public function injectSaveId(string $class, CompoundTag $saveData) : void{ + if(isset($this->saveNames[$class])){ + $saveData->setTag("id", new StringTag(reset($this->saveNames[$class]))); + }else{ + throw new \InvalidArgumentException("Entity $class is not registered"); + } + } + /** * @phpstan-param class-string $class */ diff --git a/tests/phpstan/configs/l7-baseline.neon b/tests/phpstan/configs/l7-baseline.neon index 85e86f2f8..3558ec9f2 100644 --- a/tests/phpstan/configs/l7-baseline.neon +++ b/tests/phpstan/configs/l7-baseline.neon @@ -530,6 +530,11 @@ parameters: count: 1 path: ../../../src/data/bedrock/LegacyToStringBidirectionalIdMap.php + - + message: "#^Parameter \\#1 \\$value of class pocketmine\\\\nbt\\\\tag\\\\StringTag constructor expects string, string\\|false given\\.$#" + count: 1 + path: ../../../src/entity/EntityFactory.php + - message: "#^Method pocketmine\\\\entity\\\\EntityFactory\\:\\:getSaveId\\(\\) should return string but returns string\\|false\\.$#" count: 1