From 6fb8ac211ef01bf4bdeb9852cd5ddfc8db7f9693 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 27 Jun 2021 20:56:51 +0100 Subject: [PATCH] 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. --- src/command/defaults/DefaultGamemodeCommand.php | 5 ++--- src/command/defaults/GamemodeCommand.php | 5 ++--- src/player/GameMode.php | 8 ++------ src/plugin/PluginDescription.php | 6 +++--- src/plugin/PluginEnableOrder.php | 8 ++------ 5 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/command/defaults/DefaultGamemodeCommand.php b/src/command/defaults/DefaultGamemodeCommand.php index 8a72fab70..7fc965171 100644 --- a/src/command/defaults/DefaultGamemodeCommand.php +++ b/src/command/defaults/DefaultGamemodeCommand.php @@ -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; } diff --git a/src/command/defaults/GamemodeCommand.php b/src/command/defaults/GamemodeCommand.php index 0633b914e..8e71fe23b 100644 --- a/src/command/defaults/GamemodeCommand.php +++ b/src/command/defaults/GamemodeCommand.php @@ -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; } diff --git a/src/player/GameMode.php b/src/player/GameMode.php index 3508d3d67..a41963008 100644 --- a/src/player/GameMode.php +++ b/src/player/GameMode.php @@ -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 */ diff --git a/src/plugin/PluginDescription.php b/src/plugin/PluginDescription.php index 02a81da48..9ae463093 100644 --- a/src/plugin/PluginDescription.php +++ b/src/plugin/PluginDescription.php @@ -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(); } diff --git a/src/plugin/PluginEnableOrder.php b/src/plugin/PluginEnableOrder.php index 9aee306dd..5b4483032 100644 --- a/src/plugin/PluginEnableOrder.php +++ b/src/plugin/PluginEnableOrder.php @@ -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; } /**