From 17be06a56d8e453a04c7d8dd2c8e3c32bce2857d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 24 May 2017 17:06:42 +0100 Subject: [PATCH] Rough implementation of double plants, fixes #882 --- src/pocketmine/block/DoublePlant.php | 64 +++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/pocketmine/block/DoublePlant.php b/src/pocketmine/block/DoublePlant.php index a9cea11ba..7de4912a7 100644 --- a/src/pocketmine/block/DoublePlant.php +++ b/src/pocketmine/block/DoublePlant.php @@ -25,8 +25,11 @@ namespace pocketmine\block; use pocketmine\item\Item; use pocketmine\level\Level; +use pocketmine\math\Vector3; +use pocketmine\Player; class DoublePlant extends Flowable{ + const BITFLAG_TOP = 0x08; protected $id = self::DOUBLE_PLANT; @@ -35,7 +38,7 @@ class DoublePlant extends Flowable{ } public function canBeReplaced(){ - return true; + return $this->meta === 2 or $this->meta === 3; //grass or fern } public function getName(){ @@ -47,14 +50,44 @@ class DoublePlant extends Flowable{ 4 => "Rose Bush", 5 => "Peony" ]; - return $names[$this->meta & 0x07]; + return $names[$this->meta & 0x07] ?? ""; } + public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ + $id = $block->getSide(Vector3::SIDE_DOWN)->getId(); + if(($id === Block::GRASS or $id === Block::DIRT) and $block->getSide(Vector3::SIDE_UP)->canBeReplaced()){ + $this->getLevel()->setBlock($block, $this, false, false); + $this->getLevel()->setBlock($block->getSide(Vector3::SIDE_UP), Block::get($this->id, $this->meta | self::BITFLAG_TOP), false, false); + + return true; + } + + return false; + } + + /** + * Returns whether this double-plant has a corresponding other half. + * @return bool + */ + public function isValidHalfPlant() : bool{ + if($this->meta & self::BITFLAG_TOP){ + $other = $this->getSide(Vector3::SIDE_DOWN); + }else{ + $other = $this->getSide(Vector3::SIDE_UP); + } + + return ( + $other->getId() === $this->getId() and + ($other->getDamage() & 0x07) === ($this->getDamage() & 0x07) and + ($other->getDamage() & self::BITFLAG_TOP) !== ($this->getDamage() & self::BITFLAG_TOP) + ); + } public function onUpdate($type){ if($type === Level::BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent() === true){ //Replace with common break method - $this->getLevel()->setBlock($this, new Air(), true, true); + $down = $this->getSide(Vector3::SIDE_DOWN); + if(!$this->isValidHalfPlant() or (($this->meta & self::BITFLAG_TOP) === 0 and $down->isTransparent())){ + $this->getLevel()->useBreakOn($this); return Level::BLOCK_UPDATE_NORMAL; } @@ -63,10 +96,27 @@ class DoublePlant extends Flowable{ return false; } - public function getDrops(Item $item){ - //TODO + public function onBreak(Item $item){ + if(parent::onBreak($item) and $this->isValidHalfPlant()){ + return $this->getLevel()->setBlock($this->getSide(($this->meta & self::BITFLAG_TOP) !== 0 ? Vector3::SIDE_DOWN : Vector3::SIDE_UP), Block::get(Block::AIR)); + } - return []; + return false; } + public function getDrops(Item $item){ + if(!$item->isShears() and ($this->meta === 2 or $this->meta === 3)){ //grass or fern + if(mt_rand(0, 24) === 0){ + return [ + [Item::SEEDS, 0, 1] + ]; + }else{ + return []; + } + } + + return [ + [$this->id, $this->meta & 0x07, 1] + ]; + } } \ No newline at end of file