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(); throw new InvalidCommandSyntaxException();
} }
try{ $gameMode = GameMode::fromString($args[0]);
$gameMode = GameMode::fromString($args[0]); if($gameMode === null){
}catch(\InvalidArgumentException $e){
$sender->sendMessage("Unknown game mode"); $sender->sendMessage("Unknown game mode");
return true; return true;
} }

View File

@ -53,9 +53,8 @@ class GamemodeCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException(); throw new InvalidCommandSyntaxException();
} }
try{ $gameMode = GameMode::fromString($args[0]);
$gameMode = GameMode::fromString($args[0]); if($gameMode === null){
}catch(\InvalidArgumentException $e){
$sender->sendMessage("Unknown game mode"); $sender->sendMessage("Unknown game mode");
return true; 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(); self::checkInit();
$result = self::$aliasMap[mb_strtolower($str)] ?? null; return self::$aliasMap[mb_strtolower($str)] ?? null;
if($result === null){
throw new \InvalidArgumentException("Invalid gamemode alias $str");
}
return $result;
} }
/** @var int */ /** @var int */

View File

@ -151,11 +151,11 @@ class PluginDescription{
$this->prefix = (string) ($plugin["prefix"] ?? $this->prefix); $this->prefix = (string) ($plugin["prefix"] ?? $this->prefix);
if(isset($plugin["load"])){ if(isset($plugin["load"])){
try{ $order = PluginEnableOrder::fromString($plugin["load"]);
$this->order = PluginEnableOrder::fromString($plugin["load"]); if($order === null){
}catch(\InvalidArgumentException $e){
throw new PluginException("Invalid Plugin \"load\""); throw new PluginException("Invalid Plugin \"load\"");
} }
$this->order = $order;
}else{ }else{
$this->order = PluginEnableOrder::POSTWORLD(); $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(); self::checkInit();
$result = self::$aliasMap[mb_strtolower($name)] ?? null; return self::$aliasMap[mb_strtolower($name)] ?? null;
if($result === null){
throw new \InvalidArgumentException("No such alias $name");
}
return $result;
} }
/** /**