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