From a4735eaf76be48b9cc8fa0f032e5646794246ec1 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 9 Dec 2014 02:22:18 +0100 Subject: [PATCH] Replaced some instanceof to null checks --- src/pocketmine/Player.php | 11 +++--- src/pocketmine/entity/Entity.php | 6 +-- src/pocketmine/level/Explosion.php | 6 +-- src/pocketmine/level/Level.php | 59 ++++++++++++------------------ src/pocketmine/level/Position.php | 17 +-------- 5 files changed, 35 insertions(+), 64 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index f56a4029e..7fccc192b 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -167,8 +167,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ protected $iusername; protected $displayName; protected $startAction = false; - /** @var Vector3|bool */ - protected $sleeping = false; + /** @var Vector3 */ + protected $sleeping = null; protected $clientID = null; protected $stepHeight = 0.6; @@ -517,7 +517,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ * @return bool */ public function isSleeping(){ - return $this->sleeping instanceof Vector3; + return $this->sleeping !== null; } public function unloadChunk($x, $z){ @@ -793,7 +793,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ public function sleepOn(Vector3 $pos){ foreach($this->level->getNearbyEntities($this->boundingBox->grow(2, 1, 2), $this) as $p){ if($p instanceof Player){ - if($p->sleeping instanceof Vector3){ + if($p->sleeping !== null){ if($pos->distance($p->sleeping) <= 0.1){ return false; } @@ -862,7 +862,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE){ foreach($this->level->getPlayers() as $p){ - if($p->sleeping === false){ + if($p->sleeping === null){ return; } } @@ -2579,6 +2579,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($this->dead === true){ return; } + if($this->isCreative()){ if($source instanceof EntityDamageEvent){ $cause = $source->getCause(); diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 0e76a38e2..61fd1ead1 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -1058,7 +1058,7 @@ abstract class Entity extends Location implements Metadatable{ for($v->x = $minX; $v->x <= $maxX; ++$v->x){ for($v->y = $minY; $v->y <= $maxY; ++$v->y){ $block = $this->level->getBlock($v); - if($block !== null and $block->hasEntityCollision()){ + if($block->hasEntityCollision()){ $block->onEntityCollide($this); if(!($this instanceof Player)){ $block->addVelocityToEntity($this, $vector); @@ -1098,7 +1098,7 @@ abstract class Entity extends Location implements Metadatable{ return false; } - if($pos instanceof Position and $pos->level instanceof Level and $pos->level !== $this->level){ + if($pos instanceof Position and $pos->level !== null and $pos->level !== $this->level){ if($this->switchLevel($pos->getLevel()) === false){ return false; } @@ -1113,7 +1113,7 @@ abstract class Entity extends Location implements Metadatable{ if($this->chunk === null or ($this->chunkX !== ($this->x >> 4) and $this->chunkZ !== ($this->z >> 4))){ - if($this->chunk instanceof FullChunk){ + if($this->chunk !== null){ $this->chunk->removeEntity($this); } $this->level->loadChunk($this->x >> 4, $this->z >> 4); diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index db8812f16..95a88e232 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -107,7 +107,7 @@ class Explosion{ } $block = $this->level->getBlock($vBlock); - if(!($block instanceof Air)){ + if($block->getId() !== 0){ $blastForce -= ($block->getHardness() / 5 + 0.3) * $this->stepLen; if($blastForce > 0){ $index = ($block->x << 15) + ($block->z << 7) + $block->y; @@ -181,9 +181,7 @@ class Explosion{ $air = Item::get(Item::AIR); foreach($this->affectedBlocks as $block){ - $block->setDamage($this->level->getBlockDataAt($block->x, $block->y, $block->z)); - - if($block instanceof TNT){ + if($block->getId() === Block::TNT){ $mot = (new Random())->nextSignedFloat() * M_PI * 2; $tnt = Entity::createEntity("PrimedTNT", $this->level->getChunk($block->x >> 4, $block->z >> 4), new Compound("", [ "Pos" => new Enum("Pos", [ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index f242d2bd3..c939171fa 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -698,11 +698,7 @@ class Level implements ChunkManager, Metadatable{ * @param Vector3 $pos */ public function updateAround(Vector3 $pos){ - if($pos instanceof Block){ - $block = $pos; - }else{ - $block = $this->getBlock($pos); - } + $block = $this->getBlock($pos); for($side = 0; $side <= 5; ++$side){ $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block->getSide($side))); @@ -746,7 +742,7 @@ class Level implements ChunkManager, Metadatable{ for($v->x = $minX; $v->x < $maxX; ++$v->x){ for($v->y = $minY - 1; $v->y < $maxY; ++$v->y){ $block = $this->getBlock($v); - if(!($block instanceof Air)){ + if($block->getId() !== 0){ $block->collidesWithBB($bb, $collides); } } @@ -768,7 +764,7 @@ class Level implements ChunkManager, Metadatable{ $bb = $this->getBlock($pos)->getBoundingBox(); } - return $bb instanceof AxisAlignedBB and $bb->getAverageEdgeLength() >= 1; + return $bb !== null and $bb->getAverageEdgeLength() >= 1; } /** @@ -793,7 +789,7 @@ class Level implements ChunkManager, Metadatable{ for($v->x = $minX; $v->x < $maxX; ++$v->x){ for($v->y = $minY - 1; $v->y < $maxY; ++$v->y){ $block = $this->getBlock($v); - if(!($block instanceof Air)){ + if($block->getId() !== 0){ $block->collidesWithBB($bb, $collides); } } @@ -882,7 +878,7 @@ class Level implements ChunkManager, Metadatable{ */ public function getFullLight(Vector3 $pos){ - $chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, true); + $chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, false); $level = 0; if($chunk instanceof FullChunk){ $level = $chunk->getBlockSkyLight($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f); @@ -1053,7 +1049,7 @@ class Level implements ChunkManager, Metadatable{ * @return bool */ public function setBlock(Vector3 $pos, Block $block, $direct = false, $update = true){ - if($pos->y < 0 or $pos->y >= 128 or !($block instanceof Block)){ + if($pos->y < 0 or $pos->y >= 128){ return false; } @@ -1441,11 +1437,9 @@ class Level implements ChunkManager, Metadatable{ for($x = $minX; $x <= $maxX; ++$x){ for($z = $minZ; $z <= $maxZ; ++$z){ - if($this->isChunkLoaded($x, $z)){ - foreach($this->getChunkEntities($x, $z) as $ent){ - if($ent !== $entity and $ent->boundingBox->intersectsWith($bb)){ - $nearby[] = $ent; - } + foreach($this->getChunkEntities($x, $z) as $ent){ + if($ent !== $entity and $ent->boundingBox->intersectsWith($bb)){ + $nearby[] = $ent; } } } @@ -1489,12 +1483,9 @@ class Level implements ChunkManager, Metadatable{ * @return Tile */ public function getTile(Vector3 $pos){ - if($pos instanceof Position and $pos->getLevel() !== $this){ - return null; - } - $chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4); + $chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, false); - if($chunk instanceof FullChunk){ + if($chunk !== null){ return $chunk->getTile($pos->x & 0x0f, $pos->y & 0xff, $pos->z & 0x0f); } @@ -1510,7 +1501,7 @@ class Level implements ChunkManager, Metadatable{ * @return Entity[] */ public function getChunkEntities($X, $Z){ - return ($chunk = $this->getChunk($X, $Z)) instanceof FullChunk ? $chunk->getEntities() : []; + return ($chunk = $this->getChunk($X, $Z)) !== null ? $chunk->getEntities() : []; } /** @@ -1522,7 +1513,7 @@ class Level implements ChunkManager, Metadatable{ * @return Tile[] */ public function getChunkTiles($X, $Z){ - return ($chunk = $this->getChunk($X, $Z)) instanceof FullChunk ? $chunk->getTiles() : []; + return ($chunk = $this->getChunk($X, $Z)) !== null ? $chunk->getTiles() : []; } /** @@ -1698,7 +1689,7 @@ class Level implements ChunkManager, Metadatable{ public function getChunk($x, $z, $create = false){ if(isset($this->chunks[$index = Level::chunkHash($x, $z)])){ return $this->chunks[$index]; - }elseif($this->loadChunk($x, $z, $create) and $this->chunks[$index] instanceof FullChunk){ + }elseif($this->loadChunk($x, $z, $create) and $this->chunks[$index] !== null){ return $this->chunks[$index]; } @@ -1719,11 +1710,11 @@ class Level implements ChunkManager, Metadatable{ } public function generateChunkCallback($x, $z, FullChunk $chunk){ - $oldChunk = $this->getChunk($x, $z); + $oldChunk = $this->getChunk($x, $z, false); unset($this->chunkGenerationQueue[Level::chunkHash($x, $z)]); $this->setChunk($x, $z, $chunk); - $chunk = $this->getChunk($x, $z); - if($chunk instanceof FullChunk and (!($oldChunk instanceof FullChunk) or $oldChunk->isPopulated() === false) and $chunk->isPopulated()){ + $chunk = $this->getChunk($x, $z, false); + if($chunk !== null and (!($oldChunk !== null) or $oldChunk->isPopulated() === false) and $chunk->isPopulated()){ $this->server->getPluginManager()->callEvent(new ChunkPopulateEvent($chunk)); } } @@ -1755,10 +1746,6 @@ class Level implements ChunkManager, Metadatable{ * @return int 0-127 */ public function getHighestBlockAt($x, $z){ - if(!$this->isChunkLoaded($x >> 4, $z >> 4)){ - $this->loadChunk($x >> 4, $z >> 4); - } - return $this->getChunk($x >> 4, $z >> 4, true)->getHighestBlockAt($x & 0x0f, $z & 0x0f); } @@ -1780,7 +1767,7 @@ class Level implements ChunkManager, Metadatable{ */ public function isChunkGenerated($x, $z){ $chunk = $this->getChunk($x, $z); - return $chunk instanceof FullChunk ? $chunk->isGenerated() : false; + return $chunk !== null ? $chunk->isGenerated() : false; } /** @@ -1791,7 +1778,7 @@ class Level implements ChunkManager, Metadatable{ */ public function isChunkPopulated($x, $z){ $chunk = $this->getChunk($x, $z); - return $chunk instanceof FullChunk ? $chunk->isPopulated() : false; + return $chunk !== null ? $chunk->isPopulated() : false; } /** @@ -1964,7 +1951,7 @@ class Level implements ChunkManager, Metadatable{ $this->cancelUnloadChunkRequest($x, $z); $chunk = $this->provider->getChunk($x, $z, $generate); - if($chunk instanceof FullChunk){ + if($chunk !== null){ $this->chunks[$index] = $chunk; $chunk->initChunk(); }else{ @@ -1972,7 +1959,7 @@ class Level implements ChunkManager, Metadatable{ $this->provider->loadChunk($x, $z, $generate); $this->timings->syncChunkLoadTimer->stopTiming(); - if(($chunk = $this->provider->getChunk($x, $z)) instanceof FullChunk){ + if(($chunk = $this->provider->getChunk($x, $z)) !== null){ $this->chunks[$index] = $chunk; $chunk->initChunk(); }else{ @@ -2014,7 +2001,7 @@ class Level implements ChunkManager, Metadatable{ $chunk = $this->getChunk($x, $z); - if($chunk instanceof FullChunk){ + if($chunk !== null){ $this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($chunk)); if($ev->isCancelled()){ return false; @@ -2022,7 +2009,7 @@ class Level implements ChunkManager, Metadatable{ } try{ - if($chunk instanceof FullChunk and $chunk->hasChanged() and $this->getAutoSave()){ + if($chunk !== null and $chunk->hasChanged() and $this->getAutoSave()){ $this->provider->setChunk($x, $z, $chunk); $this->provider->saveChunk($x, $z); } diff --git a/src/pocketmine/level/Position.php b/src/pocketmine/level/Position.php index 98ee2420a..d06061328 100644 --- a/src/pocketmine/level/Position.php +++ b/src/pocketmine/level/Position.php @@ -64,7 +64,7 @@ class Position extends Vector3{ * @return bool */ public function isValid(){ - return isset($this->level) and $this->level instanceof Level; + return $this->level !== null; } /** @@ -109,21 +109,6 @@ class Position extends Vector3{ return Position::fromObject(parent::getSide($side, $step), $this->level); } - /** - * Returns the distance between two points or objects - * - * @param Vector3 $pos - * - * @return float - */ - public function distance(Vector3 $pos){ - if(($pos instanceof Position) and $pos->getLevel() !== $this->getLevel()){ - return PHP_INT_MAX; - } - - return parent::distance($pos); - } - public function __toString(){ return "Position(level=" . ($this->isValid() ? $this->getLevel()->getName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; }