PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Sapling.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 class Sapling extends Flowable{
31  const OAK = 0;
32  const SPRUCE = 1;
33  const BIRCH = 2;
34  const JUNGLE = 3;
35  const ACACIA = 4;
36  const DARK_OAK = 5;
37 
38  protected $id = self::SAPLING;
39 
40  public function __construct($meta = 0){
41  $this->meta = 0;
42  }
43 
44  public function canBeActivated(){
45  return true;
46  }
47 
48  public function getName(){
49  static $names = [
50  0 => "Oak Sapling",
51  1 => "Spruce Sapling",
52  2 => "Birch Sapling",
53  3 => "Jungle Sapling",
54  4 => "Acacia Sapling",
55  5 => "Dark Oak Sapling",
56  6 => "",
57  7 => "",
58  ];
59  return $names[$this->meta & 0x07];
60  }
61 
62 
63  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
64  $down = $this->getSide(0);
65  if($down->getId() === self::GRASS or $down->getId() === self::DIRT or $down->getId() === self::FARMLAND){
66  $this->getLevel()->setBlock($block, $this, true, true);
67 
68  return true;
69  }
70 
71  return false;
72  }
73 
74  public function onActivate(Item $item, Player $player = null){
75  if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal
76  //TODO: change log type
77  Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->meta & 0x07);
78  if(($player->gamemode & 0x01) === 0){
79  $item->count--;
80  }
81 
82  return true;
83  }
84 
85  return false;
86  }
87 
88  public function onUpdate($type){
89  if($type === Level::BLOCK_UPDATE_NORMAL){
90  if($this->getSide(0)->isTransparent() === true){
91  $this->getLevel()->useBreakOn($this);
92 
93  return Level::BLOCK_UPDATE_NORMAL;
94  }
95  }elseif($type === Level::BLOCK_UPDATE_RANDOM){ //Growth
96  if(mt_rand(1, 7) === 1){
97  if(($this->meta & 0x08) === 0x08){
98  Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->meta & 0x07);
99  }else{
100  $this->meta |= 0x08;
101  $this->getLevel()->setBlock($this, $this, true);
102 
103  return Level::BLOCK_UPDATE_RANDOM;
104  }
105  }else{
106  return Level::BLOCK_UPDATE_RANDOM;
107  }
108  }
109 
110  return false;
111  }
112 
113  public function getDrops(Item $item){
114  return [
115  [$this->id, $this->meta & 0x07, 1],
116  ];
117  }
118 }
getSide($side, $step=1)
Definition: Block.php:1028