mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
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:
@ -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 */
|
||||
|
Reference in New Issue
Block a user