mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Flatten FlowerPot tile into its block (mostly)
This commit is contained in:
@ -29,12 +29,19 @@ use pocketmine\math\Facing;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\tile\FlowerPot as TileFlowerPot;
|
||||
use function assert;
|
||||
|
||||
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;
|
||||
|
||||
/** @var Block|null */
|
||||
protected $plant = null;
|
||||
|
||||
protected function writeStateToMeta() : int{
|
||||
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
|
||||
}
|
||||
|
||||
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{
|
||||
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{
|
||||
$pot = $this->getLevel()->getTile($this);
|
||||
if(!($pot instanceof TileFlowerPot)){
|
||||
$plant = $item->getBlock();
|
||||
if(!$this->canAddPlant($plant)){
|
||||
return false;
|
||||
}
|
||||
if(!$pot->canAddItem($item)){
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->occupied = true;
|
||||
$this->getLevel()->setBlock($this, $this, false);
|
||||
$pot->setItem($item->pop());
|
||||
$this->setPlant($plant);
|
||||
$item->pop();
|
||||
$this->level->setBlock($this, $this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDropsForCompatibleTool(Item $item) : array{
|
||||
$items = parent::getDropsForCompatibleTool($item);
|
||||
|
||||
$tile = $this->getLevel()->getTile($this);
|
||||
if($tile instanceof TileFlowerPot){
|
||||
$item = $tile->getItem();
|
||||
if($item->getId() !== Item::AIR){
|
||||
$items[] = $item;
|
||||
}
|
||||
if($this->plant !== null){
|
||||
$items[] = $this->plant->asItem();
|
||||
}
|
||||
|
||||
return $items;
|
||||
|
Reference in New Issue
Block a user