Revamp Tile creation (again)

This breaks down the handling of tile creation even further.
- Introduced a static Tile::override() method to allow overriding the construction class for a specific type of chest. This applies to classes as opposed to save IDs, so you can override Chest::class with MyCustomChest::class and it will take effect for any Chest save ID.
- Removed MCPE stringy save ID constants from public Tile interface. These are now only used for creating saved tiles from a stored chunk, and saving them.
- Renamed Tile::registerTile() to register()
- Tile::create() and Tile::createFromItem() now accept a class parameter instead of a stringy save ID.
- Tile::create() and Tile::createFromItem() were changed to throw \InvalidArgumentException on unknown/unregistered tile types. They also now never return null, but always (except in exception cases) return an object which is an instanceof the base class specified.
This commit is contained in:
Dylan K. Taylor
2018-12-26 19:21:37 +00:00
parent 9f4bb440bd
commit f6983efec1
12 changed files with 103 additions and 110 deletions

View File

@ -86,13 +86,10 @@ class Bed extends Transparent{
public function writeStateToWorld() : void{
parent::writeStateToWorld();
//extra block properties storage hack
$tile = Tile::create(Tile::BED, $this->getLevel(), $this->asVector3());
if($tile !== null){
if($tile instanceof TileBed){
$tile->setColor($this->color);
}
$this->level->addTile($tile);
}
/** @var TileBed $tile */
$tile = Tile::create(TileBed::class, $this->getLevel(), $this->asVector3());
$tile->setColor($this->color);
$this->level->addTile($tile);
}
public function getHardness() : float{

View File

@ -72,7 +72,8 @@ class Chest extends Transparent{
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
$chest = null;
/** @var TileChest|null $pair */
$pair = null;
if($player !== null){
$this->facing = Facing::opposite($player->getHorizontalFacing());
}
@ -85,21 +86,20 @@ class Chest extends Transparent{
if($c instanceof Chest and $c->isSameType($this) and $c->facing === $this->facing){
$tile = $this->getLevel()->getTile($c);
if($tile instanceof TileChest and !$tile->isPaired()){
$chest = $tile;
$pair = $tile;
break;
}
}
}
if(parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player)){
$tile = Tile::createFromItem(Tile::CHEST, $this->getLevel(), $this->asVector3(), $item);
if($tile !== null){
$this->level->addTile($tile);
}
/** @var TileChest $tile */
$tile = Tile::createFromItem(TileChest::class, $this->getLevel(), $this->asVector3(), $item);
$this->level->addTile($tile);
if($chest instanceof TileChest and $tile instanceof TileChest){
$chest->pairWith($tile);
$tile->pairWith($chest);
if($pair instanceof TileChest){
$pair->pairWith($tile);
$tile->pairWith($pair);
}
return true;

View File

@ -30,6 +30,7 @@ use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\Player;
use pocketmine\tile\EnchantTable as TileEnchantingTable;
use pocketmine\tile\Tile;
class EnchantingTable extends Transparent{
@ -42,9 +43,7 @@ class EnchantingTable extends Transparent{
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
if(parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player)){
if(($tile = Tile::createFromItem(Tile::ENCHANT_TABLE, $this->getLevel(), $this->asVector3(), $item)) !== null){
$this->level->addTile($tile);
}
$this->level->addTile(Tile::createFromItem(TileEnchantingTable::class, $this->getLevel(), $this->asVector3(), $item));
return true;
}

View File

@ -66,9 +66,7 @@ class EnderChest extends Chest{
}
if(Block::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player)){
if(($tile = Tile::createFromItem(Tile::ENDER_CHEST, $this->getLevel(), $this->asVector3(), $item)) !== null){
$this->level->addTile($tile);
}
$this->level->addTile(Tile::createFromItem(TileEnderChest::class, $this->getLevel(), $this->asVector3(), $item));
return true;
}

View File

@ -69,9 +69,7 @@ class FlowerPot extends Flowable{
}
if(parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player)){
if(($tile = Tile::createFromItem(Tile::FLOWER_POT, $this->getLevel(), $this->asVector3(), $item)) !== null){
$this->level->addTile($tile);
}
$this->level->addTile(Tile::createFromItem(TileFlowerPot::class, $this->getLevel(), $this->asVector3(), $item));
return true;
}

View File

@ -99,9 +99,7 @@ class Furnace extends Solid{
$this->facing = Facing::opposite($player->getHorizontalFacing());
}
if(parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player)){
if(($tile = Tile::createFromItem(Tile::FURNACE, $this->getLevel(), $this->asVector3(), $item)) !== null){
$this->level->addTile($tile);
}
$this->level->addTile(Tile::createFromItem(TileFurnace::class, $this->getLevel(), $this->asVector3(), $item));
return true;
}

View File

@ -85,9 +85,7 @@ class ItemFrame extends Flowable{
$this->facing = $face;
if(parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player)){
if(($tile = Tile::createFromItem(Tile::ITEM_FRAME, $this->getLevel(), $this->asVector3(), $item)) !== null){
$this->level->addTile($tile);
}
$this->level->addTile(Tile::createFromItem(TileItemFrame::class, $this->getLevel(), $this->asVector3(), $item));
return true;
}

View File

@ -28,6 +28,7 @@ use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\Player;
use pocketmine\tile\Sign as TileSign;
use pocketmine\tile\Tile;
class SignPost extends Transparent{
@ -82,9 +83,7 @@ class SignPost extends Transparent{
}
if($ret){
if(($tile = Tile::createFromItem(Tile::SIGN, $this->getLevel(), $this->asVector3(), $item)) !== null){
$this->level->addTile($tile);
}
$this->level->addTile(Tile::createFromItem(TileSign::class, $this->getLevel(), $this->asVector3(), $item));
return true;
}
}

View File

@ -70,14 +70,11 @@ class Skull extends Flowable{
public function writeStateToWorld() : void{
parent::writeStateToWorld();
$tile = Tile::create(Tile::SKULL, $this->getLevel(), $this->asVector3());
if($tile !== null){
if($tile instanceof TileSkull){
$tile->setRotation($this->rotation);
$tile->setType($this->type);
}
$this->level->addTile($tile);
}
/** @var TileSkull $tile */
$tile = Tile::create(TileSkull::class, $this->getLevel(), $this->asVector3());
$tile->setRotation($this->rotation);
$tile->setType($this->type);
$this->level->addTile($tile);
}
public function getHardness() : float{

View File

@ -84,17 +84,16 @@ class StandingBanner extends Transparent{
}
if($ret){
$tile = Tile::createFromItem(Tile::BANNER, $this->getLevel(), $this->asVector3(), $item);
if($tile !== null){
if($tile instanceof TileBanner and $item instanceof ItemBanner){
$tile->setBaseColor($item->getBaseColor());
if(($patterns = $item->getPatterns()) !== null){
$tile->setPatterns($patterns);
}
/** @var TileBanner $tile */
$tile = Tile::createFromItem(TileBanner::class, $this->getLevel(), $this->asVector3(), $item);
if($item instanceof ItemBanner){
$tile->setBaseColor($item->getBaseColor());
if(($patterns = $item->getPatterns()) !== null){
$tile->setPatterns($patterns);
}
$this->level->addTile($tile);
}
$this->level->addTile($tile);
return true;
}
}