diff --git a/src/pocketmine/block/Beetroot.php b/src/pocketmine/block/Beetroot.php index 2a04c42aa..e154e35b6 100644 --- a/src/pocketmine/block/Beetroot.php +++ b/src/pocketmine/block/Beetroot.php @@ -22,68 +22,10 @@ namespace pocketmine\block; use pocketmine\item\Item; -use pocketmine\level\Level; -use pocketmine\Player; -class Beetroot extends Flowable{ +class Beetroot extends Crops{ public function __construct($meta = 0){ parent::__construct(self::BEETROOT_BLOCK, $meta, "Beetroot Block"); - $this->isActivable = true; - $this->hardness = 0; - } - - public function getBoundingBox(){ - return null; - } - - public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ - $down = $this->getSide(0); - if($down->getID() === self::FARMLAND){ - $this->getLevel()->setBlock($block, $this, true, true); - - return true; - } - - return false; - } - - public function onActivate(Item $item, Player $player = null){ - if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal - $this->meta += mt_rand(2, 5); - if($this->meta > 7){ - $this->meta = 7; - } - $this->getLevel()->setBlock($this, $this, true, true); - $item->count--; - - return true; - } - - return false; - } - - public function onUpdate($type){ - if($type === Level::BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //TODO: Replace with common break method - $this->getLevel()->dropItem($this, Item::get(Item::BEETROOT_SEEDS, 0, 1)); - $this->getLevel()->setBlock($this, new Air(), false); - - return Level::BLOCK_UPDATE_NORMAL; - } - }elseif($type === Level::BLOCK_UPDATE_RANDOM){ - if(mt_rand(0, 2) == 1){ - if($this->meta < 0x07){ - ++$this->meta; - $this->getLevel()->setBlock($this, $this, true); - - return Level::BLOCK_UPDATE_RANDOM; - } - }else{ - return Level::BLOCK_UPDATE_RANDOM; - } - } - - return false; } public function getDrops(Item $item){ diff --git a/src/pocketmine/block/Cactus.php b/src/pocketmine/block/Cactus.php index 959053688..bc6358a2c 100644 --- a/src/pocketmine/block/Cactus.php +++ b/src/pocketmine/block/Cactus.php @@ -22,6 +22,7 @@ namespace pocketmine\block; use pocketmine\entity\Entity; +use pocketmine\event\block\BlockGrowEvent; use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageEvent; use pocketmine\item\Item; @@ -63,18 +64,14 @@ class Cactus extends Transparent{ public function onUpdate($type){ if($type === Level::BLOCK_UPDATE_NORMAL){ $down = $this->getSide(0); - if($down->getID() !== self::SAND and $down->getID() !== self::CACTUS){ //Replace with common break method - $this->getLevel()->setBlock($this, new Air(), false); - $this->getLevel()->dropItem($this, Item::get($this->id)); - + if($down->getID() !== self::SAND and $down->getID() !== self::CACTUS){ + $this->getLevel()->useBreakOn($this); return; }else{ for($side = 2; $side <= 5; ++$side){ $b = $this->getSide($side); if(!$b->isFlowable){ - $this->getLevel()->setBlock($this, new Air(), false); - $this->getLevel()->dropItem($this, Item::get($this->id)); - + $this->getLevel()->useBreakOn($this); return; } } @@ -85,8 +82,10 @@ class Cactus extends Transparent{ for($y = 1; $y < 3; ++$y){ $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z)); if($b->getID() === self::AIR){ - $this->getLevel()->setBlock($b, new Cactus(), true); - break; + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($b, new Cactus())); + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($b, $ev->getNewState(), true); + } } } $this->meta = 0; diff --git a/src/pocketmine/block/Carrot.php b/src/pocketmine/block/Carrot.php index 7a92e9746..de62111cc 100644 --- a/src/pocketmine/block/Carrot.php +++ b/src/pocketmine/block/Carrot.php @@ -25,67 +25,9 @@ use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\Player; -class Carrot extends Flowable{ +class Carrot extends Crops{ public function __construct($meta = 0){ parent::__construct(self::CARROT_BLOCK, $meta, "Carrot Block"); - $this->isActivable = true; - $this->hardness = 0; - } - - public function getBoundingBox(){ - return null; - } - - - public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ - $down = $this->getSide(0); - if($down->getID() === self::FARMLAND){ - $this->getLevel()->setBlock($block, $this, true, true); - - return true; - } - - return false; - } - - public function onActivate(Item $item, Player $player = null){ - if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal - $this->meta += mt_rand(2, 5); - if($this->meta > 7){ - $this->meta = 7; - } - $this->getLevel()->setBlock($this, $this, true); - $item->count--; - - return true; - } - - return false; - } - - public function onUpdate($type){ - if($type === Level::BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - //TODO - //Server::getInstance()->api->entity->drop($this, Item::get(CARROT, 0, 1)); - $this->getLevel()->setBlock($this, new Air(), false, false, true); - - return Level::BLOCK_UPDATE_NORMAL; - } - }elseif($type === Level::BLOCK_UPDATE_RANDOM){ - if(mt_rand(0, 2) == 1){ - if($this->meta < 0x07){ - ++$this->meta; - $this->getLevel()->setBlock($this, $this, true); - - return Level::BLOCK_UPDATE_RANDOM; - } - }else{ - return Level::BLOCK_UPDATE_RANDOM; - } - } - - return false; } public function getDrops(Item $item){ diff --git a/src/pocketmine/block/Crops.php b/src/pocketmine/block/Crops.php new file mode 100644 index 000000000..f7241c186 --- /dev/null +++ b/src/pocketmine/block/Crops.php @@ -0,0 +1,100 @@ +getSide(0); + if($down->getID() === self::FARMLAND){ + $this->getLevel()->setBlock($block, $this, true, true); + + return true; + } + + return false; + } + + + public function onActivate(Item $item, Player $player = null){ + if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal + $block = clone $this; + $block->meta += mt_rand(2, 5); + if($block->meta > 7){ + $block->meta = 7; + } + + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($this, $block)); + + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($this, $ev->getNewState(), true, true); + } + + $item->count--; + + return true; + } + + return false; + } + + public function onUpdate($type){ + if($type === Level::BLOCK_UPDATE_NORMAL){ + if($this->getSide(0)->isTransparent === true){ + $this->getLevel()->useBreakOn($this); + return Level::BLOCK_UPDATE_NORMAL; + } + }elseif($type === Level::BLOCK_UPDATE_RANDOM){ + if(mt_rand(0, 2) == 1){ + if($this->meta < 0x07){ + $block = clone $this; + ++$block->meta; + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($this, $block)); + + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($this, $ev->getNewState(), true, true); + }else{ + return Level::BLOCK_UPDATE_RANDOM; + } + } + }else{ + return Level::BLOCK_UPDATE_RANDOM; + } + } + + return false; + } +} \ No newline at end of file diff --git a/src/pocketmine/block/MelonStem.php b/src/pocketmine/block/MelonStem.php index b38a25720..3e69c11e0 100644 --- a/src/pocketmine/block/MelonStem.php +++ b/src/pocketmine/block/MelonStem.php @@ -21,46 +21,31 @@ namespace pocketmine\block; +use pocketmine\event\block\BlockGrowEvent; use pocketmine\item\Item; use pocketmine\level\Level; -use pocketmine\Player; +use pocketmine\Server; -class MelonStem extends Flowable{ +class MelonStem extends Crops{ public function __construct($meta = 0){ parent::__construct(self::MELON_STEM, $meta, "Melon Stem"); - $this->isActivable = true; - $this->hardness = 0; - } - - public function getBoundingBox(){ - return null; - } - - - public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ - $down = $this->getSide(0); - if($down->getID() === self::FARMLAND){ - $this->getLevel()->setBlock($block, $this, true, true); - - return true; - } - - return false; } public function onUpdate($type){ if($type === Level::BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //TODO: Replace with common break method - $this->getLevel()->dropItem($this, Item::get(Item::MELON_SEEDS, 0, mt_rand(0, 2))); - $this->getLevel()->setBlock($this, new Air(), false, false, true); - + if($this->getSide(0)->isTransparent === true){ + $this->getLevel()->useBreakOn($this); return Level::BLOCK_UPDATE_NORMAL; } }elseif($type === Level::BLOCK_UPDATE_RANDOM){ if(mt_rand(0, 2) == 1){ if($this->meta < 0x07){ - ++$this->meta; - $this->getLevel()->setBlock($this, $this, true); + $block = clone $this; + ++$block->meta; + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($this, $block)); + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($this, $ev->getNewState(), true); + } return Level::BLOCK_UPDATE_RANDOM; }else{ @@ -73,7 +58,10 @@ class MelonStem extends Flowable{ $side = $this->getSide(mt_rand(2, 5)); $d = $side->getSide(0); if($side->getID() === self::AIR and ($d->getID() === self::FARMLAND or $d->getID() === self::GRASS or $d->getID() === self::DIRT)){ - $this->getLevel()->setBlock($side, new Melon(), true); + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($side, new Melon())); + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($side, $ev->getNewState(), true); + } } } } @@ -84,20 +72,6 @@ class MelonStem extends Flowable{ return false; } - public function onActivate(Item $item, Player $player = null){ - if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal - $this->meta = 0x07; - $this->getLevel()->setBlock($this, $this, true); - if(($player->gamemode & 0x01) === 0){ - $item->count--; - } - - return true; - } - - return false; - } - public function getDrops(Item $item){ return [ [Item::MELON_SEEDS, 0, mt_rand(0, 2)], diff --git a/src/pocketmine/block/Potato.php b/src/pocketmine/block/Potato.php index 9e03f4ae2..7eedab8f3 100644 --- a/src/pocketmine/block/Potato.php +++ b/src/pocketmine/block/Potato.php @@ -25,68 +25,9 @@ use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\Player; -class Potato extends Flowable{ +class Potato extends Crops{ public function __construct($meta = 0){ parent::__construct(self::POTATO_BLOCK, $meta, "Potato Block"); - $this->isActivable = true; - $this->hardness = 0; - } - - public function getBoundingBox(){ - return null; - } - - - public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ - $down = $this->getSide(0); - if($down->getID() === self::FARMLAND){ - $this->getLevel()->setBlock($block, $this, true, true); - - return true; - } - - return false; - } - - public function onActivate(Item $item, Player $player = null){ - if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal - $this->meta += mt_rand(2, 5); - if($this->meta > 7){ - $this->meta = 7; - } - $this->getLevel()->setBlock($this, $this, true); - if(($player->gamemode & 0x01) === 0){ - $item->count--; - } - - return true; - } - - return false; - } - - public function onUpdate($type){ - if($type === Level::BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //TODO: Replace with common break method - $this->getLevel()->dropItem($this, Item::get(Item::POTATO, 0, 1)); - $this->getLevel()->setBlock($this, new Air(), false, false, true); - - return Level::BLOCK_UPDATE_NORMAL; - } - }elseif($type === Level::BLOCK_UPDATE_RANDOM){ - if(mt_rand(0, 2) == 1){ - if($this->meta < 0x07){ - ++$this->meta; - $this->getLevel()->setBlock($this, $this, true); - - return Level::BLOCK_UPDATE_RANDOM; - } - }else{ - return Level::BLOCK_UPDATE_RANDOM; - } - } - - return false; } public function getDrops(Item $item){ diff --git a/src/pocketmine/block/PumpkinStem.php b/src/pocketmine/block/PumpkinStem.php index e8e0088a3..b819da990 100644 --- a/src/pocketmine/block/PumpkinStem.php +++ b/src/pocketmine/block/PumpkinStem.php @@ -21,47 +21,32 @@ namespace pocketmine\block; +use pocketmine\event\block\BlockGrowEvent; use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\Player; +use pocketmine\Server; -class PumpkinStem extends Flowable{ +class PumpkinStem extends Crops{ public function __construct($meta = 0){ parent::__construct(self::PUMPKIN_STEM, $meta, "Pumpkin Stem"); - $this->isActivable = true; - $this->hardness = 0; - } - - public function getBoundingBox(){ - return null; - } - - - public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ - $down = $this->getSide(0); - if($down->getID() === self::FARMLAND){ - $this->getLevel()->setBlock($block, $this, true, true); - - return true; - } - - return false; } public function onUpdate($type){ if($type === Level::BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - //TODO - //Server::getInstance()->api->entity->drop($this, Item::get(PUMPKIN_SEEDS, 0, mt_rand(0, 2))); - $this->getLevel()->setBlock($this, new Air(), false, false, true); - + if($this->getSide(0)->isTransparent === true){ + $this->getLevel()->useBreakOn($this); return Level::BLOCK_UPDATE_NORMAL; } }elseif($type === Level::BLOCK_UPDATE_RANDOM){ if(mt_rand(0, 2) == 1){ if($this->meta < 0x07){ - ++$this->meta; - $this->getLevel()->setBlock($this, $this, true); + $block = clone $this; + ++$block->meta; + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($this, $block)); + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($this, $ev->getNewState(), true); + } return Level::BLOCK_UPDATE_RANDOM; }else{ @@ -74,7 +59,10 @@ class PumpkinStem extends Flowable{ $side = $this->getSide(mt_rand(2, 5)); $d = $side->getSide(0); if($side->getID() === self::AIR and ($d->getID() === self::FARMLAND or $d->getID() === self::GRASS or $d->getID() === self::DIRT)){ - $this->getLevel()->setBlock($side, new Pumpkin(), true); + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($side, new Pumpkin())); + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($side, $ev->getNewState(), true); + } } } } @@ -85,20 +73,6 @@ class PumpkinStem extends Flowable{ return false; } - public function onActivate(Item $item, Player $player = null){ - if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal - $this->meta = 0x07; - $this->getLevel()->setBlock($this, $this, true); - if(($player->gamemode & 0x01) === 0){ - $item->count--; - } - - return true; - } - - return false; - } - public function getDrops(Item $item){ return [ [Item::PUMPKIN_SEEDS, 0, mt_rand(0, 2)], diff --git a/src/pocketmine/block/Sugarcane.php b/src/pocketmine/block/Sugarcane.php index b9c8b4f38..811f61e71 100644 --- a/src/pocketmine/block/Sugarcane.php +++ b/src/pocketmine/block/Sugarcane.php @@ -21,10 +21,12 @@ namespace pocketmine\block; +use pocketmine\event\block\BlockGrowEvent; use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\math\Vector3 as Vector3; use pocketmine\Player; +use pocketmine\Server; class Sugarcane extends Flowable{ public function __construct($meta = 0){ @@ -49,7 +51,10 @@ class Sugarcane extends Flowable{ for($y = 1; $y < 3; ++$y){ $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z)); if($b->getID() === self::AIR){ - $this->getLevel()->setBlock($b, new Sugarcane(), true); + Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($b, new Sugarcane())); + if(!$ev->isCancelled()){ + $this->getLevel()->setBlock($b, $ev->getNewState(), true); + } break; } }