Replaced Position->getLevel() null checks with isValid()

This commit is contained in:
Dylan K. Taylor 2018-03-20 11:10:36 +00:00
parent 73e09392b6
commit 1648fff916
7 changed files with 22 additions and 23 deletions

View File

@ -673,30 +673,30 @@ class Block extends Position implements BlockIds, Metadatable{
} }
public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){
if($this->getLevel() instanceof Level){ if($this->isValid()){
$this->getLevel()->getBlockMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); $this->level->getBlockMetadata()->setMetadata($this, $metadataKey, $newMetadataValue);
} }
} }
public function getMetadata(string $metadataKey){ public function getMetadata(string $metadataKey){
if($this->getLevel() instanceof Level){ if($this->isValid()){
return $this->getLevel()->getBlockMetadata()->getMetadata($this, $metadataKey); return $this->level->getBlockMetadata()->getMetadata($this, $metadataKey);
} }
return null; return null;
} }
public function hasMetadata(string $metadataKey) : bool{ public function hasMetadata(string $metadataKey) : bool{
if($this->getLevel() instanceof Level){ if($this->isValid()){
return $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); return $this->level->getBlockMetadata()->hasMetadata($this, $metadataKey);
} }
return false; return false;
} }
public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ public function removeMetadata(string $metadataKey, Plugin $owningPlugin){
if($this->getLevel() instanceof Level){ if($this->isValid()){
$this->getLevel()->getBlockMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); $this->level->getBlockMetadata()->removeMetadata($this, $metadataKey, $owningPlugin);
} }
} }
} }

View File

@ -67,10 +67,9 @@ class SpawnpointCommand extends VanillaCommand{
} }
} }
$level = $target->getLevel();
if(count($args) === 4){ if(count($args) === 4){
if($level !== null){ if($target->isValid()){
$level = $target->getLevel();
$pos = $sender instanceof Player ? $sender->getPosition() : $level->getSpawnLocation(); $pos = $sender instanceof Player ? $sender->getPosition() : $level->getSpawnLocation();
$x = $this->getRelativeDouble($pos->x, $sender, $args[1]); $x = $this->getRelativeDouble($pos->x, $sender, $args[1]);
$y = $this->getRelativeDouble($pos->y, $sender, $args[2], 0, Level::Y_MAX); $y = $this->getRelativeDouble($pos->y, $sender, $args[2], 0, Level::Y_MAX);

View File

@ -97,7 +97,7 @@ class TeleportCommand extends VanillaCommand{
Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success", [$origin->getName(), $target->getName()])); Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success", [$origin->getName(), $target->getName()]));
return true; return true;
}elseif($target->getLevel() !== null){ }elseif($target->isValid()){
if(count($args) === 4 or count($args) === 6){ if(count($args) === 4 or count($args) === 6){
$pos = 1; $pos = 1;
}else{ }else{

View File

@ -2003,8 +2003,8 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
$this->chunk = null; $this->chunk = null;
} }
if($this->getLevel() !== null){ if($this->isValid()){
$this->getLevel()->removeEntity($this); $this->level->removeEntity($this);
$this->setLevel(null); $this->setLevel(null);
} }

View File

@ -65,16 +65,16 @@ class ChestInventory extends ContainerInventory{
public function onOpen(Player $who) : void{ public function onOpen(Player $who) : void{
parent::onOpen($who); 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); $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{ 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); $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); parent::onClose($who);
} }

View File

@ -71,11 +71,11 @@ class Explosion{
* @param Entity|Block $what * @param Entity|Block $what
*/ */
public function __construct(Position $center, float $size, $what = null){ public function __construct(Position $center, float $size, $what = null){
$this->source = $center; if(!$center->isValid()){
$this->level = $center->getLevel();
if($this->level === null){
throw new \InvalidArgumentException("Position does not have a valid level"); throw new \InvalidArgumentException("Position does not have a valid level");
} }
$this->source = $center;
$this->level = $center->getLevel();
if($size <= 0){ if($size <= 0){
throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $size"); throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $size");

View File

@ -269,8 +269,8 @@ abstract class Tile extends Position{
if(!$this->closed){ if(!$this->closed){
$this->closed = true; $this->closed = true;
if(($level = $this->getLevel()) instanceof Level){ if($this->isValid()){
$level->removeTile($this); $this->level->removeTile($this);
$this->setLevel(null); $this->setLevel(null);
} }