PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
block/Sugarcane.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 
27 use pocketmine\math\Vector3 as Vector3;
30 
31 class Sugarcane extends Flowable{
32 
33  protected $id = self::SUGARCANE_BLOCK;
34 
35  public function __construct($meta = 0){
36  $this->meta = $meta;
37  }
38 
39  public function getName(){
40  return "Sugarcane";
41  }
42 
43 
44  public function getDrops(Item $item){
45  return [
46  [Item::SUGARCANE, 0, 1],
47  ];
48  }
49 
50  public function onActivate(Item $item, Player $player = null){
51  if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal
52  if($this->getSide(0)->getId() !== self::SUGARCANE_BLOCK){
53  for($y = 1; $y < 3; ++$y){
54  $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z));
55  if($b->getId() === self::AIR){
56  Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($b, new Sugarcane()));
57  if(!$ev->isCancelled()){
58  $this->getLevel()->setBlock($b, $ev->getNewState(), true);
59  }
60  break;
61  }
62  }
63  $this->meta = 0;
64  $this->getLevel()->setBlock($this, $this, true);
65  }
66  if(($player->gamemode & 0x01) === 0){
67  $item->count--;
68  }
69 
70  return true;
71  }
72 
73  return false;
74  }
75 
76  public function onUpdate($type){
77  if($type === Level::BLOCK_UPDATE_NORMAL){
78  $down = $this->getSide(0);
79  if($down->isTransparent() === true and $down->getId() !== self::SUGARCANE_BLOCK){
80  $this->getLevel()->useBreakOn($this);
81 
82  return Level::BLOCK_UPDATE_NORMAL;
83  }
84  }elseif($type === Level::BLOCK_UPDATE_RANDOM){
85  if($this->getSide(0)->getId() !== self::SUGARCANE_BLOCK){
86  if($this->meta === 0x0F){
87  for($y = 1; $y < 3; ++$y){
88  $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z));
89  if($b->getId() === self::AIR){
90  $this->getLevel()->setBlock($b, new Sugarcane(), true);
91  break;
92  }
93  }
94  $this->meta = 0;
95  $this->getLevel()->setBlock($this, $this, true);
96  }else{
97  ++$this->meta;
98  $this->getLevel()->setBlock($this, $this, true);
99  }
100 
101  return Level::BLOCK_UPDATE_RANDOM;
102  }
103  }
104 
105  return false;
106  }
107 
108  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
109  $down = $this->getSide(0);
110  if($down->getId() === self::SUGARCANE_BLOCK){
111  $this->getLevel()->setBlock($block, new Sugarcane(), true);
112 
113  return true;
114  }elseif($down->getId() === self::GRASS or $down->getId() === self::DIRT or $down->getId() === self::SAND){
115  $block0 = $down->getSide(2);
116  $block1 = $down->getSide(3);
117  $block2 = $down->getSide(4);
118  $block3 = $down->getSide(5);
119  if(($block0 instanceof Water) or ($block1 instanceof Water) or ($block2 instanceof Water) or ($block3 instanceof Water)){
120  $this->getLevel()->setBlock($block, new Sugarcane(), true);
121 
122  return true;
123  }
124  }
125 
126  return false;
127  }
128 }
static getInstance()
Definition: Server.php:1444
getSide($side, $step=1)
Definition: Block.php:1028