From 279abb871d09f7d5e1a2c5b831a96b3d1d83b884 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 10 Jul 2020 21:01:43 +0100 Subject: [PATCH] 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. --- src/block/tile/Banner.php | 4 ++-- src/block/tile/Bed.php | 4 ++-- src/block/tile/Chest.php | 6 +++--- src/block/tile/ContainerTrait.php | 4 ++-- src/block/tile/FlowerPot.php | 4 ++-- src/block/tile/MonsterSpawner.php | 8 ++++---- src/block/tile/NameableTrait.php | 4 ++-- src/block/tile/Sign.php | 8 ++++---- src/block/tile/Skull.php | 4 ++-- src/entity/Entity.php | 8 ++++---- src/entity/EntityFactory.php | 8 ++++---- src/entity/Human.php | 10 +++++----- src/entity/Living.php | 12 ++++++------ src/entity/object/ExperienceOrb.php | 8 ++++---- src/entity/object/FallingBlock.php | 8 ++++---- src/entity/projectile/Projectile.php | 12 ++++++------ src/item/Armor.php | 4 ++-- src/item/Item.php | 2 +- src/network/mcpe/convert/TypeConverter.php | 4 ++-- src/network/mcpe/handler/InGamePacketHandler.php | 4 ++-- src/player/OfflinePlayer.php | 4 ++-- src/player/Player.php | 6 +++--- src/world/format/io/data/BaseNbtWorldData.php | 4 ++-- src/world/format/io/data/BedrockWorldData.php | 11 ++++++----- src/world/format/io/data/JavaWorldData.php | 15 ++++++++------- .../format/io/region/LegacyAnvilChunkTrait.php | 8 ++++---- src/world/format/io/region/McRegion.php | 12 ++++++------ 27 files changed, 94 insertions(+), 92 deletions(-) diff --git a/src/block/tile/Banner.php b/src/block/tile/Banner.php index ec9d17ca81..7c00b98ba6 100644 --- a/src/block/tile/Banner.php +++ b/src/block/tile/Banner.php @@ -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); diff --git a/src/block/tile/Bed.php b/src/block/tile/Bed.php index b41489787a..95c12e524b 100644 --- a/src/block/tile/Bed.php +++ b/src/block/tile/Bed.php @@ -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()); } } diff --git a/src/block/tile/Chest.php b/src/block/tile/Chest.php index 896cafead3..40ba79cd58 100644 --- a/src/block/tile/Chest.php +++ b/src/block/tile/Chest.php @@ -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) diff --git a/src/block/tile/ContainerTrait.php b/src/block/tile/ContainerTrait.php index 2968c54909..b01aa59445 100644 --- a/src/block/tile/ContainerTrait.php +++ b/src/block/tile/ContainerTrait.php @@ -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(); } } diff --git a/src/block/tile/FlowerPot.php b/src/block/tile/FlowerPot.php index 279041303b..f44e73e37c 100644 --- a/src/block/tile/FlowerPot.php +++ b/src/block/tile/FlowerPot.php @@ -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 } diff --git a/src/block/tile/MonsterSpawner.php b/src/block/tile/MonsterSpawner.php index 2ee3cf55c1..8b6d0eeca1 100644 --- a/src/block/tile/MonsterSpawner.php +++ b/src/block/tile/MonsterSpawner.php @@ -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 } diff --git a/src/block/tile/NameableTrait.php b/src/block/tile/NameableTrait.php index 74631a25f3..4a43e8aef3 100644 --- a/src/block/tile/NameableTrait.php +++ b/src/block/tile/NameableTrait.php @@ -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(); } } diff --git a/src/block/tile/Sign.php b/src/block/tile/Sign.php index 8d16eb48e0..f09940b352 100644 --- a/src/block/tile/Sign.php +++ b/src/block/tile/Sign.php @@ -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); diff --git a/src/block/tile/Skull.php b/src/block/tile/Skull.php index 13de524cf4..4db9ebd930 100644 --- a/src/block/tile/Skull.php +++ b/src/block/tile/Skull.php @@ -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 } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index ee28cac992..3fdcc1fae3 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -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); } diff --git a/src/entity/EntityFactory.php b/src/entity/EntityFactory.php index 95bc44ec86..a032c088fa 100644 --- a/src/entity/EntityFactory.php +++ b/src/entity/EntityFactory.php @@ -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"); } diff --git a/src/entity/Human.php b/src/entity/Human.php index ddf0994a55..b00787c227 100644 --- a/src/entity/Human.php +++ b/src/entity/Human.php @@ -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); } diff --git a/src/entity/Living.php b/src/entity/Living.php index 8675dd00ad..dab60300ce 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -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); diff --git a/src/entity/object/ExperienceOrb.php b/src/entity/object/ExperienceOrb.php index c928d4c167..3953eda2f0 100644 --- a/src/entity/object/ExperienceOrb.php +++ b/src/entity/object/ExperienceOrb.php @@ -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); diff --git a/src/entity/object/FallingBlock.php b/src/entity/object/FallingBlock.php index 69e43b800b..cfca869ea7 100644 --- a/src/entity/object/FallingBlock.php +++ b/src/entity/object/FallingBlock.php @@ -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){ diff --git a/src/entity/projectile/Projectile.php b/src/entity/projectile/Projectile.php index 4a3d8f95ec..a6c0f810d8 100644 --- a/src/entity/projectile/Projectile.php +++ b/src/entity/projectile/Projectile.php @@ -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; } diff --git a/src/item/Armor.php b/src/item/Armor.php index 039abe89b7..4f79d9c618 100644 --- a/src/item/Armor.php +++ b/src/item/Armor.php @@ -133,8 +133,8 @@ class Armor extends Durable{ protected function deserializeCompoundTag(CompoundTag $tag) : void{ parent::deserializeCompoundTag($tag); - if($tag->hasTag(self::TAG_CUSTOM_COLOR, IntTag::class)){ - $this->customColor = Color::fromARGB(Binary::unsignInt($tag->getInt(self::TAG_CUSTOM_COLOR))); + if(($colorTag = $tag->getTag(self::TAG_CUSTOM_COLOR)) instanceof IntTag){ + $this->customColor = Color::fromARGB(Binary::unsignInt($colorTag->getValue())); }else{ $this->customColor = null; } diff --git a/src/item/Item.php b/src/item/Item.php index 579fe0622a..17dc9dc295 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -647,7 +647,7 @@ class Item implements \JsonSerializable{ * Deserializes an Item from an NBT CompoundTag */ public static function nbtDeserialize(CompoundTag $tag) : Item{ - if(!$tag->hasTag("id") or !$tag->hasTag("Count")){ + if($tag->getTag("id") === null or $tag->getTag("Count") === null){ return ItemFactory::getInstance()->get(0); } diff --git a/src/network/mcpe/convert/TypeConverter.php b/src/network/mcpe/convert/TypeConverter.php index 11a83d6625..b404588c3c 100644 --- a/src/network/mcpe/convert/TypeConverter.php +++ b/src/network/mcpe/convert/TypeConverter.php @@ -137,8 +137,8 @@ class TypeConverter{ if($compound !== null){ $compound = clone $compound; - if($compound->hasTag(self::DAMAGE_TAG, IntTag::class)){ - $meta = $compound->getInt(self::DAMAGE_TAG); + if(($damageTag = $compound->getTag(self::DAMAGE_TAG)) instanceof IntTag){ + $meta = $damageTag->getValue(); $compound->removeTag(self::DAMAGE_TAG); if($compound->count() === 0){ $compound = null; diff --git a/src/network/mcpe/handler/InGamePacketHandler.php b/src/network/mcpe/handler/InGamePacketHandler.php index 3b0218105e..4fd580a419 100644 --- a/src/network/mcpe/handler/InGamePacketHandler.php +++ b/src/network/mcpe/handler/InGamePacketHandler.php @@ -598,9 +598,9 @@ class InGamePacketHandler extends PacketHandler{ if(!($nbt instanceof CompoundTag)) throw new AssumptionFailedError("PHPStan should ensure this is a CompoundTag"); //for phpstorm's benefit if($block instanceof Sign){ - if($nbt->hasTag("Text", StringTag::class)){ + if(($textBlobTag = $nbt->getTag("Text")) instanceof StringTag){ try{ - $text = SignText::fromBlob($nbt->getString("Text")); + $text = SignText::fromBlob($textBlobTag->getValue()); }catch(\InvalidArgumentException $e){ throw BadPacketException::wrap($e, "Invalid sign text update"); } diff --git a/src/player/OfflinePlayer.php b/src/player/OfflinePlayer.php index a85b657c95..a484a4c756 100644 --- a/src/player/OfflinePlayer.php +++ b/src/player/OfflinePlayer.php @@ -99,11 +99,11 @@ class OfflinePlayer implements IPlayer{ } public function getFirstPlayed() : ?int{ - return ($this->namedtag !== null and $this->namedtag->hasTag("firstPlayed", LongTag::class)) ? $this->namedtag->getLong("firstPlayed") : null; + return ($this->namedtag !== null and ($firstPlayedTag = $this->namedtag->getTag("firstPlayed")) instanceof LongTag) ? $firstPlayedTag->getValue() : null; } public function getLastPlayed() : ?int{ - return ($this->namedtag !== null and $this->namedtag->hasTag("lastPlayed", LongTag::class)) ? $this->namedtag->getLong("lastPlayed") : null; + return ($this->namedtag !== null and ($lastPlayedTag = $this->namedtag->getTag("lastPlayed")) instanceof LongTag) ? $lastPlayedTag->getValue() : null; } public function hasPlayedBefore() : bool{ diff --git a/src/player/Player.php b/src/player/Player.php index caf9057494..cd887dcf2e 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -334,10 +334,10 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, $this->firstPlayed = $nbt->getLong("firstPlayed", $now = (int) (microtime(true) * 1000)); $this->lastPlayed = $nbt->getLong("lastPlayed", $now); - if($this->server->getForceGamemode() or !$nbt->hasTag("playerGameType", IntTag::class)){ - $this->internalSetGameMode($this->server->getGamemode()); + if(!$this->server->getForceGamemode() and ($gameModeTag = $nbt->getTag("playerGameType")) instanceof IntTag){ + $this->internalSetGameMode(GameMode::fromMagicNumber($gameModeTag->getValue() & 0x03)); //TODO: bad hack here to avoid crashes on corrupted data }else{ - $this->internalSetGameMode(GameMode::fromMagicNumber($nbt->getInt("playerGameType") & 0x03)); //TODO: bad hack here to avoid crashes on corrupted data + $this->internalSetGameMode($this->server->getGamemode()); } $this->keepMovement = true; diff --git a/src/world/format/io/data/BaseNbtWorldData.php b/src/world/format/io/data/BaseNbtWorldData.php index b1092130f1..c7de29477f 100644 --- a/src/world/format/io/data/BaseNbtWorldData.php +++ b/src/world/format/io/data/BaseNbtWorldData.php @@ -125,8 +125,8 @@ abstract class BaseNbtWorldData implements WorldData{ } public function getTime() : int{ - if($this->compoundTag->hasTag("Time", IntTag::class)){ //some older PM worlds had this in the wrong format - return $this->compoundTag->getInt("Time"); + if(($timeTag = $this->compoundTag->getTag("Time")) instanceof IntTag){ //some older PM worlds had this in the wrong format + return $timeTag->getValue(); } return $this->compoundTag->getLong("Time", 0); } diff --git a/src/world/format/io/data/BedrockWorldData.php b/src/world/format/io/data/BedrockWorldData.php index a3139a90c9..6ae3103081 100644 --- a/src/world/format/io/data/BedrockWorldData.php +++ b/src/world/format/io/data/BedrockWorldData.php @@ -134,9 +134,10 @@ class BedrockWorldData extends BaseNbtWorldData{ } protected function fix() : void{ - if(!$this->compoundTag->hasTag("generatorName", StringTag::class)){ - if($this->compoundTag->hasTag("Generator", IntTag::class)){ - switch($this->compoundTag->getInt("Generator")){ //Detect correct generator from MCPE data + $generatorNameTag = $this->compoundTag->getTag("generatorName"); + if(!($generatorNameTag instanceof StringTag)){ + if(($mcpeGeneratorTypeTag = $this->compoundTag->getTag("Generator")) instanceof IntTag){ + switch($mcpeGeneratorTypeTag->getValue()){ //Detect correct generator from MCPE data case self::GENERATOR_FLAT: $this->compoundTag->setString("generatorName", "flat"); $this->compoundTag->setString("generatorOptions", "2;7,3,3,2;1"); @@ -154,11 +155,11 @@ class BedrockWorldData extends BaseNbtWorldData{ }else{ $this->compoundTag->setString("generatorName", "default"); } - }elseif(($generatorName = self::hackyFixForGeneratorClasspathInLevelDat($this->compoundTag->getString("generatorName"))) !== null){ + }elseif(($generatorName = self::hackyFixForGeneratorClasspathInLevelDat($generatorNameTag->getValue())) !== null){ $this->compoundTag->setString("generatorName", $generatorName); } - if(!$this->compoundTag->hasTag("generatorOptions", StringTag::class)){ + if(!($this->compoundTag->getTag("generatorOptions")) instanceof StringTag){ $this->compoundTag->setString("generatorOptions", ""); } } diff --git a/src/world/format/io/data/JavaWorldData.php b/src/world/format/io/data/JavaWorldData.php index 59e15bacc9..197b62fa9c 100644 --- a/src/world/format/io/data/JavaWorldData.php +++ b/src/world/format/io/data/JavaWorldData.php @@ -101,13 +101,14 @@ class JavaWorldData extends BaseNbtWorldData{ } protected function fix() : void{ - if(!$this->compoundTag->hasTag("generatorName", StringTag::class)){ + $generatorNameTag = $this->compoundTag->getTag("generatorName"); + if(!($generatorNameTag instanceof StringTag)){ $this->compoundTag->setString("generatorName", "default"); - }elseif(($generatorName = self::hackyFixForGeneratorClasspathInLevelDat($this->compoundTag->getString("generatorName"))) !== null){ + }elseif(($generatorName = self::hackyFixForGeneratorClasspathInLevelDat($generatorNameTag->getValue())) !== null){ $this->compoundTag->setString("generatorName", $generatorName); } - if(!$this->compoundTag->hasTag("generatorOptions", StringTag::class)){ + if(!($this->compoundTag->getTag("generatorOptions") instanceof StringTag)){ $this->compoundTag->setString("generatorOptions", ""); } } @@ -135,8 +136,8 @@ class JavaWorldData extends BaseNbtWorldData{ } public function getRainLevel() : float{ - if($this->compoundTag->hasTag("rainLevel", FloatTag::class)){ //PocketMine/MCPE - return $this->compoundTag->getFloat("rainLevel"); + if(($rainLevelTag = $this->compoundTag->getTag("rainLevel")) instanceof FloatTag){ //PocketMine/MCPE + return $rainLevelTag->getValue(); } return (float) $this->compoundTag->getByte("raining", 0); //PC vanilla @@ -156,8 +157,8 @@ class JavaWorldData extends BaseNbtWorldData{ } public function getLightningLevel() : float{ - if($this->compoundTag->hasTag("lightningLevel", FloatTag::class)){ //PocketMine/MCPE - return $this->compoundTag->getFloat("lightningLevel"); + if(($lightningLevelTag = $this->compoundTag->getTag("lightningLevel")) instanceof FloatTag){ //PocketMine/MCPE + return $lightningLevelTag->getValue(); } return (float) $this->compoundTag->getByte("thundering", 0); //PC vanilla diff --git a/src/world/format/io/region/LegacyAnvilChunkTrait.php b/src/world/format/io/region/LegacyAnvilChunkTrait.php index 1026286fa0..96c5e41803 100644 --- a/src/world/format/io/region/LegacyAnvilChunkTrait.php +++ b/src/world/format/io/region/LegacyAnvilChunkTrait.php @@ -86,10 +86,10 @@ trait LegacyAnvilChunkTrait{ } }; $biomeArray = null; - if($chunk->hasTag("BiomeColors", IntArrayTag::class)){ - $biomeArray = $makeBiomeArray(ChunkUtils::convertBiomeColors($chunk->getIntArray("BiomeColors"))); //Convert back to original format - }elseif($chunk->hasTag("Biomes", ByteArrayTag::class)){ - $biomeArray = $makeBiomeArray($chunk->getByteArray("Biomes")); + if(($biomeColorsTag = $chunk->getTag("BiomeColors")) instanceof IntArrayTag){ + $biomeArray = $makeBiomeArray(ChunkUtils::convertBiomeColors($biomeColorsTag->getValue())); //Convert back to original format + }elseif(($biomesTag = $chunk->getTag("Biomes")) instanceof ByteArrayTag){ + $biomeArray = $makeBiomeArray($biomesTag->getValue()); } $result = new Chunk( diff --git a/src/world/format/io/region/McRegion.php b/src/world/format/io/region/McRegion.php index d78eca5a06..6f5ce48824 100644 --- a/src/world/format/io/region/McRegion.php +++ b/src/world/format/io/region/McRegion.php @@ -68,8 +68,8 @@ class McRegion extends RegionWorldProvider{ } $subChunks = []; - $fullIds = $chunk->hasTag("Blocks", ByteArrayTag::class) ? $chunk->getByteArray("Blocks") : str_repeat("\x00", 32768); - $fullData = $chunk->hasTag("Data", ByteArrayTag::class) ? $chunk->getByteArray("Data") : str_repeat("\x00", 16384); + $fullIds = ($fullIdsTag = $chunk->getTag("Blocks")) instanceof ByteArrayTag ? $fullIdsTag->getValue() : str_repeat("\x00", 32768); + $fullData = ($fullDataTag = $chunk->getTag("Data")) instanceof ByteArrayTag ? $fullDataTag->getValue() : str_repeat("\x00", 16384); for($y = 0; $y < 8; ++$y){ $subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkFromLegacyColumn($fullIds, $fullData, $y)]); @@ -83,10 +83,10 @@ class McRegion extends RegionWorldProvider{ } }; $biomeIds = null; - if($chunk->hasTag("BiomeColors", IntArrayTag::class)){ - $biomeIds = $makeBiomeArray(ChunkUtils::convertBiomeColors($chunk->getIntArray("BiomeColors"))); //Convert back to original format - }elseif($chunk->hasTag("Biomes", ByteArrayTag::class)){ - $biomeIds = $makeBiomeArray($chunk->getByteArray("Biomes")); + if(($biomeColorsTag = $chunk->getTag("BiomeColors")) instanceof IntArrayTag){ + $biomeIds = $makeBiomeArray(ChunkUtils::convertBiomeColors($biomeColorsTag->getValue())); //Convert back to original format + }elseif(($biomesTag = $chunk->getTag("Biomes")) instanceof ByteArrayTag){ + $biomeIds = $makeBiomeArray($biomesTag->getValue()); } $result = new Chunk(