Slab: slight reduction of code repetition

This commit is contained in:
Dylan K. Taylor 2018-09-27 16:45:06 +01:00
parent 73f1480e10
commit 8910c93de1

View File

@ -57,6 +57,10 @@ abstract class Slab extends Transparent{
return $this->doubleId; return $this->doubleId;
} }
protected function getDouble() : Block{
return BlockFactory::get($this->doubleId, $this->variant);
}
public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{ public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{
if(parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock)){ if(parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock)){
return true; return true;
@ -76,11 +80,11 @@ abstract class Slab extends Transparent{
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
if($face === Facing::DOWN){ if($face === Facing::DOWN){
if($blockClicked instanceof Slab and $blockClicked->getId() === $this->getId() and $blockClicked->top and $blockClicked->getVariant() === $this->variant){ if($blockClicked instanceof Slab and $blockClicked->getId() === $this->getId() and $blockClicked->top and $blockClicked->getVariant() === $this->variant){
$this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->variant)); $this->getLevel()->setBlock($blockClicked, $this->getDouble());
return true; return true;
}elseif($blockReplace->getId() === $this->getId() and $blockReplace->getVariant() === $this->variant){ }elseif($blockReplace->getId() === $this->getId() and $blockReplace->getVariant() === $this->variant){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->variant)); $this->getLevel()->setBlock($blockReplace, $this->getDouble());
return true; return true;
}else{ }else{
@ -88,18 +92,18 @@ abstract class Slab extends Transparent{
} }
}elseif($face === Facing::UP){ }elseif($face === Facing::UP){
if($blockClicked instanceof Slab and $blockClicked->getId() === $this->getId() and !$blockClicked->top and $blockClicked->getVariant() === $this->variant){ if($blockClicked instanceof Slab and $blockClicked->getId() === $this->getId() and !$blockClicked->top and $blockClicked->getVariant() === $this->variant){
$this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->variant)); $this->getLevel()->setBlock($blockClicked, $this->getDouble());
return true; return true;
}elseif($blockReplace->getId() === $this->getId() and $blockReplace->getVariant() === $this->variant){ }elseif($blockReplace->getId() === $this->getId() and $blockReplace->getVariant() === $this->variant){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->variant)); $this->getLevel()->setBlock($blockReplace, $this->getDouble());
return true; return true;
} }
}else{ //TODO: collision }else{ //TODO: collision
if($blockReplace->getId() === $this->getId()){ if($blockReplace->getId() === $this->getId()){
if($blockReplace->getVariant() === $this->variant){ if($blockReplace->getVariant() === $this->variant){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->variant)); $this->getLevel()->setBlock($blockReplace, $this->getDouble());
return true; return true;
} }