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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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