Skip to content

Commit 33ca94d

Browse files
committed
BlockData_Example_Plugin
1 parent f6cff79 commit 33ca94d

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# BlockData_Example_Plugin
2-
Example plugin to showcase how BlockData read-writes work.
2+
This plugin gives glass a durability of 4. Players will have to break glass 4 times to successfully break it.
3+
Right-click the glass with a potato to check it's durability.

plugin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: BlockData_Example_Plugin
2+
main: NhanAZ\BlockData_Example_Plugin\Main
3+
api: 4.0.0
4+
version: 0.0.1
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)