PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Grass.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 
26 use pocketmine\level\generator\object\TallGrass as TallGrassObject;
32 
33 class Grass extends Solid{
34 
35  protected $id = self::GRASS;
36 
37  public function __construct(){
38 
39  }
40 
41  public function canBeActivated(){
42  return true;
43  }
44 
45  public function getName(){
46  return "Grass";
47  }
48 
49  public function getHardness(){
50  return 3;
51  }
52 
53  public function getDrops(Item $item){
54  return [
55  [Item::DIRT, 0, 1],
56  ];
57  }
58 
59  public function onUpdate($type){
60  if($type === Level::BLOCK_UPDATE_RANDOM){
61  //TODO: light levels
62  $x = mt_rand($this->x - 1, $this->x + 1);
63  $y = mt_rand($this->y - 2, $this->y + 2);
64  $z = mt_rand($this->z - 1, $this->z + 1);
65  $block = $this->getLevel()->getBlock(new Vector3($x, $y, $z));
66  if($block->getId() === Block::DIRT){
67  if($block->getSide(1) instanceof Transparent){
68  Server::getInstance()->getPluginManager()->callEvent($ev = new BlockSpreadEvent($block, $this, new Grass()));
69  if(!$ev->isCancelled()){
70  $this->getLevel()->setBlock($block, $ev->getNewState());
71  }
72  }
73  }
74  }
75  }
76 
77  public function onActivate(Item $item, Player $player = null){
78  if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){
79  $item->count--;
80  TallGrassObject::growGrass($this->getLevel(), $this, new Random(mt_rand()), 8, 2);
81 
82  return true;
83  }elseif($item->isHoe()){
84  $item->useOn($this);
85  $this->getLevel()->setBlock($this, new Farmland());
86 
87  return true;
88  }
89 
90  return false;
91  }
92 }
static getInstance()
Definition: Server.php:1444