GameMode::fromString() now throws InvalidArgumentException

This commit is contained in:
Dylan K. Taylor 2019-01-26 17:09:54 +00:00
parent e86ff7d988
commit 0b1bdec3ac
3 changed files with 13 additions and 11 deletions

View File

@ -40,6 +40,8 @@ final class GameMode{
* @param string $str
*
* @return int
*
* @throws \InvalidArgumentException
*/
public static function fromString(string $str) : int{
switch(strtolower(trim($str))){
@ -65,7 +67,7 @@ final class GameMode{
return self::SPECTATOR;
}
return -1;
throw new \InvalidArgumentException("Unknown gamemode string \"$str\"");
}
/**

View File

@ -49,15 +49,16 @@ class DefaultGamemodeCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException();
}
$gameMode = GameMode::fromString($args[0]);
if($gameMode !== -1){
$sender->getServer()->setConfigInt("gamemode", $gameMode);
$sender->sendMessage(new TranslationContainer("commands.defaultgamemode.success", [GameMode::toTranslation($gameMode)]));
}else{
try{
$gameMode = GameMode::fromString($args[0]);
}catch(\InvalidArgumentException $e){
$sender->sendMessage("Unknown game mode");
return true;
}
$sender->getServer()->setConfigInt("gamemode", $gameMode);
$sender->sendMessage(new TranslationContainer("commands.defaultgamemode.success", [GameMode::toTranslation($gameMode)]));
return true;
}
}

View File

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