PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
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 function mb_strtolower;
29use function spl_object_id;
30
34enum GameMode{
35 case SURVIVAL;
36 case CREATIVE;
37 case ADVENTURE;
38 case SPECTATOR;
39
40 public static function fromString(string $str) : ?self{
45 static $aliasMap = null;
46
47 if($aliasMap === null){
48 $aliasMap = [];
49 foreach(self::cases() as $case){
50 foreach($case->getAliases() as $alias){
51 $aliasMap[$alias] = $case;
52 }
53 }
54 }
55
56 return $aliasMap[mb_strtolower($str)] ?? null;
57 }
58
62 private function getMetadata() : array{
64 static $cache = [];
65
66 return $cache[spl_object_id($this)] ??= match($this){
67 self::SURVIVAL => ["Survival", KnownTranslationFactory::gameMode_survival(), ["survival", "s", "0"]],
68 self::CREATIVE => ["Creative", KnownTranslationFactory::gameMode_creative(), ["creative", "c", "1"]],
69 self::ADVENTURE => ["Adventure", KnownTranslationFactory::gameMode_adventure(), ["adventure", "a", "2"]],
70 self::SPECTATOR => ["Spectator", KnownTranslationFactory::gameMode_spectator(), ["spectator", "v", "view", "3"]]
71 };
72 }
73
74 public function getEnglishName() : string{
75 return $this->getMetadata()[0];
76 }
77
78 public function getTranslatableName() : Translatable{
79 return $this->getMetadata()[1];
80 }
81
85 public function getAliases() : array{
86 return $this->getMetadata()[2];
87 }
88
89 //TODO: ability sets per gamemode
90}