Skip to content

Commit

Permalink
PHPStan Level 8 (#56)
Browse files Browse the repository at this point in the history
* PHPStan Level 8

* PHPStan Level 8: Fix EventListener errors

* PHPStan Level 8: Fix remaining errors (hopefully?)

* PHPStan Level 8: Fix remaining errors (hopefully?) pt II

* PHPStan Level 8: Fix remaining errors (hopefully?) pt III
  • Loading branch information
DaPigGuy committed Jan 30, 2020
1 parent fe97415 commit 9273eb5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
@@ -1,7 +1,7 @@
parameters:
paths:
- /source/src
level: 7
level: 8
autoload_files:
- phar:///pocketmine/PocketMine-MP.phar/vendor/autoload.php
autoload_directories:
Expand Down
13 changes: 8 additions & 5 deletions src/DaPigGuy/PiggyCrates/EventListener.php
Expand Up @@ -10,6 +10,7 @@
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\inventory\ChestInventory;
use pocketmine\level\Level;
use pocketmine\tile\Chest;
use pocketmine\tile\Tile;
use pocketmine\utils\TextFormat;
Expand Down Expand Up @@ -39,9 +40,11 @@ public function onInteract(PlayerInteractEvent $event): void
{
$player = $event->getPlayer();
$block = $event->getBlock();
/** @var Level $level */
$level = $block->getLevel();
$item = $player->getInventory()->getItemInHand();
if ($block->getId() === Block::CHEST) {
$tile = $block->getLevel()->getTile($block);
$tile = $level->getTile($block);
if ($tile instanceof CrateTile) {
if ($tile->getCrateType() === null) {
$player->sendTip(TextFormat::RED . "Invalid or missing crate type.");
Expand All @@ -52,14 +55,14 @@ public function onInteract(PlayerInteractEvent $event): void
return;
}
if ($tile instanceof Chest) {
if (PiggyCrates::inCrateCreationMode($player)) {
if (($crate = PiggyCrates::getCrateToCreate($player)) !== null) {
$nbt = $tile->getSpawnCompound();
$nbt->setString("CrateType", PiggyCrates::getCrateToCreate($player)->getName());
$nbt->setString("CrateType", $crate->getName());
/** @var CrateTile $newTile */
$newTile = Tile::createTile("CrateTile", $event->getBlock()->getLevel(), $nbt);
$newTile = Tile::createTile("CrateTile", $level, $nbt);
$newTile->spawnToAll();
$tile->close();
$player->sendMessage(TextFormat::GREEN . PiggyCrates::getCrateToCreate($player)->getName() . " Crate created.");
$player->sendMessage(TextFormat::GREEN . $crate->getName() . " Crate created.");
PiggyCrates::setInCrateCreationMode($player, null);
$event->setCancelled();
return;
Expand Down
12 changes: 6 additions & 6 deletions src/DaPigGuy/PiggyCrates/tasks/RouletteTask.php
Expand Up @@ -37,17 +37,17 @@ class RouletteTask extends Task
public function __construct(CrateTile $tile)
{
$this->tile = $tile;
$this->itemsLeft = $tile->getCrateType()->getDropCount();
$this->itemsLeft = $tile->getCrateType() === null ? 0 : $tile->getCrateType()->getDropCount();
}

/**
* @param int $currentTick
*/
public function onRun(int $currentTick): void
{
if (!$this->tile->getCurrentPlayer() instanceof Player || !$this->tile->getCurrentPlayer()->isOnline()) {
if (!$this->tile->getCurrentPlayer() instanceof Player || !$this->tile->getCurrentPlayer()->isOnline() || ($crateType = $this->tile->getCrateType()) === null) {
$this->tile->closeCrate();
$this->getHandler()->cancel();
if (($handler = $this->getHandler()) !== null) $handler->cancel();
return;
}
$this->currentTick++;
Expand All @@ -62,14 +62,14 @@ public function onRun(int $currentTick): void
$this->tile->getCurrentPlayer()->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{PLAYER}", $this->tile->getCurrentPlayer()->getName(), $command));
}
if ($this->itemsLeft === 0) {
foreach ($this->tile->getCrateType()->getCommands() as $command) {
foreach ($crateType->getCommands() as $command) {
$this->tile->getCurrentPlayer()->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{PLAYER}", $this->tile->getCurrentPlayer()->getName(), $command));
}

$this->tile->getCurrentPlayer()->removeWindow($this->tile->getInventory());

$this->tile->closeCrate();
$this->getHandler()->cancel();
if (($handler = $this->getHandler()) !== null) $handler->cancel();
} else {
$this->currentTick = 0;
$this->showReward = false;
Expand All @@ -89,7 +89,7 @@ public function onRun(int $currentTick): void
$this->tile->getInventory()->setItem($slot - 1, $lastReward->getItem());
}
}
$lastRewards[17] = $this->tile->getCrateType()->getDrop(1)[0];
$lastRewards[17] = $crateType->getDrop(1)[0];
$this->tile->getInventory()->setItem(17, $lastRewards[17]->getItem());
$this->lastRewards = $lastRewards;
}
Expand Down
28 changes: 14 additions & 14 deletions src/DaPigGuy/PiggyCrates/tiles/CrateTile.php
Expand Up @@ -24,7 +24,7 @@ class CrateTile extends Chest
{
/** @var string */
public $crateName;
/** @var Crate */
/** @var Crate|null */
public $crateType;

/** @var bool */
Expand All @@ -49,13 +49,13 @@ public function getCrateType(): ?Crate
*/
public function openCrate(Player $player, Item $key): void
{
if ($this->crateType === null) return;
if (($crateType = $this->crateType) === null || ($level = $this->getLevel()) === null) return;
if ($this->isOpen) {
$player->sendTip(TextFormat::RED . "Crate is currently being opened.");
return;
}
if (count($player->getInventory()->getContents()) > $player->getInventory()->getSize() - $this->crateType->getDropCount()) {
$player->sendTip(TextFormat::RED . "You must have " . $this->crateType->getDropCount() . " empty slots.");
if (count($player->getInventory()->getContents()) > $player->getInventory()->getSize() - $crateType->getDropCount()) {
$player->sendTip(TextFormat::RED . "You must have " . $crateType->getDropCount() . " empty slots.");
return;
}

Expand All @@ -67,7 +67,7 @@ public function openCrate(Player $player, Item $key): void
$pk->z = $this->getFloorZ();
$pk->eventType = 1;
$pk->eventData = 1;
$this->getLevel()->broadcastPacketToViewers($this, $pk);
$level->broadcastPacketToViewers($this, $pk);

$this->getInventory()->clearAll();
$this->getInventory()->setItem(4, Item::get(Item::END_ROD, 0, 1));
Expand All @@ -79,13 +79,13 @@ public function openCrate(Player $player, Item $key): void
switch (PiggyCrates::$instance->getConfig()->getNested("crates.mode")) {
case "instant":
$this->closeCrate();
foreach ($this->crateType->getDrop($this->crateType->getDropCount()) as $drop) {
foreach ($crateType->getDrop($crateType->getDropCount()) as $drop) {
$player->getInventory()->addItem($drop->getItem());
foreach ($drop->getCommands() as $command) {
$player->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{PLAYER}", $player->getName(), $command));
}
}
foreach ($this->crateType->getCommands() as $command) {
foreach ($crateType->getCommands() as $command) {
$player->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{PLAYER}", $player->getName(), $command));
}
break;
Expand All @@ -98,15 +98,15 @@ public function openCrate(Player $player, Item $key): void

public function closeCrate(): void
{
if (!$this->isOpen) return;
if (!$this->isOpen || ($level = $this->getLevel()) === null) return;

$pk = new BlockEventPacket();
$pk->x = $this->getFloorX();
$pk->y = $this->getFloorY();
$pk->z = $this->getFloorZ();
$pk->eventType = 1;
$pk->eventData = 0;
$this->getLevel()->broadcastPacketToViewers($this, $pk);
$level->broadcastPacketToViewers($this, $pk);

$this->isOpen = false;
$this->currentPlayer = null;
Expand Down Expand Up @@ -173,22 +173,22 @@ public function addAdditionalSpawnData(CompoundTag $nbt): void
*/
public function onUpdate(): bool
{
if (!$this->closed && $this->crateType !== null && $this->crateType->getFloatingText() !== "") {
if (!$this->closed && ($level = $this->getLevel()) !== null && $this->crateType !== null && $this->crateType->getFloatingText() !== "") {
foreach ($this->floatingTextParticles as $key => $floatingTextParticle) {
/** @var Player $player */
$player = $floatingTextParticle[0];
/** @var FloatingTextParticle $particle */
$particle = $floatingTextParticle[1];
if (!$player->isOnline() || $player->getLevel() !== $this->level) {
if (!$player->isOnline() || $player->getLevel() !== $level) {
$particle->setInvisible();
$this->level->addParticle($particle, [$player]);
$level->addParticle($particle, [$player]);
unset($this->floatingTextParticles[$key]);
}
}
foreach ($this->level->getPlayers() as $player) {
foreach ($level->getPlayers() as $player) {
if (!isset($this->floatingTextParticles[$player->getName()])) {
$this->floatingTextParticles[$player->getName()] = [$player, new FloatingTextParticle($this->add(0.5, 1, 0.5), $this->crateType->getFloatingText())];
$this->level->addParticle($this->floatingTextParticles[$player->getName()][1], [$player]);
$level->addParticle($this->floatingTextParticles[$player->getName()][1], [$player]);
}
}
}
Expand Down

0 comments on commit 9273eb5

Please sign in to comment.