mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-04 17:06:16 +00:00
Position: add getLevelNonNull()
this allows assuming that a position has a valid world in places where it's never expected to not be valid. Since this is the vast majority of usages, it eliminates a lot of possible null-pointer warnings given by static analysers. TODO: Consider whether we can make Position->getLevel/World use this behaviour out of the box in the next major version.
This commit is contained in:
@ -25,6 +25,7 @@ namespace pocketmine\level;
|
||||
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\utils\MainLogger;
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use function assert;
|
||||
|
||||
class Position extends Vector3{
|
||||
@ -71,6 +72,19 @@ class Position extends Vector3{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the position's world if valid. Throws an error if the world is unexpectedly null.
|
||||
*
|
||||
* @throws AssumptionFailedError
|
||||
*/
|
||||
public function getLevelNonNull() : Level{
|
||||
$world = $this->getLevel();
|
||||
if($world === null){
|
||||
throw new AssumptionFailedError("Position world is null");
|
||||
}
|
||||
return $world;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the target Level of the position.
|
||||
*
|
||||
@ -112,7 +126,7 @@ class Position extends Vector3{
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return "Position(level=" . ($this->isValid() ? $this->getLevel()->getName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")";
|
||||
return "Position(level=" . ($this->isValid() ? $this->getLevelNonNull()->getName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")";
|
||||
}
|
||||
|
||||
public function equals(Vector3 $v) : bool{
|
||||
|
Reference in New Issue
Block a user