mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-18 11:45:30 +00:00
This is better for performance because these then don't need to be reevaluated every time they are called. When encountering an unqualified function or constant reference, PHP will first try to locate a symbol in the current namespace by that name, and then fall back to the global namespace. This short-circuits the check, which has substantial performance effects in some cases - in particular, ord(), chr() and strlen() show ~1500x faster calls when they are fully qualified. However, this doesn't mean that PM is getting a massive amount faster. In real world terms, this translates to about 10-15% performance improvement. But before anyone gets excited, you should know that the CodeOptimizer in the PreProcessor repo has been applying fully-qualified symbol optimizations to Jenkins builds for years, which is one of the reasons why Jenkins builds have better performance than home-built or source installations. We're choosing to do this for the sake of future SafePHP integration and also to be able to get rid of the buggy CodeOptimizer, so that phar and source are more consistent.
115 lines
2.9 KiB
PHP
115 lines
2.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
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;
|
|
use function is_numeric;
|
|
use function substr;
|
|
|
|
abstract class VanillaCommand extends Command{
|
|
public const MAX_COORD = 30000000;
|
|
public const MIN_COORD = -30000000;
|
|
|
|
/**
|
|
* @param CommandSender $sender
|
|
* @param mixed $value
|
|
* @param int $min
|
|
* @param int $max
|
|
*
|
|
* @return int
|
|
*/
|
|
protected function getInteger(CommandSender $sender, $value, int $min = self::MIN_COORD, int $max = self::MAX_COORD) : int{
|
|
$i = (int) $value;
|
|
|
|
if($i < $min){
|
|
$i = $min;
|
|
}elseif($i > $max){
|
|
$i = $max;
|
|
}
|
|
|
|
return $i;
|
|
}
|
|
|
|
/**
|
|
* @param float $original
|
|
* @param CommandSender $sender
|
|
* @param string $input
|
|
* @param float $min
|
|
* @param float $max
|
|
*
|
|
* @return float
|
|
*/
|
|
protected function getRelativeDouble(float $original, CommandSender $sender, string $input, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
|
|
if($input{0} === "~"){
|
|
$value = $this->getDouble($sender, substr($input, 1));
|
|
|
|
return $original + $value;
|
|
}
|
|
|
|
return $this->getDouble($sender, $input, $min, $max);
|
|
}
|
|
|
|
/**
|
|
* @param CommandSender $sender
|
|
* @param mixed $value
|
|
* @param float $min
|
|
* @param float $max
|
|
*
|
|
* @return float
|
|
*/
|
|
protected function getDouble(CommandSender $sender, $value, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
|
|
$i = (double) $value;
|
|
|
|
if($i < $min){
|
|
$i = $min;
|
|
}elseif($i > $max){
|
|
$i = $max;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|