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
This commit is contained in:
Dylan K. Taylor 2019-01-12 16:10:14 +00:00
parent 49bdd92faa
commit a756519e6b
6 changed files with 128 additions and 94 deletions

109
src/pocketmine/Gamemode.php Normal file
View File

@ -0,0 +1,109 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine;
final class Gamemode{
public const SURVIVAL = 0;
public const CREATIVE = 1;
public const ADVENTURE = 2;
public const SPECTATOR = 3;
public const VIEW = Gamemode::SPECTATOR;
private function __construct(){
//NOOP
}
/**
* Parses a string and returns a gamemode integer, -1 if not found
*
* @param string $str
*
* @return int
*/
public static function fromString(string $str) : int{
switch(strtolower(trim($str))){
case (string) self::SURVIVAL:
case "survival":
case "s":
return self::SURVIVAL;
case (string) self::CREATIVE:
case "creative":
case "c":
return self::CREATIVE;
case (string) self::ADVENTURE:
case "adventure":
case "a":
return self::ADVENTURE;
case (string) self::SPECTATOR:
case "spectator":
case "view":
case "v":
return self::SPECTATOR;
}
return -1;
}
/**
* Returns the gamemode text name
*
* @param int $mode
*
* @return string
*/
public static function toTranslation(int $mode) : string{
switch($mode){
case self::SURVIVAL:
return "%gameMode.survival";
case self::CREATIVE:
return "%gameMode.creative";
case self::ADVENTURE:
return "%gameMode.adventure";
case self::SPECTATOR:
return "%gameMode.spectator";
}
return "UNKNOWN";
}
public static function toString(int $mode) : string{
switch($mode){
case self::SURVIVAL:
return "Survival";
case self::CREATIVE:
return "Creative";
case self::ADVENTURE:
return "Adventure";
case self::SPECTATOR:
return "Spectator";
default:
throw new \InvalidArgumentException("Invalid gamemode $mode");
}
}
//TODO: ability sets per gamemode
}

View File

@ -171,12 +171,6 @@ use const PHP_INT_MAX;
*/
class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
public const SURVIVAL = 0;
public const CREATIVE = 1;
public const ADVENTURE = 2;
public const SPECTATOR = 3;
public const VIEW = Player::SPECTATOR;
/**
* Checks a supplied username and checks it is valid.
*
@ -1308,8 +1302,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
*/
public static function getClientFriendlyGamemode(int $gamemode) : int{
$gamemode &= 0x03;
if($gamemode === Player::SPECTATOR){
return Player::CREATIVE;
if($gamemode === Gamemode::SPECTATOR){
return Gamemode::CREATIVE;
}
return $gamemode;
@ -1357,7 +1351,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
if(!$client){ //Gamemode changed by server, do not send for client changes
$this->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();
}

View File

@ -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)]));

View File

@ -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");
}

View File

@ -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()]));
}
}

View File

@ -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())
]) . ";"
);
}