mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 00:07:30 +00:00
Don't explode when data contains invalid dye colour IDs
This commit is contained in:
parent
a44203a3d4
commit
d5e5a81cff
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,11 +73,11 @@ final class DyeColorIdMap{
|
||||
return ~$this->toId($color) & 0xf;
|
||||
}
|
||||
|
||||
public function fromId(int $id) : DyeColor{
|
||||
return $this->idToEnum[$id]; //TODO: this might not be present (e.g. corrupted data)
|
||||
public function fromId(int $id) : ?DyeColor{
|
||||
return $this->idToEnum[$id];
|
||||
}
|
||||
|
||||
public function fromInvertedId(int $id) : DyeColor{
|
||||
public function fromInvertedId(int $id) : ?DyeColor{
|
||||
return $this->fromId(~$id & 0xf);
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +104,8 @@ class Banner extends ItemBlockWallOrFloor{
|
||||
if($patterns !== null){
|
||||
/** @var CompoundTag $t */
|
||||
foreach($patterns as $t){
|
||||
$this->patterns[] = new BannerPatternLayer(BannerPatternType::fromString($t->getString(self::TAG_PATTERN_NAME)), $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)));
|
||||
$patternColor = $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK(); //TODO: missing pattern colour should be an error
|
||||
$this->patterns[] = new BannerPatternLayer(BannerPatternType::fromString($t->getString(self::TAG_PATTERN_NAME)), $patternColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ class DyeColorIdMapTest extends TestCase{
|
||||
foreach(DyeColor::getAll() as $color){
|
||||
$id = DyeColorIdMap::getInstance()->toId($color);
|
||||
$color2 = DyeColorIdMap::getInstance()->fromId($id);
|
||||
self::assertTrue($color->equals($color2));
|
||||
self::assertTrue($color2 !== null && $color->equals($color2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user