VanillaCommand: added getBoundedInt()

This commit is contained in:
Dylan K. Taylor 2018-03-30 11:48:42 +01:00
parent c20b16a0fe
commit 773f760fff
2 changed files with 24 additions and 7 deletions

View File

@ -86,13 +86,9 @@ class EffectCommand extends VanillaCommand{
}
if(count($args) >= 4){
$amplification = (int) $args[3];
if($amplification > 255){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.num.tooBig", [(string) $args[3], "255"]));
return true;
}elseif($amplification < 0){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.num.tooSmall", [(string) $args[3], "0"]));
return true;
$amplification = $this->getBoundedInt($sender, $args[3], 0, 255);
if($amplification === null){
return false;
}
}

View File

@ -25,6 +25,9 @@ namespace pocketmine\command\defaults;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\TranslationContainer;
use pocketmine\utils\TextFormat;
abstract class VanillaCommand extends Command{
public const MAX_COORD = 30000000;
@ -88,4 +91,22 @@ abstract class VanillaCommand extends Command{
return $i;
}
protected function getBoundedInt(CommandSender $sender, string $input, int $min, int $max) : ?int{
if(!is_numeric($input)){
throw new InvalidCommandSyntaxException();
}
$v = (int) $input;
if($v > $max){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.num.tooBig", [$input, (string) $max]));
return null;
}
if($v < $min){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.num.tooSmall", [$input, (string) $min]));
return null;
}
return $v;
}
}