|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace NhanAZ\BlockData_Example_Plugin; |
| 6 | + |
| 7 | +use NhanAZ\BlockData\BlockData; |
| 8 | +use pocketmine\block\VanillaBlocks; |
| 9 | +use pocketmine\event\block\BlockBreakEvent; |
| 10 | +use pocketmine\event\block\BlockPlaceEvent; |
| 11 | +use pocketmine\event\Listener; |
| 12 | +use pocketmine\event\player\PlayerInteractEvent; |
| 13 | +use pocketmine\item\VanillaItems; |
| 14 | +use pocketmine\plugin\PluginBase; |
| 15 | +use pocketmine\utils\TextFormat; |
| 16 | + |
| 17 | +final class Main extends PluginBase implements Listener { |
| 18 | + |
| 19 | + protected BlockData $blockdata; |
| 20 | + |
| 21 | + protected function onEnable(): void { |
| 22 | + $this->getServer()->getPluginManager()->registerEvents($this, $this); |
| 23 | + $this->blockdata = new BlockData($this); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @param PlayerInteractEvent $event |
| 28 | + * @priority MONITOR |
| 29 | + */ |
| 30 | + public function onPlayerInteract(PlayerInteractEvent $event): void { |
| 31 | + if ($event->getAction() === PlayerInteractEvent::RIGHT_CLICK_BLOCK && $event->getItem()->equals(VanillaItems::POTATO())) { |
| 32 | + $block = $event->getBlock(); |
| 33 | + if ($block->isSameType(VanillaBlocks::GLASS())) { |
| 34 | + $data = $this->blockdata->getData($block); /* "durability:4" */ |
| 35 | + $data = explode(":", $data); /* $data[0] = "durability" * $data[1] = 4 */ |
| 36 | + $event->getPlayer()->sendMessage(TextFormat::LIGHT_PURPLE . "Durability of this block is: " . $data[1]); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param BlockPlaceEvent $event |
| 43 | + * @priority HIGH |
| 44 | + */ |
| 45 | + public function onBlockPlace(BlockPlaceEvent $event): void { |
| 46 | + $block = $event->getBlock(); |
| 47 | + if ($block->isSameType(VanillaBlocks::GLASS())) { |
| 48 | + $this->blockdata->setData($block, "durability:4"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param BlockBreakEvent $event |
| 54 | + * @priority HIGH |
| 55 | + */ |
| 56 | + public function onBlockBreak(BlockBreakEvent $event): void { |
| 57 | + $block = $event->getBlock(); |
| 58 | + if ($block->isSameType(VanillaBlocks::GLASS())) { |
| 59 | + $data = $this->blockdata->getData($block); /* "durability:4" */ |
| 60 | + if ($data !== null) { |
| 61 | + $data = explode(":", $data); /* $data[0] = "durability" * $data[1] = 4 */ |
| 62 | + $durability = $data[1]; |
| 63 | + if ($durability > 1) { |
| 64 | + $event->cancel(); |
| 65 | + $durability = $durability - 1; |
| 66 | + $this->blockdata->setData($block, "durability:{$durability}"); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments