Don't explode when data contains invalid dye colour IDs

This commit is contained in:
Dylan K. Taylor
2021-04-28 13:39:03 +01:00
parent a44203a3d4
commit d5e5a81cff
6 changed files with 27 additions and 11 deletions

View File

@ -60,15 +60,21 @@ class Banner extends Spawnable{
public function readSaveData(CompoundTag $nbt) : void{
$colorIdMap = DyeColorIdMap::getInstance();
if(($baseColorTag = $nbt->getTag(self::TAG_BASE)) instanceof IntTag){
$this->baseColor = $colorIdMap->fromInvertedId($baseColorTag->getValue());
if(
($baseColorTag = $nbt->getTag(self::TAG_BASE)) instanceof IntTag &&
($baseColor = $colorIdMap->fromInvertedId($baseColorTag->getValue())) !== null
){
$this->baseColor = $baseColor;
}else{
$this->baseColor = DyeColor::BLACK(); //TODO: this should be an error
}
$patterns = $nbt->getListTag(self::TAG_PATTERNS);
if($patterns !== null){
/** @var CompoundTag $pattern */
foreach($patterns as $pattern){
$this->patterns[] = new BannerPatternLayer(BannerPatternType::fromString($pattern->getString(self::TAG_PATTERN_NAME)), $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)));
$patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK(); //TODO: missing pattern colour should be an error
$this->patterns[] = new BannerPatternLayer(BannerPatternType::fromString($pattern->getString(self::TAG_PATTERN_NAME)), $patternColor);
}
}
}

View File

@ -49,8 +49,13 @@ class Bed extends Spawnable{
}
public function readSaveData(CompoundTag $nbt) : void{
if(($colorTag = $nbt->getTag(self::TAG_COLOR)) instanceof ByteTag){
$this->color = DyeColorIdMap::getInstance()->fromId($colorTag->getValue());
if(
($colorTag = $nbt->getTag(self::TAG_COLOR)) instanceof ByteTag &&
($color = DyeColorIdMap::getInstance()->fromId($colorTag->getValue())) !== null
){
$this->color = $color;
}else{
$this->color = DyeColor::RED(); //TODO: this should be an error, but we don't have the systems to handle it yet
}
}

View File

@ -33,7 +33,11 @@ trait ColorInMetadataTrait{
* @see Block::readStateFromData()
*/
public function readStateFromData(int $id, int $stateMeta) : void{
$this->color = DyeColorIdMap::getInstance()->fromId($stateMeta);
$color = DyeColorIdMap::getInstance()->fromId($stateMeta);
if($color === null){
throw new InvalidBlockStateException("No dye colour corresponds to ID $stateMeta");
}
$this->color = $color;
}
/**