mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 16:51:42 +00:00
VanillaCommand: added getBoundedInt()
This commit is contained in:
parent
c20b16a0fe
commit
773f760fff
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user