PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pocketmine/block/Dispenser.php

https://gitlab.com/wesleyvanneck/ImagicalMine
PHP | 333 lines | 175 code | 48 blank | 110 comment | 22 complexity | 272c2ee589e4347e3d7b89d22ae87986 MD5 | raw file
  1. <?php
  2. /**
  3. * src/pocketmine/block/Dispenser.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\item\Item;
  32. use pocketmine\item\Tool;
  33. use pocketmine\nbt\NBT;
  34. use pocketmine\nbt\tag\CompoundTag;
  35. use pocketmine\nbt\tag\ListTag;
  36. use pocketmine\nbt\tag\IntTag;
  37. use pocketmine\nbt\tag\StringTag;
  38. use pocketmine\Player;
  39. use pocketmine\tile\Tile;
  40. use pocketmine\tile\Dispenser as TileDispenser;
  41. use pocketmine\item\Bucket;
  42. class Dispenser extends Solid implements RedstoneConsumer
  43. {
  44. protected $id = self::DISPENSER;
  45. /**
  46. *
  47. * @param unknown $meta (optional)
  48. */
  49. public function __construct($meta = 0)
  50. {
  51. $this->meta = $meta;
  52. }
  53. /**
  54. *
  55. * @return unknown
  56. */
  57. public function getName()
  58. {
  59. return "Dispenser";
  60. }
  61. /**
  62. *
  63. * @return unknown
  64. */
  65. public function canBeActivated()
  66. {
  67. //At the moment disable, prevent servers crash (For devs, put true if you want check error)
  68. return true;
  69. }
  70. /**
  71. *
  72. * @return unknown
  73. */
  74. public function getHardness()
  75. {
  76. return 3.5;
  77. }
  78. /**
  79. *
  80. * @return unknown
  81. */
  82. public function getToolType()
  83. {
  84. return Tool::TYPE_PICKAXE;
  85. }
  86. /**
  87. *
  88. * @param Item $item
  89. * @param Block $block
  90. * @param Block $target
  91. * @param unknown $face
  92. * @param unknown $fx
  93. * @param unknown $fy
  94. * @param unknown $fz
  95. * @param Player $player (optional)
  96. * @return unknown
  97. */
  98. public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null)
  99. {
  100. $faces = [
  101. 0 => 4,
  102. 1 => 2,
  103. 2 => 5,
  104. 3 => 3
  105. ];
  106. $this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0];
  107. $this->getLevel()->setBlock($block, $this, true, true);
  108. $nbt = new CompoundTag("", [
  109. new ListTag("Items", []),
  110. new StringTag("id", Tile::DISPENSER),
  111. new IntTag("x", $this->x),
  112. new IntTag("y", $this->y),
  113. new IntTag("z", $this->z)
  114. ]);
  115. $nbt->Items->setTagType(NBT::TAG_Compound);
  116. if ($item->hasCustomName()) {
  117. $nbt->CustomName = new StringTag("CustomName", $item->getCustomName());
  118. }
  119. if ($item->hasCustomBlockData()) {
  120. foreach ($item->getCustomBlockData() as $key => $v) {
  121. $nbt->{$key} = $v;
  122. }
  123. }
  124. Tile::createTile("Dispenser", $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
  125. return true;
  126. }
  127. /**
  128. *
  129. * @return unknown
  130. */
  131. public function getDirection()
  132. {
  133. return $this->meta & 0x07;
  134. }
  135. /**
  136. *
  137. * @param Item $item
  138. * @param Player $player (optional)
  139. * @return unknown
  140. */
  141. public function onActivate(Item $item, Player $player = null)
  142. {
  143. if ($player instanceof Player) {
  144. $t = $this->getLevel()->getTile($this);
  145. $dispenser = null;
  146. if ($t instanceof TileDispenser) {
  147. $dispenser = $t;
  148. } else {
  149. $nbt = new CompoundTag("", [
  150. new ListTag("Items", []),
  151. new StringTag("id", Tile::DISPENSER),
  152. new IntTag("x", $this->x),
  153. new IntTag("y", $this->y),
  154. new IntTag("z", $this->z)
  155. ]);
  156. $nbt->Items->setTagType(NBT::TAG_Compound);
  157. $dispenser = Tile::createTile("Dispenser", $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
  158. }
  159. if (isset($dispenser->namedtag->Lock) and $dispenser->namedtag->Lock instanceof StringTag) {
  160. if ($dispenser->namedtag->Lock->getValue() !== $item->getCustomName()) {
  161. return true;
  162. }
  163. }
  164. $player->addWindow($dispenser->getInventory());
  165. }
  166. return true;
  167. }
  168. /**
  169. *
  170. * @param Item $item
  171. * @return unknown
  172. */
  173. public function getDrops(Item $item)
  174. {
  175. $drops = [];
  176. if ($item->isPickaxe() >= Tool::TIER_WOODEN) {
  177. $drops[] = [$this->id, 3, 1];
  178. }
  179. return $drops;
  180. }
  181. /**
  182. *
  183. * @return unknown
  184. */
  185. public function isPowered()
  186. {
  187. return ($this->meta & 0x08) === 0x08;
  188. }
  189. /**
  190. * Toggles the current state of this plate
  191. */
  192. public function togglePowered()
  193. {
  194. $this->meta ^= 0x08;
  195. $this->isPowered()?$this->power=15:$this->power=0;
  196. $this->getLevel()->setBlock($this, $this, true, true);
  197. }
  198. /**
  199. *
  200. * @param unknown $type
  201. * @param unknown $power
  202. */
  203. public function onRedstoneUpdate($type, $power)
  204. {
  205. if (!$this->isPowered() and $this->isCharged()) {
  206. // Power Up
  207. $this->togglePowered();
  208. // Check if Empty
  209. $dispenserTile = $this->getLevel()->getTile($this);
  210. $inventory = $dispenserTile->getInventory();
  211. $filledSlots = [];
  212. for ($i = 0; $i < $inventory->getSize(); ++$i) {
  213. if (!($inventory->getItem($i)->getId() === Item::AIR or $inventory->getItem($i)->getCount() <= 0)) {
  214. $filledSlots[] = $i;
  215. }
  216. }
  217. if (count($filledSlots) === 0) {
  218. // Dispenser is empty so make sound of being empty - Need to work out the sound emmited
  219. //$this->getLevel()->addSound(new ClickSound($this, 500));
  220. //Server::getInstance()->getLogger()->debug("!EMPTY!");
  221. } else {
  222. // Not empty so need to randomly deploy an item
  223. $chosenSlot = $filledSlots[mt_rand(0, count($filledSlots)-1)];
  224. // Get Item from Inventory
  225. $item = $inventory->getItem($chosenSlot);
  226. // Depending on Item Type do different actions
  227. if ($item instanceof Bucket) {
  228. if ($item->getDamage() === 0) {
  229. // Bucket Empty
  230. // Update Inventory Count
  231. $item->setCount($item->getCount() - 1);
  232. $inventory->setItem($chosenSlot, $item);
  233. if ($this->getSide($this->getDirection()) instanceof StillWater) {
  234. // Water on Side, so fill bucket
  235. // Remove Water
  236. $this->getLevel()->setBlock($this->getSide($this->getDirection()), new Air());
  237. // Create Bucket
  238. $filledBucket = Item::get(Item::BUCKET, Block::WATER, 1);
  239. if ($inventory->canAddItem($filledBucket)) {
  240. $inventory->addItem($filledBucket);
  241. } else {
  242. $this->getLevel()->dropItem($this->getSide($this->getDirection()), $filledBucket);
  243. }
  244. } elseif ($this->getSide($this->getDirection()) instanceof StillLava) {
  245. // Lava on Side, so fill bucket
  246. // Remove Lava
  247. $this->getLevel()->setBlock($this->getSide($this->getDirection()), new Air());
  248. // Create Bucket
  249. $filledBucket = Item::get(Item::BUCKET, Block::LAVA, 1);
  250. if ($inventory->canAddItem($filledBucket)) {
  251. $inventory->addItem($filledBucket);
  252. } else {
  253. $this->getLevel()->dropItem($this->getSide($this->getDirection()), $filledBucket);
  254. }
  255. } else {
  256. // Drop Item
  257. $item->setCount(1);
  258. $this->getLevel()->dropItem($this->getSide($this->getDirection()), $item);
  259. }
  260. } elseif (Block::get($item->getDamage()) instanceof Water) {
  261. // Water Bucket
  262. $this->getLevel()->setBlock($this->getSide($this->getDirection()), new StillWater());
  263. $inventory->clear($chosenSlot);
  264. $inventory->addItem(Item::get(Item::BUCKET));
  265. } elseif (Block::get($item->getDamage()) instanceof Lava) {
  266. // Lava Bucket
  267. $this->getLevel()->setBlock($this->getSide($this->getDirection()), new StillLava());
  268. $inventory->clear($chosenSlot);
  269. $inventory->addItem(Item::get(Item::BUCKET));
  270. }
  271. } else {
  272. // Update Inventory Count
  273. $item->setCount($item->getCount() - 1);
  274. $inventory->setItem($chosenSlot, $item);
  275. // Drop Item
  276. $item->setCount(1);
  277. $this->getLevel()->dropItem($this->getSide($this->getDirection()), $item);
  278. }
  279. }
  280. } elseif ($this->isPowered() and !$this->isCharged()) {
  281. // Power Down
  282. $this->togglePowered();
  283. }
  284. }
  285. }