ExperienceUtils: handle an unhandled error condition explicitly

this would previously throw a TypeError on some negative numbers and crash. This should still crash, but this makes it explicit.
This commit is contained in:
Dylan K. Taylor 2020-08-25 21:28:29 +01:00
parent dac76f0e0f
commit eba888449d

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\entity\utils;
use pocketmine\math\Math;
use pocketmine\utils\AssumptionFailedError;
use function max;
abstract class ExperienceUtils{
@ -59,6 +60,9 @@ abstract class ExperienceUtils{
* This returns a floating-point number, the decimal part being the progress through the resulting level.
*/
public static function getLevelFromXp(int $xp) : float{
if($xp < 0){
throw new \InvalidArgumentException("XP must be at least 0");
}
if($xp <= self::getXpToReachLevel(16)){
$a = 1;
$b = 6;
@ -74,6 +78,9 @@ abstract class ExperienceUtils{
}
$x = Math::solveQuadratic($a, $b, $c - $xp);
if(count($x) === 0){
throw new AssumptionFailedError("Expected at least 1 solution");
}
return max($x); //we're only interested in the positive solution
}