Entity: do not assume that save IDs are always strings

this is only the first of many changes needed to make entity savedata fully format-agnostic, but it's a start.
This commit is contained in:
Dylan K. Taylor 2020-11-05 15:01:57 +00:00
parent 4b46549cd1
commit e6348bbd34
3 changed files with 14 additions and 1 deletions

View File

@ -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());

View File

@ -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<Entity> $class
*/

View File

@ -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