mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
Make Tile::registerTile() and Entity::registerEntity() throw exceptions instead of returning false
This commit is contained in:
parent
b9769c407b
commit
d305a1342f
@ -301,34 +301,39 @@ 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
|
||||
* 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 */
|
||||
|
||||
$class = new \ReflectionClass($className);
|
||||
if(is_a($className, Entity::class, true) and !$class->isAbstract()){
|
||||
if($className::NETWORK_ID !== -1){
|
||||
self::$knownEntities[$className::NETWORK_ID] = $className;
|
||||
}elseif(!$force){
|
||||
return false;
|
||||
}
|
||||
|
||||
$shortName = $class->getShortName();
|
||||
if(!in_array($shortName, $saveNames, true)){
|
||||
$saveNames[] = $shortName;
|
||||
}
|
||||
|
||||
foreach($saveNames as $name){
|
||||
self::$knownEntities[$name] = $className;
|
||||
}
|
||||
|
||||
self::$saveNames[$className] = $saveNames;
|
||||
|
||||
return true;
|
||||
try{
|
||||
$class = new \ReflectionClass($className);
|
||||
}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");
|
||||
}
|
||||
|
||||
return false;
|
||||
if($className::NETWORK_ID !== -1){
|
||||
self::$knownEntities[$className::NETWORK_ID] = $className;
|
||||
}elseif(!$force){
|
||||
throw new \InvalidArgumentException("Class $className does not declare a valid NETWORK_ID and not force-registering");
|
||||
}
|
||||
|
||||
$shortName = $class->getShortName();
|
||||
if(!in_array($shortName, $saveNames, true)){
|
||||
$saveNames[] = $shortName;
|
||||
}
|
||||
|
||||
foreach($saveNames as $name){
|
||||
self::$knownEntities[$name] = $className;
|
||||
}
|
||||
|
||||
self::$saveNames[$className] = $saveNames;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,31 +110,32 @@ abstract class Tile extends Position{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
* @param array $saveNames
|
||||
*
|
||||
* @return bool
|
||||
* @throws \ReflectionException
|
||||
* @param string $className
|
||||
* @param string[] $saveNames
|
||||
*/
|
||||
public static function registerTile(string $className, array $saveNames = []) : bool{
|
||||
$class = new \ReflectionClass($className);
|
||||
if(is_a($className, Tile::class, true) and !$class->isAbstract()){
|
||||
$shortName = $class->getShortName();
|
||||
if(!in_array($shortName, $saveNames, true)){
|
||||
$saveNames[] = $shortName;
|
||||
}
|
||||
|
||||
foreach($saveNames as $name){
|
||||
self::$knownTiles[$name] = $className;
|
||||
}
|
||||
|
||||
self::$saveNames[$className] = $saveNames;
|
||||
|
||||
|
||||
return true;
|
||||
public static function registerTile(string $className, array $saveNames = []) : void{
|
||||
try{
|
||||
$class = new \ReflectionClass($className);
|
||||
}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");
|
||||
}
|
||||
|
||||
return false;
|
||||
$shortName = $class->getShortName();
|
||||
if(!in_array($shortName, $saveNames, true)){
|
||||
$saveNames[] = $shortName;
|
||||
}
|
||||
|
||||
foreach($saveNames as $name){
|
||||
self::$knownTiles[$name] = $className;
|
||||
}
|
||||
|
||||
self::$saveNames[$className] = $saveNames;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user