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

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