Separate ID handling from GameMode

the aliases of 0,1,2,3 remain for user-interface level compatibility.
This commit is contained in:
Dylan K. Taylor
2021-06-27 20:32:35 +01:00
parent 5cdf0b169f
commit 902ea515f7
7 changed files with 83 additions and 31 deletions

View File

@ -44,21 +44,18 @@ final class GameMode{
/** @var self[] */
protected static $aliasMap = [];
/** @var self[] */
protected static $magicNumberMap = [];
protected static function setup() : void{
self::registerAll(
new self("survival", 0, "Survival", "gameMode.survival", ["s", "0"]),
new self("creative", 1, "Creative", "gameMode.creative", ["c", "1"]),
new self("adventure", 2, "Adventure", "gameMode.adventure", ["a", "2"]),
new self("spectator", 3, "Spectator", "gameMode.spectator", ["v", "view", "3"])
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"])
);
}
protected static function register(self $member) : void{
self::Enum_register($member);
self::$magicNumberMap[$member->getMagicNumber()] = $member;
foreach($member->getAliases() as $alias){
self::$aliasMap[$alias] = $member;
}
@ -69,18 +66,6 @@ final class GameMode{
return self::$aliasMap[$str] ?? self::Enum_fromString($str);
}
/**
* @return GameMode
* @throws \InvalidArgumentException
*/
public static function fromMagicNumber(int $n) : self{
self::checkInit();
if(!isset(self::$magicNumberMap[$n])){
throw new \InvalidArgumentException("No " . self::class . " enum member matches magic number $n");
}
return self::$magicNumberMap[$n];
}
/** @var int */
private $magicNumber;
/** @var string */
@ -93,18 +78,13 @@ final class GameMode{
/**
* @param string[] $aliases
*/
private function __construct(string $enumName, int $magicNumber, string $englishName, string $translationKey, array $aliases = []){
private function __construct(string $enumName, string $englishName, string $translationKey, array $aliases = []){
$this->Enum___construct($enumName);
$this->magicNumber = $magicNumber;
$this->englishName = $englishName;
$this->translationKey = $translationKey;
$this->aliases = $aliases;
}
public function getMagicNumber() : int{
return $this->magicNumber;
}
public function getEnglishName() : string{
return $this->englishName;
}

View File

@ -29,6 +29,7 @@ use pocketmine\block\UnknownBlock;
use pocketmine\block\VanillaBlocks;
use pocketmine\command\CommandSender;
use pocketmine\crafting\CraftingGrid;
use pocketmine\data\java\GameModeIdMap;
use pocketmine\entity\animation\Animation;
use pocketmine\entity\animation\ArmSwingAnimation;
use pocketmine\entity\animation\CriticalHitAnimation;
@ -332,7 +333,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->lastPlayed = $nbt->getLong("lastPlayed", $now);
if(!$this->server->getForceGamemode() and ($gameModeTag = $nbt->getTag("playerGameType")) instanceof IntTag){
$this->internalSetGameMode(GameMode::fromMagicNumber($gameModeTag->getValue() & 0x03)); //TODO: bad hack here to avoid crashes on corrupted data
$this->internalSetGameMode(GameModeIdMap::getInstance()->fromId($gameModeTag->getValue()) ?? GameMode::SURVIVAL()); //TODO: bad hack here to avoid crashes on corrupted data
}else{
$this->internalSetGameMode($this->server->getGamemode());
}
@ -2069,7 +2070,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$nbt->setInt("SpawnZ", $spawn->getFloorZ());
}
$nbt->setInt("playerGameType", $this->gamemode->getMagicNumber());
$nbt->setInt("playerGameType", GameModeIdMap::getInstance()->toId($this->gamemode));
$nbt->setLong("firstPlayed", $this->firstPlayed);
$nbt->setLong("lastPlayed", (int) floor(microtime(true) * 1000));