diff --git a/src/player/GameMode.php b/src/player/GameMode.php index 3d5c0c7dc..3508d3d67 100644 --- a/src/player/GameMode.php +++ b/src/player/GameMode.php @@ -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 */ diff --git a/src/plugin/PluginEnableOrder.php b/src/plugin/PluginEnableOrder.php index ae800decb..9aee306dd 100644 --- a/src/plugin/PluginEnableOrder.php +++ b/src/plugin/PluginEnableOrder.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\plugin; use pocketmine\utils\EnumTrait; +use function mb_strtolower; /** * This doc-block is generated automatically, do not modify it manually. @@ -34,12 +35,58 @@ use pocketmine\utils\EnumTrait; * @method static PluginEnableOrder STARTUP() */ final class PluginEnableOrder{ - use EnumTrait; + use EnumTrait { + __construct as Enum___construct; + register as Enum_register; + } protected static function setup() : void{ self::registerAll( - new self("startup"), - new self("postworld") + new self("startup", ["startup"]), + new self("postworld", ["postworld"]) ); } + + /** + * @var self[] + * @phpstan-var array + */ + private static array $aliasMap = []; + + protected static function register(self $member) : void{ + self::Enum_register($member); + foreach($member->getAliases() as $alias){ + self::$aliasMap[mb_strtolower($alias)] = $member; + } + } + + 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; + } + + /** + * @var string[] + * @phpstan-var list + */ + private array $aliases; + + /** + * @param string[] $aliases + * @phpstan-param list $aliases + */ + private function __construct(string $enumName, array $aliases){ + $this->Enum___construct($enumName); + $this->aliases = $aliases; + } + + /** + * @return string[] + * @phpstan-return list + */ + public function getAliases() : array{ return $this->aliases; } } diff --git a/src/utils/EnumTrait.php b/src/utils/EnumTrait.php index 163c0b0b2..e464442fe 100644 --- a/src/utils/EnumTrait.php +++ b/src/utils/EnumTrait.php @@ -56,19 +56,6 @@ trait EnumTrait{ return $result; } - /** - * Returns the enum member matching the given name. - * This is overridden to change the return typehint. - * - * @throws \InvalidArgumentException if no member matches. - */ - public static function fromString(string $name) : self{ - //phpstan doesn't support generic traits yet :( - /** @var self $result */ - $result = self::_registryFromString($name); - return $result; - } - /** @var int|null */ private static $nextId = null;