Extract Fallable into trait + interface

Traits are inferior to components in pretty much every aspect imaginable :(
This commit is contained in:
Dylan K. Taylor 2019-02-19 09:58:10 +00:00
parent 6174f1e0ae
commit a4c3ee20b2
7 changed files with 95 additions and 24 deletions

View File

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataValidator;
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
use pocketmine\inventory\AnvilInventory;
use pocketmine\item\Item;
use pocketmine\item\TieredTool;
@ -33,7 +35,8 @@ use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\Player;
class Anvil extends Fallable{
class Anvil extends Transparent implements Fallable{
use FallableTrait;
public const TYPE_NORMAL = 0;
public const TYPE_SLIGHTLY_DAMAGED = 4;
@ -54,10 +57,6 @@ class Anvil extends Fallable{
return 0b11;
}
public function isTransparent() : bool{
return true;
}
public function getHardness() : float{
return 5;
}
@ -92,4 +91,8 @@ class Anvil extends Fallable{
}
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
public function tickFalling() : ?Block{
return null;
}
}

View File

@ -23,9 +23,14 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
use pocketmine\math\Facing;
class ConcretePowder extends Fallable{
class ConcretePowder extends Solid implements Fallable{
use FallableTrait {
onNearbyBlockChange as protected startFalling;
}
public function getHardness() : float{
return 0.5;
@ -39,7 +44,7 @@ class ConcretePowder extends Fallable{
if(($block = $this->checkAdjacentWater()) !== null){
$this->level->setBlock($this, $block);
}else{
parent::onNearbyBlockChange();
$this->startFalling();
}
}

View File

@ -23,11 +23,14 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use function mt_rand;
class Gravel extends Fallable{
class Gravel extends Solid implements Fallable{
use FallableTrait;
protected $id = self::GRAVEL;
@ -56,4 +59,8 @@ class Gravel extends Fallable{
return parent::getDropsForCompatibleTool($item);
}
public function tickFalling() : ?Block{
return null;
}
}

View File

@ -23,7 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block;
class Sand extends Fallable{
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
class Sand extends Solid implements Fallable{
use FallableTrait;
public function getHardness() : float{
return 0.5;
@ -32,4 +36,8 @@ class Sand extends Fallable{
public function getToolType() : int{
return BlockToolType::TYPE_SHOVEL;
}
public function tickFalling() : ?Block{
return null;
}
}

View File

@ -0,0 +1,38 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\block\utils;
use pocketmine\block\Block;
interface Fallable{
/**
* Called every tick by FallingBlock to update the falling state of this block. Used by concrete to check when it
* hits water.
* Return null if you don't want to change the usual behaviour.
*
* @return Block|null
*/
public function tickFalling() : ?Block;
}

View File

@ -21,33 +21,43 @@
declare(strict_types=1);
namespace pocketmine\block;
namespace pocketmine\block\utils;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\block\Fire;
use pocketmine\block\Liquid;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\object\FallingBlock;
use pocketmine\level\Position;
use pocketmine\math\Facing;
abstract class Fallable extends Solid{
/**
* This trait handles falling behaviour for blocks that need them.
* TODO: convert this into a dynamic component
* @see Fallable
*/
trait FallableTrait{
abstract protected function asPosition() : Position;
abstract protected function getId() : int;
abstract protected function getDamage() : int;
public function onNearbyBlockChange() : void{
$down = $this->getSide(Facing::DOWN);
if($down->getId() === self::AIR or $down instanceof Liquid or $down instanceof Fire){
$this->level->setBlock($this, BlockFactory::get(Block::AIR));
$pos = $this->asPosition();
$down = $pos->level->getBlock($pos->getSide(Facing::DOWN));
if($down->getId() === Block::AIR or $down instanceof Liquid or $down instanceof Fire){
$pos->level->setBlock($pos, BlockFactory::get(Block::AIR));
$nbt = EntityFactory::createBaseNBT($this->add(0.5, 0, 0.5));
$nbt = EntityFactory::createBaseNBT($pos->add(0.5, 0, 0.5));
$nbt->setInt("TileID", $this->getId());
$nbt->setByte("Data", $this->getDamage());
/** @var FallingBlock $fall */
$fall = EntityFactory::create(FallingBlock::class, $this->getLevel(), $nbt);
$fall = EntityFactory::create(FallingBlock::class, $pos->getLevel(), $nbt);
$fall->spawnToAll();
}
}
/**
* @return null|Block
*/
public function tickFalling() : ?Block{
return null;
}
}

View File

@ -25,7 +25,7 @@ namespace pocketmine\entity\object;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\block\Fallable;
use pocketmine\block\utils\Fallable;
use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityBlockChangeEvent;
use pocketmine\event\entity\EntityDamageEvent;