PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/pocketmine/block/Leaves.php

https://gitlab.com/wesleyvanneck/ImagicalMine
PHP | 248 lines | 147 code | 27 blank | 74 comment | 21 complexity | a0acdbb7c933fd9fc7097d175ae12b2c MD5 | raw file
  1. <?php
  2. /**
  3. * src/pocketmine/block/Leaves.php
  4. *
  5. * @package default
  6. */
  7. /*
  8. *
  9. * _ _ _ __ __ _
  10. * (_) (_) | | \/ (_)
  11. * _ _ __ ___ __ _ __ _ _ ___ __ _| | \ / |_ _ __ ___
  12. * | | '_ ` _ \ / _` |/ _` | |/ __/ _` | | |\/| | | '_ \ / _ \
  13. * | | | | | | | (_| | (_| | | (_| (_| | | | | | | | | | __/
  14. * |_|_| |_| |_|\__,_|\__, |_|\___\__,_|_|_| |_|_|_| |_|\___|
  15. * __/ |
  16. * |___/
  17. *
  18. * This program is a third party build by ImagicalMine.
  19. *
  20. * PocketMine is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Lesser General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * @author ImagicalMine Team
  26. * @link http://forums.imagicalcorp.ml/
  27. *
  28. *
  29. */
  30. namespace pocketmine\block;
  31. use pocketmine\event\block\LeavesDecayEvent;
  32. use pocketmine\item\Item;
  33. use pocketmine\item\Tool;
  34. use pocketmine\level\Level;
  35. use pocketmine\Player;
  36. use pocketmine\Server;
  37. class Leaves extends Transparent
  38. {
  39. const OAK = 0;
  40. const SPRUCE = 1;
  41. const BIRCH = 2;
  42. const JUNGLE = 3;
  43. const ACACIA = 0;
  44. const DARK_OAK = 1;
  45. protected $id = self::LEAVES;
  46. /**
  47. *
  48. * @param unknown $meta (optional)
  49. */
  50. public function __construct($meta = 0)
  51. {
  52. $this->meta = $meta;
  53. }
  54. /**
  55. *
  56. * @return unknown
  57. */
  58. public function getHardness()
  59. {
  60. return 0.2;
  61. }
  62. /**
  63. *
  64. * @return unknown
  65. */
  66. public function getToolType()
  67. {
  68. return Tool::TYPE_SHEARS;
  69. }
  70. /**
  71. *
  72. * @return unknown
  73. */
  74. public function getName()
  75. {
  76. static $names = [
  77. self::OAK => "Oak Leaves",
  78. self::SPRUCE => "Spruce Leaves",
  79. self::BIRCH => "Birch Leaves",
  80. self::JUNGLE => "Jungle Leaves",
  81. ];
  82. return $names[$this->meta & 0x03];
  83. }
  84. /**
  85. *
  86. * @param Block $pos
  87. * @param array $visited
  88. * @param unknown $distance
  89. * @param unknown $check (reference)
  90. * @param unknown $fromSide (optional)
  91. * @return unknown
  92. */
  93. private function findLog(Block $pos, array $visited, $distance, &$check, $fromSide = null)
  94. {
  95. ++$check;
  96. $index = $pos->x . "." . $pos->y . "." . $pos->z;
  97. if (isset($visited[$index])) {
  98. return false;
  99. }
  100. if ($pos->getId() === self::WOOD) {
  101. return true;
  102. } elseif ($pos->getId() === self::LEAVES and $distance < 3) {
  103. $visited[$index] = true;
  104. $down = $pos->getSide(0)->getId();
  105. if ($down === Item::WOOD) {
  106. return true;
  107. }
  108. if ($fromSide === null) {
  109. for ($side = 2; $side <= 5; ++$side) {
  110. if ($this->findLog($pos->getSide($side), $visited, $distance + 1, $check, $side) === true) {
  111. return true;
  112. }
  113. }
  114. } else { //No more loops
  115. switch ($fromSide) {
  116. case 2:
  117. if ($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true) {
  118. return true;
  119. } elseif ($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true) {
  120. return true;
  121. } elseif ($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true) {
  122. return true;
  123. }
  124. break;
  125. case 3:
  126. if ($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true) {
  127. return true;
  128. } elseif ($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true) {
  129. return true;
  130. } elseif ($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true) {
  131. return true;
  132. }
  133. break;
  134. case 4:
  135. if ($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true) {
  136. return true;
  137. } elseif ($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true) {
  138. return true;
  139. } elseif ($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true) {
  140. return true;
  141. }
  142. break;
  143. case 5:
  144. if ($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true) {
  145. return true;
  146. } elseif ($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true) {
  147. return true;
  148. } elseif ($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true) {
  149. return true;
  150. }
  151. break;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. *
  159. * @param unknown $type
  160. * @return unknown
  161. */
  162. public function onUpdate($type)
  163. {
  164. if ($type === Level::BLOCK_UPDATE_NORMAL) {
  165. if (($this->meta & 0b00001100) === 0) {
  166. $this->meta |= 0x08;
  167. $this->getLevel()->setBlock($this, $this, false, false, true);
  168. }
  169. } elseif ($type === Level::BLOCK_UPDATE_RANDOM) {
  170. if (($this->meta & 0b00001100) === 0x08) {
  171. $this->meta &= 0x03;
  172. $visited = [];
  173. $check = 0;
  174. Server::getInstance()->getPluginManager()->callEvent($ev = new LeavesDecayEvent($this));
  175. if ($ev->isCancelled() or $this->findLog($this, $visited, 0, $check) === true) {
  176. $this->getLevel()->setBlock($this, $this, false, false);
  177. } else {
  178. $this->getLevel()->useBreakOn($this);
  179. return Level::BLOCK_UPDATE_NORMAL;
  180. }
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. *
  187. * @param Item $item
  188. * @param Block $block
  189. * @param Block $target
  190. * @param unknown $face
  191. * @param unknown $fx
  192. * @param unknown $fy
  193. * @param unknown $fz
  194. * @param Player $player (optional)
  195. */
  196. public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null)
  197. {
  198. $this->meta |= 0x04;
  199. $this->getLevel()->setBlock($this, $this, true);
  200. }
  201. /**
  202. *
  203. * @param Item $item
  204. * @return unknown
  205. */
  206. public function getDrops(Item $item)
  207. {
  208. $drops = [];
  209. if ($item->isShears()) {
  210. $drops[] = [Item::LEAVES, $this->meta & 0x03, 1];
  211. } else {
  212. if (mt_rand(1, 20) === 1) { //Saplings
  213. $drops[] = [Item::SAPLING, $this->meta & 0x03, 1];
  214. }
  215. if (($this->meta & 0x03) === self::OAK and mt_rand(1, 200) === 1) { //Apples
  216. $drops[] = [Item::APPLE, 0, 1];
  217. }
  218. }
  219. return $drops;
  220. }
  221. }