Make GameMode::fromString() and PluginEnableOrder::fromString() return null, instead of throwing exceptions

since these are always used for handling userdata, it doesn't make sense for them to throw exceptions.
This commit is contained in:
Dylan K. Taylor 2021-06-27 20:56:51 +01:00
parent 3dd33cd35e
commit 6fb8ac211e
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 11 additions and 21 deletions

View File

@ -51,9 +51,8 @@ class DefaultGamemodeCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException();
}
try{
$gameMode = GameMode::fromString($args[0]);
}catch(\InvalidArgumentException $e){
$gameMode = GameMode::fromString($args[0]);
if($gameMode === null){
$sender->sendMessage("Unknown game mode");
return true;
}

View File

@ -53,9 +53,8 @@ class GamemodeCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException();
}
try{
$gameMode = GameMode::fromString($args[0]);
}catch(\InvalidArgumentException $e){
$gameMode = GameMode::fromString($args[0]);
if($gameMode === null){
$sender->sendMessage("Unknown game mode");
return true;
}

View File

@ -61,13 +61,9 @@ final class GameMode{
}
}
public static function fromString(string $str) : self{
public static function fromString(string $str) : ?self{
self::checkInit();
$result = self::$aliasMap[mb_strtolower($str)] ?? null;
if($result === null){
throw new \InvalidArgumentException("Invalid gamemode alias $str");
}
return $result;
return self::$aliasMap[mb_strtolower($str)] ?? null;
}
/** @var int */

View File

@ -151,11 +151,11 @@ class PluginDescription{
$this->prefix = (string) ($plugin["prefix"] ?? $this->prefix);
if(isset($plugin["load"])){
try{
$this->order = PluginEnableOrder::fromString($plugin["load"]);
}catch(\InvalidArgumentException $e){
$order = PluginEnableOrder::fromString($plugin["load"]);
if($order === null){
throw new PluginException("Invalid Plugin \"load\"");
}
$this->order = $order;
}else{
$this->order = PluginEnableOrder::POSTWORLD();
}

View File

@ -60,13 +60,9 @@ final class PluginEnableOrder{
}
}
public static function fromString(string $name) : self{
public static function fromString(string $name) : ?self{
self::checkInit();
$result = self::$aliasMap[mb_strtolower($name)] ?? null;
if($result === null){
throw new \InvalidArgumentException("No such alias $name");
}
return $result;
return self::$aliasMap[mb_strtolower($name)] ?? null;
}
/**