mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-12 12:55:21 +00:00
Make Tile::registerTile() and Entity::registerEntity() throw exceptions instead of returning false
This commit is contained in:
parent
b9769c407b
commit
d305a1342f
@ -301,17 +301,27 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
|||||||
* NOTE: The first save name in the $saveNames array will be used when saving the entity to disk. The reflection
|
* NOTE: The first save name in the $saveNames array will be used when saving the entity to disk. The reflection
|
||||||
* name of the class will be appended to the end and only used if no other save names are specified.
|
* name of the class will be appended to the end and only used if no other save names are specified.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function registerEntity(string $className, bool $force = false, array $saveNames = []) : bool{
|
public static function registerEntity(string $className, bool $force = false, array $saveNames = []) : void{
|
||||||
/** @var Entity $className */
|
/** @var Entity $className */
|
||||||
|
|
||||||
|
try{
|
||||||
$class = new \ReflectionClass($className);
|
$class = new \ReflectionClass($className);
|
||||||
if(is_a($className, Entity::class, true) and !$class->isAbstract()){
|
}catch(\ReflectionException $e){
|
||||||
|
throw new \InvalidArgumentException("Class $className does not exist");
|
||||||
|
}
|
||||||
|
if(!$class->isSubclassOf(Entity::class)){
|
||||||
|
throw new \InvalidArgumentException("Class $className does not extend " . Entity::class);
|
||||||
|
}
|
||||||
|
if(!$class->isInstantiable()){
|
||||||
|
throw new \InvalidArgumentException("Class $className cannot be constructed");
|
||||||
|
}
|
||||||
|
|
||||||
if($className::NETWORK_ID !== -1){
|
if($className::NETWORK_ID !== -1){
|
||||||
self::$knownEntities[$className::NETWORK_ID] = $className;
|
self::$knownEntities[$className::NETWORK_ID] = $className;
|
||||||
}elseif(!$force){
|
}elseif(!$force){
|
||||||
return false;
|
throw new \InvalidArgumentException("Class $className does not declare a valid NETWORK_ID and not force-registering");
|
||||||
}
|
}
|
||||||
|
|
||||||
$shortName = $class->getShortName();
|
$shortName = $class->getShortName();
|
||||||
@ -324,11 +334,6 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
|||||||
}
|
}
|
||||||
|
|
||||||
self::$saveNames[$className] = $saveNames;
|
self::$saveNames[$className] = $saveNames;
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,14 +111,21 @@ abstract class Tile extends Position{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @param array $saveNames
|
* @param string[] $saveNames
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \ReflectionException
|
|
||||||
*/
|
*/
|
||||||
public static function registerTile(string $className, array $saveNames = []) : bool{
|
public static function registerTile(string $className, array $saveNames = []) : void{
|
||||||
|
try{
|
||||||
$class = new \ReflectionClass($className);
|
$class = new \ReflectionClass($className);
|
||||||
if(is_a($className, Tile::class, true) and !$class->isAbstract()){
|
}catch(\ReflectionException $e){
|
||||||
|
throw new \InvalidArgumentException("Class $className does not exist");
|
||||||
|
}
|
||||||
|
if(!$class->isSubclassOf(Tile::class)){
|
||||||
|
throw new \InvalidArgumentException("Class $className does not extend " . Tile::class);
|
||||||
|
}
|
||||||
|
if(!$class->isInstantiable()){
|
||||||
|
throw new \InvalidArgumentException("Class $className cannot be constructed");
|
||||||
|
}
|
||||||
|
|
||||||
$shortName = $class->getShortName();
|
$shortName = $class->getShortName();
|
||||||
if(!in_array($shortName, $saveNames, true)){
|
if(!in_array($shortName, $saveNames, true)){
|
||||||
$saveNames[] = $shortName;
|
$saveNames[] = $shortName;
|
||||||
@ -129,12 +136,6 @@ abstract class Tile extends Position{
|
|||||||
}
|
}
|
||||||
|
|
||||||
self::$saveNames[$className] = $saveNames;
|
self::$saveNames[$className] = $saveNames;
|
||||||
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user