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

@ -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);
}

View File

@ -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", "");
}
}

View File

@ -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

View File

@ -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(

View File

@ -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(