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:
Dylan K. Taylor
2020-07-10 21:01:43 +01:00
parent 91b028c208
commit 279abb871d
27 changed files with 94 additions and 92 deletions

View File

@@ -481,12 +481,12 @@ abstract class Entity{
$this->fallDistance = $nbt->getFloat("FallDistance", 0.0);
if($nbt->hasTag("CustomName", StringTag::class)){
$this->setNameTag($nbt->getString("CustomName"));
if(($customNameTag = $nbt->getTag("CustomName")) instanceof StringTag){
$this->setNameTag($customNameTag->getValue());
if($nbt->hasTag("CustomNameVisible", StringTag::class)){
if(($customNameVisibleTag = $nbt->getTag("CustomNameVisible")) instanceof StringTag){
//Older versions incorrectly saved this as a string (see 890f72dbf23a77f294169b79590770470041adc4)
$this->setNameTagVisible($nbt->getString("CustomNameVisible") !== "");
$this->setNameTagVisible($customNameVisibleTag->getValue() !== "");
}else{
$this->setNameTagVisible($nbt->getByte("CustomNameVisible", 1) !== 0);
}

View File

@@ -118,10 +118,10 @@ final class EntityFactory{
throw new \UnexpectedValueException("Unknown painting motive");
}
$blockIn = new Vector3($nbt->getInt("TileX"), $nbt->getInt("TileY"), $nbt->getInt("TileZ"));
if($nbt->hasTag("Direction", ByteTag::class)){
$facing = Painting::DATA_TO_FACING[$nbt->getByte("Direction")] ?? Facing::NORTH;
}elseif($nbt->hasTag("Facing", ByteTag::class)){
$facing = Painting::DATA_TO_FACING[$nbt->getByte("Facing")] ?? Facing::NORTH;
if(($directionTag = $nbt->getTag("Direction")) instanceof ByteTag){
$facing = Painting::DATA_TO_FACING[$directionTag->getValue()] ?? Facing::NORTH;
}elseif(($facingTag = $nbt->getTag("Facing")) instanceof ByteTag){
$facing = Painting::DATA_TO_FACING[$facingTag->getValue()] ?? Facing::NORTH;
}else{
throw new \UnexpectedValueException("Missing facing info");
}

View File

@@ -105,7 +105,7 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
}
return new Skin( //this throws if the skin is invalid
$skinTag->getString("Name"),
$skinTag->hasTag("Data", StringTag::class) ? $skinTag->getString("Data") : $skinTag->getByteArray("Data"), //old data (this used to be saved as a StringTag in older versions of PM)
($skinDataTag = $skinTag->getTag("Data")) instanceof StringTag ? $skinDataTag->getValue() : $skinTag->getByteArray("Data"), //old data (this used to be saved as a StringTag in older versions of PM)
$skinTag->getByteArray("CapeData", ""),
$skinTag->getString("GeometryName", ""),
$skinTag->getByteArray("GeometryData", "")
@@ -194,8 +194,8 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
* For Human entities which are not players, sets their properties such as nametag, skin and UUID from NBT.
*/
protected function initHumanData(CompoundTag $nbt) : void{
if($nbt->hasTag("NameTag", StringTag::class)){
$this->setNameTag($nbt->getString("NameTag"));
if(($nameTagTag = $nbt->getTag("NameTag")) instanceof StringTag){
$this->setNameTag($nameTagTag->getValue());
}
$this->uuid = UUID::fromData((string) $this->getId(), $this->skin->getSkinData(), $this->getNameTag());
@@ -251,8 +251,8 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
$nbt->getFloat("XpP", 0.0));
$this->xpManager->setLifetimeTotalXp($nbt->getInt("XpTotal", 0));
if($nbt->hasTag("XpSeed", IntTag::class)){
$this->xpSeed = $nbt->getInt("XpSeed");
if(($xpSeedTag = $nbt->getTag("XpSeed")) instanceof IntTag){
$this->xpSeed = $xpSeedTag->getValue();
}else{
$this->xpSeed = random_int(Limits::INT32_MIN, Limits::INT32_MAX);
}

View File

@@ -134,12 +134,12 @@ abstract class Living extends Entity{
$health = $this->getMaxHealth();
if($nbt->hasTag("HealF", FloatTag::class)){
$health = $nbt->getFloat("HealF");
}elseif($nbt->hasTag("Health", ShortTag::class)){
$health = $nbt->getShort("Health"); //Older versions of PocketMine-MP incorrectly saved this as a short instead of a float
}elseif($nbt->hasTag("Health", FloatTag::class)){
$health = $nbt->getFloat("Health");
if(($healFTag = $nbt->getTag("HealF")) instanceof FloatTag){
$health = $healFTag->getValue();
}elseif(($healthTag = $nbt->getTag("Health")) instanceof ShortTag){
$health = $healthTag->getValue(); //Older versions of PocketMine-MP incorrectly saved this as a short instead of a float
}elseif(($healthTag = $nbt->getTag("Health")) instanceof FloatTag){
$health = $healthTag->getValue();
}
$this->setHealth($health);

View File

@@ -108,10 +108,10 @@ class ExperienceOrb extends Entity{
$this->age = $nbt->getShort("Age", 0);
$value = 1;
if($nbt->hasTag(self::TAG_VALUE_PC, ShortTag::class)){ //PC
$value = $nbt->getShort(self::TAG_VALUE_PC);
}elseif($nbt->hasTag(self::TAG_VALUE_PE, IntTag::class)){ //PE save format
$value = $nbt->getInt(self::TAG_VALUE_PE);
if(($valuePcTag = $nbt->getTag(self::TAG_VALUE_PC)) instanceof ShortTag){ //PC
$value = $valuePcTag->getValue();
}elseif(($valuePeTag = $nbt->getTag(self::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
$value = $valuePeTag->getValue();
}
$this->setXpValue($value);

View File

@@ -64,10 +64,10 @@ class FallingBlock extends Entity{
$blockId = 0;
//TODO: 1.8+ save format
if($nbt->hasTag("TileID", IntTag::class)){
$blockId = $nbt->getInt("TileID");
}elseif($nbt->hasTag("Tile", ByteTag::class)){
$blockId = $nbt->getByte("Tile");
if(($tileIdTag = $nbt->getTag("TileID")) instanceof IntTag){
$blockId = $tileIdTag->getValue();
}elseif(($tileTag = $nbt->getTag("Tile")) instanceof ByteTag){
$blockId = $tileTag->getValue();
}
if($blockId === 0){

View File

@@ -82,20 +82,20 @@ abstract class Projectile extends Entity{
$blockId = null;
$blockData = null;
if($nbt->hasTag("tileX", IntTag::class) and $nbt->hasTag("tileY", IntTag::class) and $nbt->hasTag("tileZ", IntTag::class)){
$blockPos = new Vector3($nbt->getInt("tileX"), $nbt->getInt("tileY"), $nbt->getInt("tileZ"));
if(($tileXTag = $nbt->getTag("tileX")) instanceof IntTag and ($tileYTag = $nbt->getTag("tileY")) instanceof IntTag and ($tileZTag = $nbt->getTag("tileZ")) instanceof IntTag){
$blockPos = new Vector3($tileXTag->getValue(), $tileYTag->getValue(), $tileZTag->getValue());
}else{
break;
}
if($nbt->hasTag("blockId", IntTag::class)){
$blockId = $nbt->getInt("blockId");
if(($blockIdTag = $nbt->getTag("blockId")) instanceof IntTag){
$blockId = $blockIdTag->getValue();
}else{
break;
}
if($nbt->hasTag("blockData", ByteTag::class)){
$blockData = $nbt->getByte("blockData");
if(($blockDataTag = $nbt->getTag("blockData")) instanceof ByteTag){
$blockData = $blockDataTag->getValue();
}else{
break;
}