mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 10:22:56 +00:00
Remove all usages of CompoundTag->hasTag()
in pretty much every case, these usages really wanted to read the tag's contents anyway, which can be combined with a getTag() and instanceof call for more concise and static analysis friendly code. In the few cases where the tag contents wasn't needed, it still wanted to check the type, which, again, can be done in a more static analysis friendly way by just using getTag() and instanceof.
This commit is contained in:
@ -61,8 +61,8 @@ class Banner extends Spawnable{
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
$colorIdMap = DyeColorIdMap::getInstance();
|
||||
if($nbt->hasTag(self::TAG_BASE, IntTag::class)){
|
||||
$this->baseColor = $colorIdMap->fromInvertedId($nbt->getInt(self::TAG_BASE));
|
||||
if(($baseColorTag = $nbt->getTag(self::TAG_BASE)) instanceof IntTag){
|
||||
$this->baseColor = $colorIdMap->fromInvertedId($baseColorTag->getValue());
|
||||
}
|
||||
|
||||
$patterns = $nbt->getListTag(self::TAG_PATTERNS);
|
||||
|
@ -49,8 +49,8 @@ class Bed extends Spawnable{
|
||||
}
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
if($nbt->hasTag(self::TAG_COLOR, ByteTag::class)){
|
||||
$this->color = DyeColorIdMap::getInstance()->fromId($nbt->getByte(self::TAG_COLOR));
|
||||
if(($colorTag = $nbt->getTag(self::TAG_COLOR)) instanceof ByteTag){
|
||||
$this->color = DyeColorIdMap::getInstance()->fromId($colorTag->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,9 +59,9 @@ class Chest extends Spawnable implements Container, Nameable{
|
||||
}
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
if($nbt->hasTag(self::TAG_PAIRX, IntTag::class) and $nbt->hasTag(self::TAG_PAIRZ, IntTag::class)){
|
||||
$pairX = $nbt->getInt(self::TAG_PAIRX);
|
||||
$pairZ = $nbt->getInt(self::TAG_PAIRZ);
|
||||
if(($pairXTag = $nbt->getTag(self::TAG_PAIRX)) instanceof IntTag and ($pairZTag = $nbt->getTag(self::TAG_PAIRZ)) instanceof IntTag){
|
||||
$pairX = $pairXTag->getValue();
|
||||
$pairZ = $pairZTag->getValue();
|
||||
if(
|
||||
($this->pos->x === $pairX and abs($this->pos->z - $pairZ) === 1) or
|
||||
($this->pos->z === $pairZ and abs($this->pos->x - $pairX) === 1)
|
||||
|
@ -56,8 +56,8 @@ trait ContainerTrait{
|
||||
$inventory->getListeners()->add(...$listeners);
|
||||
}
|
||||
|
||||
if($tag->hasTag(Container::TAG_LOCK, StringTag::class)){
|
||||
$this->lock = $tag->getString(Container::TAG_LOCK);
|
||||
if(($lockTag = $tag->getTag(Container::TAG_LOCK)) instanceof StringTag){
|
||||
$this->lock = $lockTag->getValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,9 +42,9 @@ class FlowerPot extends Spawnable{
|
||||
private $plant = null;
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){
|
||||
if(($itemIdTag = $nbt->getTag(self::TAG_ITEM)) instanceof ShortTag and ($itemMetaTag = $nbt->getTag(self::TAG_ITEM_DATA)) instanceof IntTag){
|
||||
try{
|
||||
$this->setPlant(BlockFactory::getInstance()->get($nbt->getShort(self::TAG_ITEM), $nbt->getInt(self::TAG_ITEM_DATA)));
|
||||
$this->setPlant(BlockFactory::getInstance()->get($itemIdTag->getValue(), $itemMetaTag->getValue()));
|
||||
}catch(\InvalidArgumentException $e){
|
||||
//noop
|
||||
}
|
||||
|
@ -95,11 +95,11 @@ class MonsterSpawner extends Spawnable{
|
||||
private $requiredPlayerRange = self::DEFAULT_REQUIRED_PLAYER_RANGE;
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
if($nbt->hasTag(self::TAG_LEGACY_ENTITY_TYPE_ID, IntTag::class)){
|
||||
if(($legacyIdTag = $nbt->getTag(self::TAG_LEGACY_ENTITY_TYPE_ID)) instanceof IntTag){
|
||||
//TODO: this will cause unexpected results when there's no mapping for the entity
|
||||
$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);
|
||||
$this->entityTypeId = LegacyEntityIdToStringIdMap::getInstance()->legacyToString($legacyIdTag->getValue()) ?? ":";
|
||||
}elseif(($idTag = $nbt->getTag(self::TAG_ENTITY_TYPE_ID)) instanceof StringTag){
|
||||
$this->entityTypeId = $idTag->getValue();
|
||||
}else{
|
||||
$this->entityTypeId = ":"; //default - TODO: replace this with a constant
|
||||
}
|
||||
|
@ -59,8 +59,8 @@ trait NameableTrait{
|
||||
}
|
||||
|
||||
protected function loadName(CompoundTag $tag) : void{
|
||||
if($tag->hasTag(Nameable::TAG_CUSTOM_NAME, StringTag::class)){
|
||||
$this->customName = $tag->getString(Nameable::TAG_CUSTOM_NAME);
|
||||
if(($customNameTag = $tag->getTag(Nameable::TAG_CUSTOM_NAME)) instanceof StringTag){
|
||||
$this->customName = $customNameTag->getValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,14 +59,14 @@ class Sign extends Spawnable{
|
||||
}
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
if($nbt->hasTag(self::TAG_TEXT_BLOB, StringTag::class)){ //MCPE 1.2 save format
|
||||
$this->text = SignText::fromBlob(mb_scrub($nbt->getString(self::TAG_TEXT_BLOB), 'UTF-8'));
|
||||
if(($textBlobTag = $nbt->getTag(self::TAG_TEXT_BLOB)) instanceof StringTag){ //MCPE 1.2 save format
|
||||
$this->text = SignText::fromBlob(mb_scrub($textBlobTag->getValue(), 'UTF-8'));
|
||||
}else{
|
||||
$text = [];
|
||||
for($i = 0; $i < SignText::LINE_COUNT; ++$i){
|
||||
$textKey = sprintf(self::TAG_TEXT_LINE, $i + 1);
|
||||
if($nbt->hasTag($textKey, StringTag::class)){
|
||||
$text[$i] = mb_scrub($nbt->getString($textKey), 'UTF-8');
|
||||
if(($lineTag = $nbt->getTag($textKey)) instanceof StringTag){
|
||||
$text[$i] = mb_scrub($lineTag->getValue(), 'UTF-8');
|
||||
}
|
||||
}
|
||||
$this->text = new SignText($text);
|
||||
|
@ -51,9 +51,9 @@ class Skull extends Spawnable{
|
||||
}
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
if($nbt->hasTag(self::TAG_SKULL_TYPE, ByteTag::class)){
|
||||
if(($skullTypeTag = $nbt->getTag(self::TAG_SKULL_TYPE)) instanceof ByteTag){
|
||||
try{
|
||||
$this->skullType = SkullType::fromMagicNumber($nbt->getByte(self::TAG_SKULL_TYPE));
|
||||
$this->skullType = SkullType::fromMagicNumber($skullTypeTag->getValue());
|
||||
}catch(\InvalidArgumentException $e){
|
||||
//bad data, drop it
|
||||
}
|
||||
|
Reference in New Issue
Block a user