PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Leaves.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 Leaves extends Transparent{
31  const OAK = 0;
32  const SPRUCE = 1;
33  const BIRCH = 2;
34  const JUNGLE = 3;
35  const ACACIA = 0;
36  const DARK_OAK = 1;
37 
38  protected $id = self::LEAVES;
39 
40  public function __construct($meta = 0){
41  $this->meta = $meta;
42  }
43 
44  public function getHardness(){
45  return 1;
46  }
47 
48  public function getName(){
49  static $names = [
50  self::OAK => "Oak Leaves",
51  self::SPRUCE => "Spruce Leaves",
52  self::BIRCH => "Birch Leaves",
53  self::JUNGLE => "Jungle Leaves",
54  ];
55  return $names[$this->meta & 0x03];
56  }
57 
58  private function findLog(Block $pos, array $visited, $distance, &$check, $fromSide = null){
59  ++$check;
60  $index = $pos->x . "." . $pos->y . "." . $pos->z;
61  if(isset($visited[$index])){
62  return false;
63  }
64  if($pos->getId() === self::WOOD){
65  return true;
66  }elseif($pos->getId() === self::LEAVES and $distance < 3){
67  $visited[$index] = true;
68  $down = $pos->getSide(0)->getId();
69  if($down === Item::WOOD){
70  return true;
71  }
72  if($fromSide === null){
73  for($side = 2; $side <= 5; ++$side){
74  if($this->findLog($pos->getSide($side), $visited, $distance + 1, $check, $side) === true){
75  return true;
76  }
77  }
78  }else{ //No more loops
79  switch($fromSide){
80  case 2:
81  if($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true){
82  return true;
83  }elseif($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true){
84  return true;
85  }elseif($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true){
86  return true;
87  }
88  break;
89  case 3:
90  if($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true){
91  return true;
92  }elseif($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true){
93  return true;
94  }elseif($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true){
95  return true;
96  }
97  break;
98  case 4:
99  if($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true){
100  return true;
101  }elseif($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true){
102  return true;
103  }elseif($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true){
104  return true;
105  }
106  break;
107  case 5:
108  if($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true){
109  return true;
110  }elseif($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true){
111  return true;
112  }elseif($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true){
113  return true;
114  }
115  break;
116  }
117  }
118  }
119 
120  return false;
121  }
122 
123  public function onUpdate($type){
124  if($type === Level::BLOCK_UPDATE_NORMAL){
125  if(($this->meta & 0b00001100) === 0){
126  $this->meta |= 0x08;
127  $this->getLevel()->setBlock($this, $this, false, false, true);
128  }
129  }elseif($type === Level::BLOCK_UPDATE_RANDOM){
130  if(($this->meta & 0b00001100) === 0x08){
131  $this->meta &= 0x03;
132  $visited = [];
133  $check = 0;
134 
135  Server::getInstance()->getPluginManager()->callEvent($ev = new LeavesDecayEvent($this));
136 
137  if($ev->isCancelled() or $this->findLog($this, $visited, 0, $check) === true){
138  $this->getLevel()->setBlock($this, $this, false, false);
139  }else{
140  $this->getLevel()->useBreakOn($this);
141 
142  return Level::BLOCK_UPDATE_NORMAL;
143  }
144  }
145  }
146 
147  return false;
148  }
149 
150  public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
151  $this->meta |= 0x04;
152  $this->getLevel()->setBlock($this, $this, true);
153  }
154 
155  public function getDrops(Item $item){
156  $drops = [];
157  if($item->isShears()){
158  $drops[] = [Item::LEAVES, $this->meta & 0x03, 1];
159  }else{
160  if(mt_rand(1, 20) === 1){ //Saplings
161  $drops[] = [Item::SAPLING, $this->meta & 0x03, 1];
162  }
163  if(($this->meta & 0x03) === self::OAK and mt_rand(1, 200) === 1){ //Apples
164  $drops[] = [Item::APPLE, 0, 1];
165  }
166  }
167 
168  return $drops;
169  }
170 }
static getInstance()
Definition: Server.php:1444
getSide($side, $step=1)
Definition: Block.php:1028