Automate creation and deletion of Tiles for appropriate blocks

closes #880
This commit is contained in:
Dylan K. Taylor
2019-02-13 12:27:13 +00:00
parent 260c5dcf00
commit 01e7ebeb5c
13 changed files with 100 additions and 97 deletions

View File

@ -41,6 +41,7 @@ use pocketmine\metadata\Metadatable;
use pocketmine\metadata\MetadataValue;
use pocketmine\Player;
use pocketmine\plugin\Plugin;
use pocketmine\tile\TileFactory;
use function array_merge;
use function assert;
use function dechex;
@ -172,8 +173,26 @@ class Block extends Position implements BlockIds, Metadatable{
$this->collisionBoxes = null;
}
/**
* Returns the class of Tile associated with this block.
*
* @return string|null class extending Tile, or null
*/
protected function getTileClass() : ?string{
return null;
}
public function writeStateToWorld() : void{
$this->level->getChunkAtPosition($this)->setBlock($this->x & 0xf, $this->y, $this->z & 0xf, $this->getId(), $this->getDamage());
$tileType = $this->getTileClass();
$oldTile = $this->level->getTile($this);
if($oldTile !== null and ($tileType === null or !($oldTile instanceof $tileType))){
$oldTile->close();
}
if($tileType !== null){
$this->level->addTile(TileFactory::create($tileType, $this->level, $this->asVector3()));
}
}
/**
@ -315,6 +334,9 @@ class Block extends Position implements BlockIds, Metadatable{
* @return bool
*/
public function onBreak(Item $item, Player $player = null) : bool{
if(($t = $this->level->getTile($this)) !== null){
$t->onBlockDestroyed();
}
return $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR));
}