From 1648fff916be33160f5141c9efe416b77be80450 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 20 Mar 2018 11:10:36 +0000 Subject: [PATCH] Replaced Position->getLevel() null checks with isValid() --- src/pocketmine/block/Block.php | 16 ++++++++-------- .../command/defaults/SpawnpointCommand.php | 5 ++--- .../command/defaults/TeleportCommand.php | 2 +- src/pocketmine/entity/Entity.php | 4 ++-- src/pocketmine/inventory/ChestInventory.php | 8 ++++---- src/pocketmine/level/Explosion.php | 6 +++--- src/pocketmine/tile/Tile.php | 4 ++-- 7 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 2749de1d9..81cd666d7 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -673,30 +673,30 @@ class Block extends Position implements BlockIds, Metadatable{ } public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ - if($this->getLevel() instanceof Level){ - $this->getLevel()->getBlockMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); + if($this->isValid()){ + $this->level->getBlockMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); } } public function getMetadata(string $metadataKey){ - if($this->getLevel() instanceof Level){ - return $this->getLevel()->getBlockMetadata()->getMetadata($this, $metadataKey); + if($this->isValid()){ + return $this->level->getBlockMetadata()->getMetadata($this, $metadataKey); } return null; } public function hasMetadata(string $metadataKey) : bool{ - if($this->getLevel() instanceof Level){ - return $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); + if($this->isValid()){ + return $this->level->getBlockMetadata()->hasMetadata($this, $metadataKey); } return false; } public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ - if($this->getLevel() instanceof Level){ - $this->getLevel()->getBlockMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); + if($this->isValid()){ + $this->level->getBlockMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); } } } diff --git a/src/pocketmine/command/defaults/SpawnpointCommand.php b/src/pocketmine/command/defaults/SpawnpointCommand.php index 65d49e364..263a38375 100644 --- a/src/pocketmine/command/defaults/SpawnpointCommand.php +++ b/src/pocketmine/command/defaults/SpawnpointCommand.php @@ -67,10 +67,9 @@ class SpawnpointCommand extends VanillaCommand{ } } - $level = $target->getLevel(); - if(count($args) === 4){ - if($level !== null){ + if($target->isValid()){ + $level = $target->getLevel(); $pos = $sender instanceof Player ? $sender->getPosition() : $level->getSpawnLocation(); $x = $this->getRelativeDouble($pos->x, $sender, $args[1]); $y = $this->getRelativeDouble($pos->y, $sender, $args[2], 0, Level::Y_MAX); diff --git a/src/pocketmine/command/defaults/TeleportCommand.php b/src/pocketmine/command/defaults/TeleportCommand.php index 6c7824a20..b83b21677 100644 --- a/src/pocketmine/command/defaults/TeleportCommand.php +++ b/src/pocketmine/command/defaults/TeleportCommand.php @@ -97,7 +97,7 @@ class TeleportCommand extends VanillaCommand{ Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success", [$origin->getName(), $target->getName()])); return true; - }elseif($target->getLevel() !== null){ + }elseif($target->isValid()){ if(count($args) === 4 or count($args) === 6){ $pos = 1; }else{ diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 464ae2769..9755ca146 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -2003,8 +2003,8 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ $this->chunk = null; } - if($this->getLevel() !== null){ - $this->getLevel()->removeEntity($this); + if($this->isValid()){ + $this->level->removeEntity($this); $this->setLevel(null); } diff --git a/src/pocketmine/inventory/ChestInventory.php b/src/pocketmine/inventory/ChestInventory.php index 65cdb6736..741ee29b0 100644 --- a/src/pocketmine/inventory/ChestInventory.php +++ b/src/pocketmine/inventory/ChestInventory.php @@ -65,16 +65,16 @@ class ChestInventory extends ContainerInventory{ public function onOpen(Player $who) : void{ parent::onOpen($who); - if(count($this->getViewers()) === 1 and ($level = $this->getHolder()->getLevel()) instanceof Level){ + if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){ $this->broadcastBlockEventPacket(true); - $level->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), LevelSoundEventPacket::SOUND_CHEST_OPEN); + $this->getHolder()->getLevel()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), LevelSoundEventPacket::SOUND_CHEST_OPEN); } } public function onClose(Player $who) : void{ - if(count($this->getViewers()) === 1 and ($level = $this->getHolder()->getLevel()) instanceof Level){ + if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){ $this->broadcastBlockEventPacket(false); - $level->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), LevelSoundEventPacket::SOUND_CHEST_CLOSED); + $this->getHolder()->getLevel()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), LevelSoundEventPacket::SOUND_CHEST_CLOSED); } parent::onClose($who); } diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index 8c22c9ec7..4efbd7f50 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -71,11 +71,11 @@ class Explosion{ * @param Entity|Block $what */ public function __construct(Position $center, float $size, $what = null){ - $this->source = $center; - $this->level = $center->getLevel(); - if($this->level === null){ + if(!$center->isValid()){ throw new \InvalidArgumentException("Position does not have a valid level"); } + $this->source = $center; + $this->level = $center->getLevel(); if($size <= 0){ throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $size"); diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index d08b0fde2..7b2d4ca7b 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -269,8 +269,8 @@ abstract class Tile extends Position{ if(!$this->closed){ $this->closed = true; - if(($level = $this->getLevel()) instanceof Level){ - $level->removeTile($this); + if($this->isValid()){ + $this->level->removeTile($this); $this->setLevel(null); }