From a756519e6bd0496cbcd36566e05591e4a68dee30 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 12 Jan 2019 16:10:14 +0000 Subject: [PATCH] Move gamemode constants & functions to their own class future enhancements: - make gamemode an object containing information about abilities that players have in this gamemode (gamemodes are just predefined ability sets) - get the magic numbers out of the API --- src/pocketmine/Gamemode.php | 109 ++++++++++++++++++ src/pocketmine/Player.php | 22 ++-- src/pocketmine/Server.php | 72 +----------- .../defaults/DefaultGamemodeCommand.php | 6 +- .../command/defaults/GamemodeCommand.php | 10 +- .../network/mcpe/RakLibInterface.php | 3 +- 6 files changed, 128 insertions(+), 94 deletions(-) create mode 100644 src/pocketmine/Gamemode.php diff --git a/src/pocketmine/Gamemode.php b/src/pocketmine/Gamemode.php new file mode 100644 index 000000000..1eb494ed2 --- /dev/null +++ b/src/pocketmine/Gamemode.php @@ -0,0 +1,109 @@ +sendGamemode(); }else{ - Command::broadcastCommandMessage($this, new TranslationContainer("commands.gamemode.success.self", [Server::getGamemodeString($gm)])); + Command::broadcastCommandMessage($this, new TranslationContainer("commands.gamemode.success.self", [Gamemode::toTranslation($gm)])); } $this->sendSettings(); @@ -1406,7 +1400,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ */ public function isSurvival(bool $literal = false) : bool{ if($literal){ - return $this->gamemode === Player::SURVIVAL; + return $this->gamemode === Gamemode::SURVIVAL; }else{ return ($this->gamemode & 0x01) === 0; } @@ -1422,7 +1416,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ */ public function isCreative(bool $literal = false) : bool{ if($literal){ - return $this->gamemode === Player::CREATIVE; + return $this->gamemode === Gamemode::CREATIVE; }else{ return ($this->gamemode & 0x01) === 1; } @@ -1438,7 +1432,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ */ public function isAdventure(bool $literal = false) : bool{ if($literal){ - return $this->gamemode === Player::ADVENTURE; + return $this->gamemode === Gamemode::ADVENTURE; }else{ return ($this->gamemode & 0x02) > 0; } @@ -1448,7 +1442,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ * @return bool */ public function isSpectator() : bool{ - return $this->gamemode === Player::SPECTATOR; + return $this->gamemode === Gamemode::SPECTATOR; } public function isFireProof() : bool{ @@ -1893,7 +1887,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->firstPlayed = $nbt->getLong("firstPlayed", $now = (int) (microtime(true) * 1000)); $this->lastPlayed = $nbt->getLong("lastPlayed", $now); - $this->gamemode = $nbt->getInt("playerGameType", self::SURVIVAL) & 0x03; + $this->gamemode = $nbt->getInt("playerGameType", Gamemode::SURVIVAL) & 0x03; if($this->server->getForceGamemode()){ $this->gamemode = $this->server->getGamemode(); } diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index a0498e7a6..b750e112f 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -526,76 +526,6 @@ class Server{ return $this->getConfigBool("force-gamemode", false); } - /** - * Returns the gamemode text name - * - * @param int $mode - * - * @return string - */ - public static function getGamemodeString(int $mode) : string{ - switch($mode){ - case Player::SURVIVAL: - return "%gameMode.survival"; - case Player::CREATIVE: - return "%gameMode.creative"; - case Player::ADVENTURE: - return "%gameMode.adventure"; - case Player::SPECTATOR: - return "%gameMode.spectator"; - } - - return "UNKNOWN"; - } - - public static function getGamemodeName(int $mode) : string{ - switch($mode){ - case Player::SURVIVAL: - return "Survival"; - case Player::CREATIVE: - return "Creative"; - case Player::ADVENTURE: - return "Adventure"; - case Player::SPECTATOR: - return "Spectator"; - default: - throw new \InvalidArgumentException("Invalid gamemode $mode"); - } - } - - /** - * Parses a string and returns a gamemode integer, -1 if not found - * - * @param string $str - * - * @return int - */ - public static function getGamemodeFromString(string $str) : int{ - switch(strtolower(trim($str))){ - case (string) Player::SURVIVAL: - case "survival": - case "s": - return Player::SURVIVAL; - - case (string) Player::CREATIVE: - case "creative": - case "c": - return Player::CREATIVE; - - case (string) Player::ADVENTURE: - case "adventure": - case "a": - return Player::ADVENTURE; - - case (string) Player::SPECTATOR: - case "spectator": - case "view": - case "v": - return Player::SPECTATOR; - } - return -1; - } - /** * Returns Server global difficulty. Note that this may be overridden in individual Levels. * @return int @@ -2237,7 +2167,7 @@ class Server{ $this->dispatchSignals = true; } - $this->logger->info($this->getLanguage()->translateString("pocketmine.server.defaultGameMode", [self::getGamemodeString($this->getGamemode())])); + $this->logger->info($this->getLanguage()->translateString("pocketmine.server.defaultGameMode", [Gamemode::toTranslation($this->getGamemode())])); $this->logger->info($this->getLanguage()->translateString("pocketmine.server.startFinished", [round(microtime(true) - \pocketmine\START_TIME, 3)])); diff --git a/src/pocketmine/command/defaults/DefaultGamemodeCommand.php b/src/pocketmine/command/defaults/DefaultGamemodeCommand.php index 04c94d990..d5bdc0f09 100644 --- a/src/pocketmine/command/defaults/DefaultGamemodeCommand.php +++ b/src/pocketmine/command/defaults/DefaultGamemodeCommand.php @@ -25,8 +25,8 @@ namespace pocketmine\command\defaults; use pocketmine\command\CommandSender; use pocketmine\command\utils\InvalidCommandSyntaxException; +use pocketmine\Gamemode; use pocketmine\lang\TranslationContainer; -use pocketmine\Server; use function count; class DefaultGamemodeCommand extends VanillaCommand{ @@ -49,11 +49,11 @@ class DefaultGamemodeCommand extends VanillaCommand{ throw new InvalidCommandSyntaxException(); } - $gameMode = Server::getGamemodeFromString($args[0]); + $gameMode = Gamemode::fromString($args[0]); if($gameMode !== -1){ $sender->getServer()->setConfigInt("gamemode", $gameMode); - $sender->sendMessage(new TranslationContainer("commands.defaultgamemode.success", [Server::getGamemodeString($gameMode)])); + $sender->sendMessage(new TranslationContainer("commands.defaultgamemode.success", [Gamemode::toTranslation($gameMode)])); }else{ $sender->sendMessage("Unknown game mode"); } diff --git a/src/pocketmine/command/defaults/GamemodeCommand.php b/src/pocketmine/command/defaults/GamemodeCommand.php index 063993aee..9199e74e6 100644 --- a/src/pocketmine/command/defaults/GamemodeCommand.php +++ b/src/pocketmine/command/defaults/GamemodeCommand.php @@ -26,9 +26,9 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; use pocketmine\command\utils\InvalidCommandSyntaxException; +use pocketmine\Gamemode; use pocketmine\lang\TranslationContainer; use pocketmine\Player; -use pocketmine\Server; use pocketmine\utils\TextFormat; use function count; @@ -52,7 +52,7 @@ class GamemodeCommand extends VanillaCommand{ throw new InvalidCommandSyntaxException(); } - $gameMode = Server::getGamemodeFromString($args[0]); + $gameMode = Gamemode::fromString($args[0]); if($gameMode === -1){ $sender->sendMessage("Unknown game mode"); @@ -77,10 +77,10 @@ class GamemodeCommand extends VanillaCommand{ $sender->sendMessage("Game mode change for " . $target->getName() . " failed!"); }else{ if($target === $sender){ - Command::broadcastCommandMessage($sender, new TranslationContainer("commands.gamemode.success.self", [Server::getGamemodeString($gameMode)])); + Command::broadcastCommandMessage($sender, new TranslationContainer("commands.gamemode.success.self", [Gamemode::toTranslation($gameMode)])); }else{ - $target->sendMessage(new TranslationContainer("gameMode.changed", [Server::getGamemodeString($gameMode)])); - Command::broadcastCommandMessage($sender, new TranslationContainer("commands.gamemode.success.other", [Server::getGamemodeString($gameMode), $target->getName()])); + $target->sendMessage(new TranslationContainer("gameMode.changed", [Gamemode::toTranslation($gameMode)])); + Command::broadcastCommandMessage($sender, new TranslationContainer("commands.gamemode.success.other", [Gamemode::toTranslation($gameMode), $target->getName()])); } } diff --git a/src/pocketmine/network/mcpe/RakLibInterface.php b/src/pocketmine/network/mcpe/RakLibInterface.php index 9bcea2896..3a3529602 100644 --- a/src/pocketmine/network/mcpe/RakLibInterface.php +++ b/src/pocketmine/network/mcpe/RakLibInterface.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\network\mcpe; +use pocketmine\Gamemode; use pocketmine\network\AdvancedNetworkInterface; use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\Network; @@ -196,7 +197,7 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{ $info->getMaxPlayerCount(), $this->rakLib->getServerId(), $this->server->getName(), - Server::getGamemodeName($this->server->getGamemode()) + Gamemode::toString($this->server->getGamemode()) ]) . ";" ); }