Skip to content

Commit

Permalink
Fixing PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
matcracker committed May 6, 2020
1 parent 18869e0 commit e915f47
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 32 deletions.
11 changes: 5 additions & 6 deletions src/matcracker/BedcoreProtect/commands/BCPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use matcracker\BedcoreProtect\math\MathUtils;
use matcracker\BedcoreProtect\storage\QueryManager;
use matcracker\BedcoreProtect\ui\Forms;
use matcracker\BedcoreProtect\utils\Utils;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginIdentifiableCommand;
Expand All @@ -37,7 +38,6 @@
use pocketmine\plugin\Plugin;
use pocketmine\utils\TextFormat;
use SOFe\AwaitGenerator\Await;
use UnexpectedValueException;
use function count;
use function ctype_digit;
use function explode;
Expand Down Expand Up @@ -227,9 +227,7 @@ static function (): void {
if (isset($args[1])) {
$parser = new CommandParser($sender->getName(), $this->plugin->getParsedConfig(), $args, ['time', 'radius'], true);
if ($parser->parse()) {
if (($level = $sender->getLevel()) === null) {
throw new UnexpectedValueException($sender->getName() . " has an invalid world.");
}
$level = Utils::getLevelNonNull($sender->getLevel());
$sender->sendMessage(TextFormat::colorize(Main::MESSAGE_PREFIX . $lang->translateString('command.rollback.started', [$level->getName()])));

$bb = $this->getSelectionArea($sender) ?? MathUtils::getRangedVector($sender->asVector3(), $parser->getRadius() ?? 0);
Expand All @@ -246,10 +244,11 @@ static function (): void {
if (isset($args[1])) {
$parser = new CommandParser($sender->getName(), $this->plugin->getParsedConfig(), $args, ['time', 'radius'], true);
if ($parser->parse()) {
$sender->sendMessage(TextFormat::colorize(Main::MESSAGE_PREFIX . $lang->translateString('command.restore.started', [$sender->getLevel()->getName()])));
$level = Utils::getLevelNonNull($sender->getLevel());
$sender->sendMessage(TextFormat::colorize(Main::MESSAGE_PREFIX . $lang->translateString('command.restore.started', [$level->getName()])));

$bb = $this->getSelectionArea($sender) ?? MathUtils::getRangedVector($sender->asVector3(), $parser->getRadius());
$this->queryManager->restore(new Area($sender->getLevel(), $bb), $parser);
$this->queryManager->restore(new Area($level, $bb), $parser);
} else {
$sender->sendMessage(TextFormat::colorize(Main::MESSAGE_PREFIX . "&c{$parser->getErrorMessage()}"));
}
Expand Down
18 changes: 9 additions & 9 deletions src/matcracker/BedcoreProtect/listeners/BlockListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use matcracker\BedcoreProtect\enums\Action;
use matcracker\BedcoreProtect\utils\BlockUtils;
use matcracker\BedcoreProtect\utils\Utils;
use pocketmine\block\Bed;
use pocketmine\block\Block;
use pocketmine\block\Chest;
Expand All @@ -40,7 +41,6 @@
use pocketmine\tile\Chest as TileChest;
use function array_filter;
use function count;
use function var_dump;

final class BlockListener extends BedcoreListener
{
Expand Down Expand Up @@ -106,16 +106,18 @@ static function (Block $side): bool {
public function trackBlockPlace(BlockPlaceEvent $event): void
{
$player = $event->getPlayer();
if ($this->config->isEnabledWorld($player->getLevel()) && $this->config->getBlockPlace()) {
$level = Utils::getLevelNonNull($player->getLevel());

if ($this->config->isEnabledWorld($level) && $this->config->getBlockPlace()) {
$replacedBlock = $event->getBlockReplaced();
$block = $event->getBlock();

//HACK: Remove when issue PMMP#1760 is fixed (never).
$this->plugin->getScheduler()->scheduleDelayedTask(
new ClosureTask(
function (int $currentTick) use ($replacedBlock, $block, $player) : void {
function (int $currentTick) use ($replacedBlock, $block, $level, $player) : void {
//Update the block instance to get the real placed block data.
$updBlock = $block->getLevel()->getBlock($block->asVector3());
$updBlock = $level->getBlock($block->asVector3());

/** @var Block|null $otherHalfBlock */
$otherHalfBlock = null;
Expand Down Expand Up @@ -150,7 +152,7 @@ public function trackBlockSpread(BlockSpreadEvent $event): void
$block = $event->getBlock();
$source = $event->getSource();

if ($this->config->isEnabledWorld($block->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($block->getLevel()))) {
if ($source instanceof Liquid && $source->getId() === $source->getStillForm()->getId()) {
$this->blocksQueries->addBlockLogByBlock($source, $block, $source, Action::PLACE());
}
Expand All @@ -166,11 +168,9 @@ public function trackBlockSpread(BlockSpreadEvent $event): void
public function trackBlockBurn(BlockBurnEvent $event): void
{
$block = $event->getBlock();
if ($this->config->isEnabledWorld($block->getLevel()) && $this->config->getBlockBurn()) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($block->getLevel())) && $this->config->getBlockBurn()) {
$cause = $event->getCausingBlock();

var_dump($block->asVector3());

$this->blocksQueries->addBlockLogByBlock($cause, $block, $cause, Action::BREAK());
}
}
Expand All @@ -185,7 +185,7 @@ public function trackBlockForm(BlockFormEvent $event): void
{
$block = $event->getBlock();

if ($this->config->isEnabledWorld($block->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($block->getLevel()))) {
if ($block instanceof Liquid && $this->config->getLiquidTracking()) {
$result = $event->getNewState();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ final class BlockSniperListener extends BedcoreListener
*/
public function trackBrushUse(BrushUseEvent $event): void
{
if ($this->config->isEnabledWorld($event->getLevel()) && $this->config->getBlockSniperHook()) {
$level = $event->getLevel();

if ($this->config->isEnabledWorld($level) && $this->config->getBlockSniperHook()) {
/** @var Block[] $newBlocks */
$newBlocks = iterator_to_array($event->getShape()->getBlocksInside());
/** @var Block[] $oldBlocks */
$oldBlocks = array_map(static function (Block $block): Block {
return $block->getLevel()->getBlock($block->asVector3());
$oldBlocks = array_map(static function (Block $block) use ($level): Block {
return $level->getBlock($block->asVector3());
}, $newBlocks);
$this->blocksQueries->addBlocksLogByEntity($event->getPlayer(), $oldBlocks, $newBlocks, Action::PLACE());
}
Expand Down
11 changes: 6 additions & 5 deletions src/matcracker/BedcoreProtect/listeners/EntityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace matcracker\BedcoreProtect\listeners;

use matcracker\BedcoreProtect\enums\Action;
use matcracker\BedcoreProtect\utils\Utils;
use pocketmine\block\BlockFactory;
use pocketmine\entity\Human;
use pocketmine\entity\Living;
Expand All @@ -46,7 +47,7 @@ final class EntityListener extends BedcoreListener
public function trackEntityExplode(EntityExplodeEvent $event): void
{
$entity = $event->getEntity();
if ($this->config->isEnabledWorld($entity->getLevel()) && $this->config->getExplosions()) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($entity->getLevel())) && $this->config->getExplosions()) {
$this->blocksQueries->addBlocksLogByEntity($entity, $event->getBlockList(), $this->air, Action::BREAK());
}
}
Expand All @@ -65,7 +66,7 @@ public function trackEntitySpawn(EntitySpawnEvent $event): void
return;
}

if ($this->config->isEnabledWorld($entity->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($entity->getLevel()))) {
if ($entity instanceof FallingBlock && $this->config->getBlockMovement()) {
$this->blocksQueries->addBlockLogByEntity($entity, BlockFactory::get($entity->getBlock(), $entity->getDamage()), $this->air, Action::BREAK(), $entity->asPosition());

Expand Down Expand Up @@ -100,7 +101,7 @@ public function trackEntityDamageByEntity(EntityDamageByEntityEvent $event): voi
return;
}

if ($this->config->isEnabledWorld($entity->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($entity->getLevel()))) {
if ($entity instanceof Painting && $this->config->getBlockBreak()) {
$damager = $event->getDamager();
if ($damager !== null) {
Expand All @@ -123,7 +124,7 @@ public function trackEntityDeath(EntityDeathEvent $event): void
return;
}

if ($this->config->isEnabledWorld($entity->getLevel()) && $this->config->getEntityKills()) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($entity->getLevel())) && $this->config->getEntityKills()) {
$damageEvent = $entity->getLastDamageCause();
if ($damageEvent instanceof EntityDamageByEntityEvent) {
$damager = $damageEvent->getDamager();
Expand All @@ -145,7 +146,7 @@ public function trackEntityDeath(EntityDeathEvent $event): void
public function trackEntityBlockChange(EntityBlockChangeEvent $event): void
{
$entity = $event->getEntity();
if ($this->config->isEnabledWorld($entity->getLevel()) && $this->config->getBlockMovement()) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($entity->getLevel())) && $this->config->getBlockMovement()) {
$this->blocksQueries->addBlockLogByEntity($entity, $event->getBlock(), $event->getTo(), Action::PLACE(), $entity->asPosition());
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/matcracker/BedcoreProtect/listeners/InspectorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use matcracker\BedcoreProtect\Inspector;
use matcracker\BedcoreProtect\utils\BlockUtils;
use matcracker\BedcoreProtect\utils\Utils;
use pocketmine\block\ItemFrame;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function onInspectBlockBreak(BlockBreakEvent $event): void
{
$player = $event->getPlayer();

if ($this->config->isEnabledWorld($player->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($player->getLevel()))) {
if (Inspector::isInspector($player)) { //It checks the block clicked
$this->pluginQueries->requestBlockLog($player, $event->getBlock());
$event->setCancelled();
Expand All @@ -68,7 +69,7 @@ public function onInspectBlockPlace(BlockPlaceEvent $event): void
{
$player = $event->getPlayer();

if ($this->config->isEnabledWorld($player->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($player->getLevel()))) {
if (Inspector::isInspector($player)) { //It checks the block where the player places.
$this->pluginQueries->requestBlockLog($player, $event->getBlockReplaced());
$event->setCancelled();
Expand All @@ -85,7 +86,7 @@ public function onInspectBlockInteract(PlayerInteractEvent $event): void
{
$player = $event->getPlayer();

if ($this->config->isEnabledWorld($player->getLevel())) {
if ($this->config->isEnabledWorld(Utils::getLevelNonNull($player->getLevel()))) {
if (Inspector::isInspector($player)) {
$clickedBlock = $event->getBlock();
$itemInHand = $event->getItem();
Expand Down
7 changes: 1 addition & 6 deletions src/matcracker/BedcoreProtect/utils/ConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use BadMethodCallException;
use pocketmine\level\Level;
use pocketmine\utils\Config;
use UnexpectedValueException;
use function count;
use function date_default_timezone_set;
use function in_array;
Expand Down Expand Up @@ -88,16 +87,12 @@ public function getDatabaseFileName(): string
return (string)$this->data['database']['sqlite']['file'];
}

public function isEnabledWorld(?Level $world): bool
public function isEnabledWorld(Level $world): bool
{
if (!$this->isValid) {
throw new BadMethodCallException('The configuration must be validated.');
}

if ($world === null) {
throw new UnexpectedValueException('Got an invalid world.');
}

return in_array($world->getFolderName(), $this->getEnabledWorlds()) || count($this->getEnabledWorlds()) === 0;
}

Expand Down
10 changes: 10 additions & 0 deletions src/matcracker/BedcoreProtect/utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use DateTime;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\nbt\BigEndianNBTStream;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\NamedTag;
Expand Down Expand Up @@ -131,6 +132,15 @@ public static function timeAgo(int $timestamp, int $level = 6): string
return $string . ' ago';
}

public static function getLevelNonNull(?Level $level): Level //TODO: remove when PMMP adds it.
{
if ($level === null) {
throw new UnexpectedValueException("Position world is null");
}

return $level;
}

/**
* It serializes the CompoundTag to a Base64 string.
*
Expand Down

0 comments on commit e915f47

Please sign in to comment.