Get rid of EnumTrait::fromString()

it's better to just implement this for stuff where there's explicitly designated aliases, otherwise we could end up with unexpected BC breaks (e.g. hardcoding POSTWORLD in plugin.yml would suddenly break if the core enum was changed, even though it remained valid).
This commit is contained in:
Dylan K. Taylor
2021-06-27 20:52:56 +01:00
parent 902ea515f7
commit 3dd33cd35e
3 changed files with 61 additions and 23 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\player;
use pocketmine\utils\EnumTrait;
use function mb_strtolower;
/**
* This doc-block is generated automatically, do not modify it manually.
@ -39,7 +40,6 @@ final class GameMode{
use EnumTrait {
__construct as Enum___construct;
register as Enum_register;
fromString as Enum_fromString;
}
/** @var self[] */
@ -47,23 +47,27 @@ final class GameMode{
protected static function setup() : void{
self::registerAll(
new self("survival", "Survival", "gameMode.survival", ["s", "0"]),
new self("creative", "Creative", "gameMode.creative", ["c", "1"]),
new self("adventure", "Adventure", "gameMode.adventure", ["a", "2"]),
new self("spectator", "Spectator", "gameMode.spectator", ["v", "view", "3"])
new self("survival", "Survival", "gameMode.survival", ["survival", "s", "0"]),
new self("creative", "Creative", "gameMode.creative", ["creative", "c", "1"]),
new self("adventure", "Adventure", "gameMode.adventure", ["adventure", "a", "2"]),
new self("spectator", "Spectator", "gameMode.spectator", ["spectator", "v", "view", "3"])
);
}
protected static function register(self $member) : void{
self::Enum_register($member);
foreach($member->getAliases() as $alias){
self::$aliasMap[$alias] = $member;
self::$aliasMap[mb_strtolower($alias)] = $member;
}
}
public static function fromString(string $str) : self{
self::checkInit();
return self::$aliasMap[$str] ?? self::Enum_fromString($str);
$result = self::$aliasMap[mb_strtolower($str)] ?? null;
if($result === null){
throw new \InvalidArgumentException("Invalid gamemode alias $str");
}
return $result;
}
/** @var int */