Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
matcracker committed Apr 11, 2020
1 parent 7deffd1 commit 3d1703f
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/matcracker/BedcoreProtect/commands/CommandParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use pocketmine\block\BlockIds;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\lang\BaseLang;
use pocketmine\math\AxisAlignedBB;
use pocketmine\Server;
use UnexpectedValueException;
Expand Down Expand Up @@ -74,6 +75,9 @@ final class CommandParser
private $requiredParams;
/** @var bool */
private $parsed = false;

/** @var BaseLang */
private $lang;
/** @var string */
private $errorMessage;

Expand Down Expand Up @@ -109,6 +113,8 @@ public function __construct(string $senderName, ConfigParser $configParser, arra
if (($dr = $this->configParser->getDefaultRadius()) !== 0) {
$this->data['radius'] = $dr;
}

$this->lang = Main::getInstance()->getLanguage();
}

/**
Expand All @@ -132,18 +138,16 @@ public static function initActions(): void

public function parse(): bool
{
$lang = Main::getInstance()->getLanguage();

if (($c = count($this->arguments)) < 1 || $c > self::MAX_PARAMETERS) {
$this->errorMessage = $lang->translateString('parser.few-many-parameters', [self::MAX_PARAMETERS]);
$this->errorMessage = $this->lang->translateString('parser.few-many-parameters', [self::MAX_PARAMETERS]);

return false;
}

foreach ($this->arguments as $argument) {
$arrayData = explode("=", $argument);
if (count($arrayData) !== 2) {
$this->errorMessage = $lang->translateString('parser.invalid-parameter', [implode(',', array_keys(self::$ACTIONS))]);
$this->errorMessage = $this->lang->translateString('parser.invalid-parameter', [implode(',', array_keys(self::$ACTIONS))]);

return false;
}
Expand All @@ -167,12 +171,12 @@ public function parse(): bool
if (mb_substr($user, 0, 1) === '#') {
$user = mb_substr($user, 1);
if (!in_array($user, Utils::getEntitySaveNames())) {
$this->errorMessage = $lang->translateString('parser.no-entity', [$user]);
$this->errorMessage = $this->lang->translateString('parser.no-entity', [$user]);

return false;
}
} elseif (!Server::getInstance()->getOfflinePlayer($user)->hasPlayedBefore()) {
$this->errorMessage = $lang->translateString('parser.no-player', [$user]);
$this->errorMessage = $this->lang->translateString('parser.no-player', [$user]);

return false;
}
Expand All @@ -183,7 +187,7 @@ public function parse(): bool
case 't':
$time = Utils::parseTime($paramValues);
if ($time === null) {
$this->errorMessage = $lang->translateString('parser.invalid-amount-time');
$this->errorMessage = $this->lang->translateString('parser.invalid-amount-time');

return false;
}
Expand All @@ -192,14 +196,14 @@ public function parse(): bool
case 'radius':
case 'r':
if (!ctype_digit($paramValues)) {
$this->errorMessage = $lang->translateString('parser.invalid-amount-radius');
$this->errorMessage = $this->lang->translateString('parser.invalid-amount-radius');

return false;
}
$paramValues = (int)$paramValues;
$maxRadius = $this->configParser->getMaxRadius();
if ($paramValues < 0 || ($maxRadius !== 0 && $paramValues > $maxRadius)) {
$this->errorMessage = $lang->translateString('parser.invalid-radius');
$this->errorMessage = $this->lang->translateString('parser.invalid-radius');

return false;
}
Expand All @@ -210,7 +214,7 @@ public function parse(): bool
case 'a':
$paramValues = strtolower($paramValues);
if (!array_key_exists($paramValues, self::$ACTIONS)) {
$this->errorMessage = $lang->translateString('parser.invalid-action');
$this->errorMessage = $this->lang->translateString('parser.invalid-action');

return false;
}
Expand Down Expand Up @@ -245,13 +249,13 @@ static function (?Block $block): bool {
}
);
} catch (InvalidArgumentException $exception) {
$this->errorMessage = $lang->translateString('parser.invalid-block-' . ($index === 'blocks' ? 'include' : 'exclude'));
$this->errorMessage = $this->lang->translateString('parser.invalid-block-' . ($index === 'blocks' ? 'include' : 'exclude'));

return false;
}
break;
default:
$this->errorMessage = $lang->translateString('parser.invalid-parameter', [implode(',', array_keys(self::$ACTIONS))]);
$this->errorMessage = $this->lang->translateString('parser.invalid-parameter', [implode(',', array_keys(self::$ACTIONS))]);
return false;
}
}
Expand All @@ -264,7 +268,7 @@ static function (?Block $block): bool {
}

if (count(array_intersect_key(array_flip($this->requiredParams), $filter)) !== count($this->requiredParams)) {
$this->errorMessage = $lang->translateString('parser.missing-parameters', [implode(',', $this->requiredParams)]);
$this->errorMessage = $this->lang->translateString('parser.missing-parameters', [implode(',', $this->requiredParams)]);

return false;
}
Expand Down

0 comments on commit 3d1703f

Please sign in to comment.