PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
src/player/GameMode.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\player;
25
28use pocketmine\utils\LegacyEnumShimTrait;
29use function mb_strtolower;
30use function spl_object_id;
31
43enum GameMode{
44 use LegacyEnumShimTrait;
45
46 case SURVIVAL;
47 case CREATIVE;
48 case ADVENTURE;
49 case SPECTATOR;
50
51 public static function fromString(string $str) : ?self{
56 static $aliasMap = null;
57
58 if($aliasMap === null){
59 $aliasMap = [];
60 foreach(self::cases() as $case){
61 foreach($case->getAliases() as $alias){
62 $aliasMap[$alias] = $case;
63 }
64 }
65 }
66
67 return $aliasMap[mb_strtolower($str)] ?? null;
68 }
69
73 private function getMetadata() : array{
75 static $cache = [];
76
77 return $cache[spl_object_id($this)] ??= match($this){
78 self::SURVIVAL => ["Survival", KnownTranslationFactory::gameMode_survival(), ["survival", "s", "0"]],
79 self::CREATIVE => ["Creative", KnownTranslationFactory::gameMode_creative(), ["creative", "c", "1"]],
80 self::ADVENTURE => ["Adventure", KnownTranslationFactory::gameMode_adventure(), ["adventure", "a", "2"]],
81 self::SPECTATOR => ["Spectator", KnownTranslationFactory::gameMode_spectator(), ["spectator", "v", "view", "3"]]
82 };
83 }
84
85 public function getEnglishName() : string{
86 return $this->getMetadata()[0];
87 }
88
89 public function getTranslatableName() : Translatable{
90 return $this->getMetadata()[1];
91 }
92
96 public function getAliases() : array{
97 return $this->getMetadata()[2];
98 }
99
100 //TODO: ability sets per gamemode
101}