mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-11 04:17:48 +00:00
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:
parent
dac76f0e0f
commit
eba888449d
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\entity\utils;
|
namespace pocketmine\entity\utils;
|
||||||
|
|
||||||
use pocketmine\math\Math;
|
use pocketmine\math\Math;
|
||||||
|
use pocketmine\utils\AssumptionFailedError;
|
||||||
use function max;
|
use function max;
|
||||||
|
|
||||||
abstract class ExperienceUtils{
|
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.
|
* This returns a floating-point number, the decimal part being the progress through the resulting level.
|
||||||
*/
|
*/
|
||||||
public static function getLevelFromXp(int $xp) : float{
|
public static function getLevelFromXp(int $xp) : float{
|
||||||
|
if($xp < 0){
|
||||||
|
throw new \InvalidArgumentException("XP must be at least 0");
|
||||||
|
}
|
||||||
if($xp <= self::getXpToReachLevel(16)){
|
if($xp <= self::getXpToReachLevel(16)){
|
||||||
$a = 1;
|
$a = 1;
|
||||||
$b = 6;
|
$b = 6;
|
||||||
@ -74,6 +78,9 @@ abstract class ExperienceUtils{
|
|||||||
}
|
}
|
||||||
|
|
||||||
$x = Math::solveQuadratic($a, $b, $c - $xp);
|
$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
|
return max($x); //we're only interested in the positive solution
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user