Skip to content

Commit

Permalink
Use BlockUpdateEvent
Browse files Browse the repository at this point in the history
Use BlockUpdateEvent instead so that the plugin will be compatible with plugins that use setBlock() to remove the ores. A plugin like the Minions plugin by CLADevs will be compatible after this change.
  • Loading branch information
AGTHARN authored Sep 13, 2020
1 parent cd5256a commit 74f2fb0
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions src/RKAbdul/OreSpawners/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use pocketmine\Server;
use pocketmine\utils\Config;
use pocketmine\item\Item;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\block\BlockUpdateEvent;
use pocketmine\block\Block;

class EventListener implements Listener{
Expand All @@ -28,43 +27,27 @@ public function __construct(Main $plugin){
$this->plugin = $plugin;
$this->cfg = $this->plugin->getConfig()->getAll();
}
public function onBlockBreak(BlockBreakEvent $event){
public function onBlockUpdate(BlockUpdateEvent $event){
$block = $event->getBlock();
$bbelow = $event->getPlayer()->getLevel()->getBlock($event->getBlock()->floor()->down(1));
$bbelow = $block->getLevel()->getBlock($event->getBlock()->floor()->down(1));
$blocks = [];
foreach(array_values($this->plugin->getConfig()->get("ore-generator-blocks")) as $blockID){
array_push($blocks, $blockID);
};
if (in_array($bbelow->getId(), $blocks)) {
$ore = $this->checkBlock($bbelow);
if (!$event->isCancelled()) {
$this->plugin->getScheduler()->scheduleDelayedTask(new ClosureTask( function (int $currentTick) use ($event, $ore): void {
$event->getPlayer()->getLevel()->setBlock($event->getBlock()->floor(), $ore);
}), 20 * intval($this->cfg["delay"]));
}
}

}

public function onBlockPlace(BlockPlaceEvent $event){
$block = $event->getBlock();
$babove = $event->getPlayer()->getLevel()->getBlock($event->getBlock()->floor()->up(1));
if ($babove->getId() == 0) {
$blocks = [];
foreach(array_values($this->plugin->getConfig()->get("ore-generator-blocks")) as $blockID){
array_push($blocks, $blockID);
};
if (in_array($block->getId(), $blocks)) {
$ore = $this->checkBlock($block);
if (!$event->isCancelled()) {
$this->plugin->getScheduler()->scheduleDelayedTask(new ClosureTask( function (int $currentTick) use ($event, $ore): void {
$event->getPlayer()->getLevel()->setBlock($event->getBlock()->floor()->up(1), $ore);
}), 20 * intval($this->cfg["delay"]));
}
}
}
}

foreach(array_values($this->plugin->getConfig()->get("ore-generator-blocks")) as $blockID){
array_push($blocks, $blockID);
};
if (in_array($bbelow->getId(), $blocks)) {
$ore = $this->checkBlock($bbelow);
if (!$event->isCancelled()) {
if ($event->getBlock()->getId() == $ore->getId()) return;
$this->plugin->getScheduler()->scheduleDelayedTask(new ClosureTask( function (int $currentTick) use ($event, $ore): void {
if ($event->getBlock()->getLevel() !== null){
$event->getBlock()->getLevel()->setBlock($event->getBlock()->floor(), $ore, false, true);
}
}), 20 * intval($this->cfg["delay"]));
}
}
}

public function checkBlock(Block $bbelow) {
$bbid = $bbelow->getID();
Expand Down Expand Up @@ -101,4 +84,3 @@ public function checkBlock(Block $bbelow) {
return $ore;
}
}

0 comments on commit 74f2fb0

Please sign in to comment.