Flatten FlowerPot tile into its block (mostly)

This commit is contained in:
Dylan K. Taylor 2019-02-26 16:31:38 +00:00
parent 53af7f5da8
commit 9afcd72fb6
2 changed files with 100 additions and 68 deletions

View File

@ -29,12 +29,19 @@ use pocketmine\math\Facing;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\tile\FlowerPot as TileFlowerPot; use pocketmine\tile\FlowerPot as TileFlowerPot;
use function assert;
class FlowerPot extends Flowable{ class FlowerPot extends Flowable{
/** @var bool */ /**
* TODO: get rid of this hack (it's currently needed to deal with blockfactory state handling)
* @var bool
*/
protected $occupied = false; protected $occupied = false;
/** @var Block|null */
protected $plant = null;
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{
return $this->occupied ? 1 : 0; return $this->occupied ? 1 : 0;
} }
@ -47,6 +54,59 @@ class FlowerPot extends Flowable{
return 0b1111; //vanilla uses various values, we only care about 1 and 0 for PE return 0b1111; //vanilla uses various values, we only care about 1 and 0 for PE
} }
public function readStateFromWorld() : void{
parent::readStateFromWorld();
$tile = $this->level->getTile($this);
if($tile instanceof TileFlowerPot){
$this->setPlant($tile->getPlant());
}else{
$this->occupied = false;
}
}
public function writeStateToWorld() : void{
parent::writeStateToWorld();
$tile = $this->level->getTile($this);
assert($tile instanceof TileFlowerPot);
$tile->setPlant($this->plant);
}
/**
* @return Block|null
*/
public function getPlant() : ?Block{
return $this->plant;
}
/**
* @param Block|null $plant
*/
public function setPlant(?Block $plant) : void{
if($plant === null or $plant instanceof Air){
$this->plant = null;
}else{
$this->plant = clone $plant;
}
$this->occupied = $this->plant !== null;
}
public function canAddPlant(Block $block) : bool{
if($this->plant !== null){
return false;
}
return
$block instanceof Cactus or
$block instanceof Dandelion or
$block instanceof DeadBush or
$block instanceof Flower or
$block instanceof RedMushroom or
$block instanceof Sapling or
($block instanceof TallGrass and $block->getIdInfo()->getVariant() === 2); //fern - TODO: clean up
//TODO: bamboo
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{ protected function recalculateBoundingBox() : ?AxisAlignedBB{
return AxisAlignedBB::one()->contract(3 / 16, 0, 3 / 16)->trim(Facing::UP, 5 / 8); return AxisAlignedBB::one()->contract(3 / 16, 0, 3 / 16)->trim(Facing::UP, 5 / 8);
} }
@ -66,30 +126,22 @@ class FlowerPot extends Flowable{
} }
public function onActivate(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ public function onActivate(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
$pot = $this->getLevel()->getTile($this); $plant = $item->getBlock();
if(!($pot instanceof TileFlowerPot)){ if(!$this->canAddPlant($plant)){
return false; return false;
} }
if(!$pot->canAddItem($item)){
return true;
}
$this->occupied = true; $this->setPlant($plant);
$this->getLevel()->setBlock($this, $this, false); $item->pop();
$pot->setItem($item->pop()); $this->level->setBlock($this, $this);
return true; return true;
} }
public function getDropsForCompatibleTool(Item $item) : array{ public function getDropsForCompatibleTool(Item $item) : array{
$items = parent::getDropsForCompatibleTool($item); $items = parent::getDropsForCompatibleTool($item);
if($this->plant !== null){
$tile = $this->getLevel()->getTile($this); $items[] = $this->plant->asItem();
if($tile instanceof TileFlowerPot){
$item = $tile->getItem();
if($item->getId() !== Item::AIR){
$items[] = $item;
}
} }
return $items; return $items;

View File

@ -23,79 +23,59 @@ declare(strict_types=1);
namespace pocketmine\tile; namespace pocketmine\tile;
use pocketmine\item\Item; use pocketmine\block\Air;
use pocketmine\item\ItemFactory; use pocketmine\block\Block;
use pocketmine\level\Level; use pocketmine\block\BlockFactory;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
/**
* @deprecated
* @see \pocketmine\block\FlowerPot
*/
class FlowerPot extends Spawnable{ class FlowerPot extends Spawnable{
public const TAG_ITEM = "item"; private const TAG_ITEM = "item";
public const TAG_ITEM_DATA = "mData"; private const TAG_ITEM_DATA = "mData";
/** @var Item */ /** @var Block|null */
private $item; private $plant = null;
public function __construct(Level $level, Vector3 $pos){
$this->item = ItemFactory::air();
parent::__construct($level, $pos);
}
public function readSaveData(CompoundTag $nbt) : void{ public function readSaveData(CompoundTag $nbt) : void{
if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){ if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){
$this->item = ItemFactory::get($nbt->getShort(self::TAG_ITEM, 0), $nbt->getInt(self::TAG_ITEM_DATA, 0), 1); //prevent stupidity with wrong items
if(($id = $nbt->getShort(self::TAG_ITEM)) >= 0 and $id <= 255 and ($data = $nbt->getInt(self::TAG_ITEM_DATA)) >= 0 and $data <= 15){
$this->setPlant(BlockFactory::get($id, $data));
}
}else{
//TODO: new PlantBlock tag
} }
} }
protected function writeSaveData(CompoundTag $nbt) : void{ protected function writeSaveData(CompoundTag $nbt) : void{
$nbt->setShort(self::TAG_ITEM, $this->item->getId()); if($this->plant !== null){
$nbt->setInt(self::TAG_ITEM_DATA, $this->item->getDamage()); $nbt->setShort(self::TAG_ITEM, $this->plant->getId());
} $nbt->setInt(self::TAG_ITEM_DATA, $this->plant->getDamage());
public function canAddItem(Item $item) : bool{
if(!$this->isEmpty()){
return false;
}
switch($item->getId()){
/** @noinspection PhpMissingBreakStatementInspection */
case Item::TALL_GRASS:
if($item->getDamage() === 1){
return false;
}
case Item::SAPLING:
case Item::DEAD_BUSH:
case Item::DANDELION:
case Item::RED_FLOWER:
case Item::BROWN_MUSHROOM:
case Item::RED_MUSHROOM:
case Item::CACTUS:
return true;
default:
return false;
} }
} }
public function getItem() : Item{ public function getPlant() : ?Block{
return clone $this->item; return $this->plant !== null ? clone $this->plant : null;
} }
public function setItem(Item $item){ public function setPlant(?Block $plant){
$this->item = clone $item; if($plant === null or $plant instanceof Air){
$this->plant = null;
}else{
$this->plant = clone $plant;
}
$this->onChanged(); $this->onChanged();
} }
public function removeItem(){
$this->setItem(ItemFactory::air());
}
public function isEmpty() : bool{
return $this->getItem()->isNull();
}
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{ protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
$nbt->setShort(self::TAG_ITEM, $this->item->getId()); if($this->plant !== null){
$nbt->setInt(self::TAG_ITEM_DATA, $this->item->getDamage()); $nbt->setShort(self::TAG_ITEM, $this->plant->getId());
$nbt->setInt(self::TAG_ITEM_DATA, $this->plant->getDamage());
}
} }
} }