Skip to content

Commit

Permalink
Improved Formatting & Added Documentation (#14)
Browse files Browse the repository at this point in the history
* Improved Formatting & Added Documentation

* phpstan

* phpstan x2

* phpstan x3

* phpstan x4
  • Loading branch information
AGTHARN authored Dec 4, 2020
1 parent 9fd72a8 commit 665663b
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 87 deletions.
217 changes: 140 additions & 77 deletions src/RKAbdul/OreSpawners/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@ 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 @@ -53,6 +66,7 @@ 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 @@ -63,54 +77,14 @@ public function onBlockUpdate(BlockUpdateEvent $event)
}
}
}

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;
}


/**
* 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 onBlockPlace(BlockPlaceEvent $event)
{
$block = $event->getBlock();
Expand All @@ -132,6 +106,49 @@ 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);
}
}
}

/**
* 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 Down Expand Up @@ -173,38 +190,57 @@ public function onPlayerInteract(PlayerInteractEvent $event): bool
return false;
}

public function getTile(Vector3 $pos): ?Tile
{
return $this->getTileAt((int)floor($pos->x), (int)floor($pos->y), (int)floor($pos->z));
}

public function onBlockBreak(BlockBreakEvent $event)
/**
* Checks the OreSpawner spawning block type.
*
* @param Block $bbelow
* @return object|bool
*/
public function checkBlock(Block $bbelow)
{
$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);
$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 ($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 (isset($ore)) {
return $ore;
}
return false;
}


/**
* Checks the OreSpawner type.
*
* @param Block $bbelow
* @return string|bool
*/
public function checkSpawner(Block $bbelow)
{
$bbid = $bbelow->getId();
Expand Down Expand Up @@ -243,4 +279,31 @@ 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));
}
}
36 changes: 26 additions & 10 deletions src/RKAbdul/OreSpawners/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,37 @@ class Main extends PluginBase
public const VERSION = 4;

private $cfg;


/**
* Disables the plugin if config version is below const VERSION.
*
* @return void
*/
public function onEnable()
{
if (!file_exists($this->getDataFolder())) {
@mkdir($this->getDataFolder());
} else if (!file_exists($this->getDataFolder() . "config.yml")) {
$this->getLogger()->info("Config Not Found! Creating new config...");
$this->saveDefaultConfig();
}
$this->cfg = $this->getConfig()->getAll();

if ($this->cfg["version"] < self::VERSION) {
$this->getLogger()->error("Config Version is outdated! Please delete your current config file!");
$this->getServer()->getPluginManager()->disablePlugin($this);
}

$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
EzTiles::register($this);
EzTiles::init();
}


/**
* Checks if the OreSpawner command is run.
*
* @param CommandSender $sender
* @param Command $command
* @param string $label
* @param array $args
* @return bool
*/
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool
{

if ($command->getName() === "orespawner") {
$typesArray = ["coal", "lapis", "iron", "gold", "diamond", "emerald", "redstone"];
if (!$sender->hasPermission("orespawner.give")) {
Expand Down Expand Up @@ -72,7 +81,14 @@ public function onCommand(CommandSender $sender, Command $command, string $label
}
return false;
}


/**
* Creates OreSpawners from given arguments.
*
* @param string $ore
* @param int $amount
* @return object
*/
public function createOreSpawner(string $ore, int $amount)
{
$gen = $this->cfg["ore-generator-blocks"][$ore];
Expand Down

0 comments on commit 665663b

Please sign in to comment.