PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Crops.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\block;
23 
29 
30 abstract class Crops extends Flowable{
31 
32  public function canBeActivated(){
33  return true;
34  }
35 
36  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
37  $down = $this->getSide(0);
38  if($down->getId() === self::FARMLAND){
39  $this->getLevel()->setBlock($block, $this, true, true);
40 
41  return true;
42  }
43 
44  return false;
45  }
46 
47 
48  public function onActivate(Item $item, Player $player = null){
49  if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal
50  $block = clone $this;
51  $block->meta += mt_rand(2, 5);
52  if($block->meta > 7){
53  $block->meta = 7;
54  }
55 
56  Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($this, $block));
57 
58  if(!$ev->isCancelled()){
59  $this->getLevel()->setBlock($this, $ev->getNewState(), true, true);
60  }
61 
62  $item->count--;
63 
64  return true;
65  }
66 
67  return false;
68  }
69 
70  public function onUpdate($type){
71  if($type === Level::BLOCK_UPDATE_NORMAL){
72  if($this->getSide(0)->isTransparent() === true){
73  $this->getLevel()->useBreakOn($this);
74  return Level::BLOCK_UPDATE_NORMAL;
75  }
76  }elseif($type === Level::BLOCK_UPDATE_RANDOM){
77  if(mt_rand(0, 2) == 1){
78  if($this->meta < 0x07){
79  $block = clone $this;
80  ++$block->meta;
81  Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($this, $block));
82 
83  if(!$ev->isCancelled()){
84  $this->getLevel()->setBlock($this, $ev->getNewState(), true, true);
85  }else{
86  return Level::BLOCK_UPDATE_RANDOM;
87  }
88  }
89  }else{
90  return Level::BLOCK_UPDATE_RANDOM;
91  }
92  }
93 
94  return false;
95  }
96 }
static getInstance()
Definition: Server.php:1444
getSide($side, $step=1)
Definition: Block.php:1028