Skip to content

Commit

Permalink
Fixed delay bug
Browse files Browse the repository at this point in the history
Added so blocks can't be placed over an OreSpawner
  • Loading branch information
RKAbdul authored Aug 7, 2021
1 parent 665663b commit 70d3068
Showing 1 changed file with 85 additions and 141 deletions.
226 changes: 85 additions & 141 deletions src/RKAbdul/OreSpawners/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\block\BlockUpdateEvent;

use DenielWorld\EzTiles\data\TileInfo;
use DenielWorld\EzTiles\tile\SimpleTile;
use RKAbdul\OreSpawners\libs\DenielWorld\EzTiles\data\TileInfo;
use RKAbdul\OreSpawners\libs\DenielWorld\EzTiles\tile\SimpleTile;

class EventListener implements Listener
{
Expand All @@ -29,26 +29,13 @@ class EventListener implements Listener
private $plugin;

private $cfg;

/**
* Initialize objects.
*
* @param Main $plugin
* @return void
*/

public function __construct(Main $plugin)
{
$this->plugin = $plugin;
$this->cfg = $this->plugin->getConfig()->getAll();
}

/**
* Checks if a block is updated and if it is an OreSpawner.
* If so, a new ore will be created.
*
* @param BlockUpdateEvent $event
* @return void
*/
public function onBlockUpdate(BlockUpdateEvent $event)
{
$block = $event->getBlock();
Expand All @@ -66,7 +53,6 @@ public function onBlockUpdate(BlockUpdateEvent $event)
$delay = $this->getDelay($bbelow);
if (!$event->isCancelled()) {
$event->setCancelled(true);
/** @phpstan-ignore-next-line */
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) {
Expand All @@ -77,14 +63,54 @@ public function onBlockUpdate(BlockUpdateEvent $event)
}
}
}

/**
* Checks if a block is placed and if it is an OreSpawner.
* If so, a new tile is created.
*
* @param BlockPlaceEvent $event
* @return void
*/

public function checkBlock(Block $bbelow)
{
$bbid = $bbelow->getId();
$coalid = intval($this->cfg["ore-generator-blocks"]["coal"]);
$ironid = intval($this->cfg["ore-generator-blocks"]["iron"]);
$goldid = intval($this->cfg["ore-generator-blocks"]["gold"]);
$diamondid = intval($this->cfg["ore-generator-blocks"]["diamond"]);
$emeraldid = intval($this->cfg["ore-generator-blocks"]["emerald"]);
$lapizid = intval($this->cfg["ore-generator-blocks"]["lapis"]);
$redstoneid = intval($this->cfg["ore-generator-blocks"]["redstone"]);
switch ($bbid) {
case $coalid:
$ore = Block::get(Block::COAL_ORE);
break;
case $ironid:
$ore = Block::get(Block::IRON_ORE);
break;
case $goldid:
$ore = Block::get(Block::GOLD_ORE);
break;
case $diamondid:
$ore = Block::get(Block::DIAMOND_ORE);
break;
case $emeraldid:
$ore = Block::get(Block::EMERALD_ORE);
break;
case $lapizid:
$ore = Block::get(Block::LAPIS_ORE);
break;
case $redstoneid:
$ore = Block::get(Block::REDSTONE_ORE);
break;
}
if (isset($ore)) {
return $ore;
}
return false;
}

public function getDelay(Block $block)
{
$tile = $block->getLevel()->getTile($block->asVector3());
$stacked = $tile->getData("stacked")->getValue();
$base = intval($this->cfg["base-delay"]);
return ($base / $stacked) * 20;
}

public function onBlockPlace(BlockPlaceEvent $event)
{
$block = $event->getBlock();
Expand All @@ -104,51 +130,15 @@ public function onBlockPlace(BlockPlaceEvent $event)
}
}
}
}

/**
* Checks if a block is broken and if it is an OreSpawner.
* If so, the player gets back their OreSpawner(s).
*
* @param BlockBreakEvent $event
* @return void
*/
public function onBlockBreak(BlockBreakEvent $event)
{
$player = $event->getPlayer();

$block = $event->getBlock();
$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 ($event->isCancelled()) return;
if (in_array($event->getBlock()->getId(), $blocks)) {
$tile = $event->getBlock()->getLevel()->getTile($block);
if (!$tile instanceof SimpleTile) return;
$tile = $player->getLevel()->getTile($block->asVector3());
$type = $this->checkSpawner($block);
$count = $tile instanceof SimpleTile ? $tile->getData("stacked")->getValue() : 1;
$orespawner = $this->plugin->createOreSpawner($type, $count);
$drops = array();
$drops[] = $orespawner;
$event->setDrops($drops);
} else if (in_array($bbelow->getId(), $blocks)) {
if ($this->cfg["drop-xp"] == false) {
$event->setXpDropAmount(0);
}
if ($this->checkBlock($bbelow)) {
$event->setCancelled(true);
$event->getPlayer()->sendMessage(Tf::RED . "You can not place blocks over an OreSpawner!");
}
}

/**
* Checks if a player has interacted and if the interacted
* block is an OreSpawner through tiles. If so, OreSpawners
* can be stacked and/or OreSpawners in the block can be
* checked.
*
* @param PlayerInteractEvent $event
* @return bool
*/

public function onPlayerInteract(PlayerInteractEvent $event): bool
{
if ($this->cfg["stacking"] == false || $event->isCancelled()) return false;
Expand All @@ -163,7 +153,7 @@ public function onPlayerInteract(PlayerInteractEvent $event): bool
if ($tile instanceof SimpleTile) {
if (!$player->getGamemode() == 1) {
$stacked = $tile->getData("stacked")->getValue();
if ($item->getNamedTag()->hasTag("orespawner")) {
if (in_array($item->getId(), $blocks) && $item->getNamedTag()->hasTag("orespawner")) {
if ($event->getBlock()->getId() == $item->getId()) {
if (!($stacked >= intval($this->cfg["max"]))) {
$event->setCancelled(true);
Expand All @@ -190,57 +180,38 @@ public function onPlayerInteract(PlayerInteractEvent $event): bool
return false;
}

/**
* Checks the OreSpawner spawning block type.
*
* @param Block $bbelow
* @return object|bool
*/
public function checkBlock(Block $bbelow)
public function getTile(Vector3 $pos): ?Tile
{
$bbid = $bbelow->getId();
$coalid = intval($this->cfg["ore-generator-blocks"]["coal"]);
$ironid = intval($this->cfg["ore-generator-blocks"]["iron"]);
$goldid = intval($this->cfg["ore-generator-blocks"]["gold"]);
$diamondid = intval($this->cfg["ore-generator-blocks"]["diamond"]);
$emeraldid = intval($this->cfg["ore-generator-blocks"]["emerald"]);
$lapizid = intval($this->cfg["ore-generator-blocks"]["lapis"]);
$redstoneid = intval($this->cfg["ore-generator-blocks"]["redstone"]);
switch ($bbid) {
case $coalid:
$ore = Block::get(Block::COAL_ORE);
break;
case $ironid:
$ore = Block::get(Block::IRON_ORE);
break;
case $goldid:
$ore = Block::get(Block::GOLD_ORE);
break;
case $diamondid:
$ore = Block::get(Block::DIAMOND_ORE);
break;
case $emeraldid:
$ore = Block::get(Block::EMERALD_ORE);
break;
case $lapizid:
$ore = Block::get(Block::LAPIS_ORE);
break;
case $redstoneid:
$ore = Block::get(Block::REDSTONE_ORE);
break;
return $this->getTileAt((int)floor($pos->x), (int)floor($pos->y), (int)floor($pos->z));
}

public function onBlockBreak(BlockBreakEvent $event)
{
$player = $event->getPlayer();
$block = $event->getBlock();
$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 (isset($ore)) {
return $ore;
if ($event->isCancelled()) return;
if (in_array($event->getBlock()->getId(), $blocks)) {
$tile = $event->getBlock()->getLevel()->getTile($block);
if (!$tile instanceof SimpleTile) return;
$tile = $player->getLevel()->getTile($block->asVector3());
$type = $this->checkSpawner($block);
$count = $tile instanceof SimpleTile ? $tile->getData("stacked")->getValue() : 1;
$orespawner = $this->plugin->createOreSpawner($type, $count);
$drops = array();
$drops[] = $orespawner;
$event->setDrops($drops);
} else if (in_array($bbelow->getId(), $blocks)) {
if ($this->cfg["drop-xp"] == false) {
$event->setXpDropAmount(0);
}
}
return false;
}

/**
* Checks the OreSpawner type.
*
* @param Block $bbelow
* @return string|bool
*/

public function checkSpawner(Block $bbelow)
{
$bbid = $bbelow->getId();
Expand Down Expand Up @@ -279,31 +250,4 @@ public function checkSpawner(Block $bbelow)
}
return false;
}

/**
* Calculates delay till the next ore spawns.
*
* @param Block $block
* @return int
*/
public function getDelay(Block $block)
{
$tile = $block->getLevel()->getTile($block->asVector3());
/** @phpstan-ignore-next-line */
$stacked = $tile->getData("stacked")->getValue();
$base = intval($this->cfg["base-delay"]);
return ($base / $stacked) * 20;
}

/**
* Returns the tile from a Vector3 position.
*
* @param Vector3 $pos
* @return Tile
*/
public function getTile(Vector3 $pos): ?Tile
{
/** @phpstan-ignore-next-line */
return $this->getTileAt((int)floor($pos->x), (int)floor($pos->y), (int)floor($pos->z));
}
}

0 comments on commit 70d3068

Please sign in to comment.