Constify server.properties references

This commit is contained in:
Dylan K. Taylor 2023-08-25 12:49:39 +01:00
parent b56f1b679e
commit d1a7c1d453
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
6 changed files with 103 additions and 49 deletions

View File

@ -357,15 +357,15 @@ class Server{
} }
public function getPort() : int{ public function getPort() : int{
return $this->configGroup->getConfigInt("server-port", self::DEFAULT_PORT_IPV4); return $this->configGroup->getConfigInt(ServerProperties::SERVER_PORT_IPV4, self::DEFAULT_PORT_IPV4);
} }
public function getPortV6() : int{ public function getPortV6() : int{
return $this->configGroup->getConfigInt("server-portv6", self::DEFAULT_PORT_IPV6); return $this->configGroup->getConfigInt(ServerProperties::SERVER_PORT_IPV6, self::DEFAULT_PORT_IPV6);
} }
public function getViewDistance() : int{ public function getViewDistance() : int{
return max(2, $this->configGroup->getConfigInt("view-distance", self::DEFAULT_MAX_VIEW_DISTANCE)); return max(2, $this->configGroup->getConfigInt(ServerProperties::VIEW_DISTANCE, self::DEFAULT_MAX_VIEW_DISTANCE));
} }
/** /**
@ -376,12 +376,12 @@ class Server{
} }
public function getIp() : string{ public function getIp() : string{
$str = $this->configGroup->getConfigString("server-ip"); $str = $this->configGroup->getConfigString(ServerProperties::SERVER_IPV4);
return $str !== "" ? $str : "0.0.0.0"; return $str !== "" ? $str : "0.0.0.0";
} }
public function getIpV6() : string{ public function getIpV6() : string{
$str = $this->configGroup->getConfigString("server-ipv6"); $str = $this->configGroup->getConfigString(ServerProperties::SERVER_IPV6);
return $str !== "" ? $str : "::"; return $str !== "" ? $str : "::";
} }
@ -390,30 +390,30 @@ class Server{
} }
public function getGamemode() : GameMode{ public function getGamemode() : GameMode{
return GameMode::fromString($this->configGroup->getConfigString("gamemode", GameMode::SURVIVAL()->name())) ?? GameMode::SURVIVAL(); return GameMode::fromString($this->configGroup->getConfigString(ServerProperties::GAME_MODE, GameMode::SURVIVAL()->name())) ?? GameMode::SURVIVAL();
} }
public function getForceGamemode() : bool{ public function getForceGamemode() : bool{
return $this->configGroup->getConfigBool("force-gamemode", false); return $this->configGroup->getConfigBool(ServerProperties::FORCE_GAME_MODE, false);
} }
/** /**
* Returns Server global difficulty. Note that this may be overridden in individual worlds. * Returns Server global difficulty. Note that this may be overridden in individual worlds.
*/ */
public function getDifficulty() : int{ public function getDifficulty() : int{
return $this->configGroup->getConfigInt("difficulty", World::DIFFICULTY_NORMAL); return $this->configGroup->getConfigInt(ServerProperties::DIFFICULTY, World::DIFFICULTY_NORMAL);
} }
public function hasWhitelist() : bool{ public function hasWhitelist() : bool{
return $this->configGroup->getConfigBool("white-list", false); return $this->configGroup->getConfigBool(ServerProperties::WHITELIST, false);
} }
public function isHardcore() : bool{ public function isHardcore() : bool{
return $this->configGroup->getConfigBool("hardcore", false); return $this->configGroup->getConfigBool(ServerProperties::HARDCORE, false);
} }
public function getMotd() : string{ public function getMotd() : string{
return $this->configGroup->getConfigString("motd", self::DEFAULT_SERVER_NAME); return $this->configGroup->getConfigString(ServerProperties::MOTD, self::DEFAULT_SERVER_NAME);
} }
public function getLoader() : ThreadSafeClassLoader{ public function getLoader() : ThreadSafeClassLoader{
@ -811,26 +811,26 @@ class Server{
$this->configGroup = new ServerConfigGroup( $this->configGroup = new ServerConfigGroup(
new Config($pocketmineYmlPath, Config::YAML, []), new Config($pocketmineYmlPath, Config::YAML, []),
new Config(Path::join($this->dataPath, "server.properties"), Config::PROPERTIES, [ new Config(Path::join($this->dataPath, "server.properties"), Config::PROPERTIES, [
"motd" => self::DEFAULT_SERVER_NAME, ServerProperties::MOTD => self::DEFAULT_SERVER_NAME,
"server-port" => self::DEFAULT_PORT_IPV4, ServerProperties::SERVER_PORT_IPV4 => self::DEFAULT_PORT_IPV4,
"server-portv6" => self::DEFAULT_PORT_IPV6, ServerProperties::SERVER_PORT_IPV6 => self::DEFAULT_PORT_IPV6,
"enable-ipv6" => true, ServerProperties::ENABLE_IPV6 => true,
"white-list" => false, ServerProperties::WHITELIST => false,
"max-players" => self::DEFAULT_MAX_PLAYERS, ServerProperties::MAX_PLAYERS => self::DEFAULT_MAX_PLAYERS,
"gamemode" => GameMode::SURVIVAL()->name(), ServerProperties::GAME_MODE => GameMode::SURVIVAL()->name(),
"force-gamemode" => false, ServerProperties::FORCE_GAME_MODE => false,
"hardcore" => false, ServerProperties::HARDCORE => false,
"pvp" => true, ServerProperties::PVP => true,
"difficulty" => World::DIFFICULTY_NORMAL, ServerProperties::DIFFICULTY => World::DIFFICULTY_NORMAL,
"generator-settings" => "", ServerProperties::DEFAULT_WORLD_GENERATOR_SETTINGS => "",
"level-name" => "world", ServerProperties::DEFAULT_WORLD_NAME => "world",
"level-seed" => "", ServerProperties::DEFAULT_WORLD_SEED => "",
"level-type" => "DEFAULT", ServerProperties::DEFAULT_WORLD_GENERATOR => "DEFAULT",
"enable-query" => true, ServerProperties::ENABLE_QUERY => true,
"auto-save" => true, ServerProperties::AUTO_SAVE => true,
"view-distance" => self::DEFAULT_MAX_VIEW_DISTANCE, ServerProperties::VIEW_DISTANCE => self::DEFAULT_MAX_VIEW_DISTANCE,
"xbox-auth" => true, ServerProperties::XBOX_AUTH => true,
"language" => "eng" ServerProperties::LANGUAGE => "eng"
]) ])
); );
@ -840,7 +840,7 @@ class Server{
} }
$this->forceLanguage = $this->configGroup->getPropertyBool("settings.force-language", false); $this->forceLanguage = $this->configGroup->getPropertyBool("settings.force-language", false);
$selectedLang = $this->configGroup->getConfigString("language", $this->configGroup->getPropertyString("settings.language", Language::FALLBACK_LANGUAGE)); $selectedLang = $this->configGroup->getConfigString(ServerProperties::LANGUAGE, $this->configGroup->getPropertyString("settings.language", Language::FALLBACK_LANGUAGE));
try{ try{
$this->language = new Language($selectedLang); $this->language = new Language($selectedLang);
}catch(LanguageNotFoundException $e){ }catch(LanguageNotFoundException $e){
@ -932,9 +932,9 @@ class Server{
$this->banByIP = new BanList($bannedIpsTxt); $this->banByIP = new BanList($bannedIpsTxt);
$this->banByIP->load(); $this->banByIP->load();
$this->maxPlayers = $this->configGroup->getConfigInt("max-players", self::DEFAULT_MAX_PLAYERS); $this->maxPlayers = $this->configGroup->getConfigInt(ServerProperties::MAX_PLAYERS, self::DEFAULT_MAX_PLAYERS);
$this->onlineMode = $this->configGroup->getConfigBool("xbox-auth", true); $this->onlineMode = $this->configGroup->getConfigBool(ServerProperties::XBOX_AUTH, true);
if($this->onlineMode){ if($this->onlineMode){
$this->logger->info($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_auth_enabled())); $this->logger->info($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_auth_enabled()));
}else{ }else{
@ -943,8 +943,8 @@ class Server{
$this->logger->warning($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_authProperty_disabled())); $this->logger->warning($this->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_authProperty_disabled()));
} }
if($this->configGroup->getConfigBool("hardcore", false) && $this->getDifficulty() < World::DIFFICULTY_HARD){ if($this->configGroup->getConfigBool(ServerProperties::HARDCORE, false) && $this->getDifficulty() < World::DIFFICULTY_HARD){
$this->configGroup->setConfigInt("difficulty", World::DIFFICULTY_HARD); $this->configGroup->setConfigInt(ServerProperties::DIFFICULTY, World::DIFFICULTY_HARD);
} }
@cli_set_process_title($this->getName() . " " . $this->getPocketMineVersion()); @cli_set_process_title($this->getName() . " " . $this->getPocketMineVersion());
@ -1001,7 +1001,7 @@ class Server{
} }
$this->worldManager = new WorldManager($this, Path::join($this->dataPath, "worlds"), $providerManager); $this->worldManager = new WorldManager($this, Path::join($this->dataPath, "worlds"), $providerManager);
$this->worldManager->setAutoSave($this->configGroup->getConfigBool("auto-save", $this->worldManager->getAutoSave())); $this->worldManager->setAutoSave($this->configGroup->getConfigBool(ServerProperties::AUTO_SAVE, $this->worldManager->getAutoSave()));
$this->worldManager->setAutoSaveInterval($this->configGroup->getPropertyInt("ticks-per.autosave", $this->worldManager->getAutoSaveInterval())); $this->worldManager->setAutoSaveInterval($this->configGroup->getPropertyInt("ticks-per.autosave", $this->worldManager->getAutoSaveInterval()));
$this->updater = new UpdateChecker($this, $this->configGroup->getPropertyString("auto-updater.host", "update.pmmp.io")); $this->updater = new UpdateChecker($this, $this->configGroup->getPropertyString("auto-updater.host", "update.pmmp.io"));
@ -1136,11 +1136,11 @@ class Server{
} }
if($this->worldManager->getDefaultWorld() === null){ if($this->worldManager->getDefaultWorld() === null){
$default = $this->configGroup->getConfigString("level-name", "world"); $default = $this->configGroup->getConfigString(ServerProperties::DEFAULT_WORLD_NAME, "world");
if(trim($default) == ""){ if(trim($default) == ""){
$this->getLogger()->warning("level-name cannot be null, using default"); $this->getLogger()->warning("level-name cannot be null, using default");
$default = "world"; $default = "world";
$this->configGroup->setConfigString("level-name", "world"); $this->configGroup->setConfigString(ServerProperties::DEFAULT_WORLD_NAME, "world");
} }
if(!$this->worldManager->loadWorld($default, true)){ if(!$this->worldManager->loadWorld($default, true)){
if($this->worldManager->isWorldGenerated($default)){ if($this->worldManager->isWorldGenerated($default)){
@ -1148,8 +1148,8 @@ class Server{
return false; return false;
} }
$generatorName = $this->configGroup->getConfigString("level-type"); $generatorName = $this->configGroup->getConfigString(ServerProperties::DEFAULT_WORLD_GENERATOR);
$generatorOptions = $this->configGroup->getConfigString("generator-settings"); $generatorOptions = $this->configGroup->getConfigString(ServerProperties::DEFAULT_WORLD_GENERATOR_SETTINGS);
$generatorClass = $getGenerator($generatorName, $generatorOptions, $default); $generatorClass = $getGenerator($generatorName, $generatorOptions, $default);
if($generatorClass === null){ if($generatorClass === null){
@ -1159,7 +1159,7 @@ class Server{
$creationOptions = WorldCreationOptions::create() $creationOptions = WorldCreationOptions::create()
->setGeneratorClass($generatorClass) ->setGeneratorClass($generatorClass)
->setGeneratorOptions($generatorOptions); ->setGeneratorOptions($generatorOptions);
$convertedSeed = Generator::convertSeed($this->configGroup->getConfigString("level-seed")); $convertedSeed = Generator::convertSeed($this->configGroup->getConfigString(ServerProperties::DEFAULT_WORLD_SEED));
if($convertedSeed !== null){ if($convertedSeed !== null){
$creationOptions->setSeed($convertedSeed); $creationOptions->setSeed($convertedSeed);
} }
@ -1213,7 +1213,7 @@ class Server{
} }
private function startupPrepareNetworkInterfaces() : bool{ private function startupPrepareNetworkInterfaces() : bool{
$useQuery = $this->configGroup->getConfigBool("enable-query", true); $useQuery = $this->configGroup->getConfigBool(ServerProperties::ENABLE_QUERY, true);
$typeConverter = TypeConverter::getInstance(); $typeConverter = TypeConverter::getInstance();
$packetSerializerContext = new PacketSerializerContext($typeConverter->getItemTypeDictionary()); $packetSerializerContext = new PacketSerializerContext($typeConverter->getItemTypeDictionary());
@ -1223,7 +1223,7 @@ class Server{
if( if(
!$this->startupPrepareConnectableNetworkInterfaces($this->getIp(), $this->getPort(), false, $useQuery, $packetBroadcaster, $entityEventBroadcaster, $packetSerializerContext, $typeConverter) || !$this->startupPrepareConnectableNetworkInterfaces($this->getIp(), $this->getPort(), false, $useQuery, $packetBroadcaster, $entityEventBroadcaster, $packetSerializerContext, $typeConverter) ||
( (
$this->configGroup->getConfigBool("enable-ipv6", true) && $this->configGroup->getConfigBool(ServerProperties::ENABLE_IPV6, true) &&
!$this->startupPrepareConnectableNetworkInterfaces($this->getIpV6(), $this->getPortV6(), true, $useQuery, $packetBroadcaster, $entityEventBroadcaster, $packetSerializerContext, $typeConverter) !$this->startupPrepareConnectableNetworkInterfaces($this->getIpV6(), $this->getPortV6(), true, $useQuery, $packetBroadcaster, $entityEventBroadcaster, $packetSerializerContext, $typeConverter)
) )
){ ){

50
src/ServerProperties.php Normal file
View File

@ -0,0 +1,50 @@
<?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 ServerProperties{
public const AUTO_SAVE = "auto-save";
public const DEFAULT_WORLD_GENERATOR = "level-type";
public const DEFAULT_WORLD_GENERATOR_SETTINGS = "generator-settings";
public const DEFAULT_WORLD_NAME = "level-name";
public const DEFAULT_WORLD_SEED = "level-seed";
public const DIFFICULTY = "difficulty";
public const ENABLE_IPV6 = "enable-ipv6";
public const ENABLE_QUERY = "enable-query";
public const FORCE_GAME_MODE = "force-gamemode";
public const GAME_MODE = "gamemode";
public const HARDCORE = "hardcore";
public const LANGUAGE = "language";
public const MAX_PLAYERS = "max-players";
public const MOTD = "motd";
public const PVP = "pvp";
public const SERVER_IPV4 = "server-ip";
public const SERVER_IPV6 = "server-ipv6";
public const SERVER_PORT_IPV4 = "server-port";
public const SERVER_PORT_IPV6 = "server-portv6";
public const VIEW_DISTANCE = "view-distance";
public const WHITELIST = "white-list";
public const XBOX_AUTH = "xbox-auth";
}

View File

@ -28,6 +28,7 @@ use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory; use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames; use pocketmine\permission\DefaultPermissionNames;
use pocketmine\player\GameMode; use pocketmine\player\GameMode;
use pocketmine\ServerProperties;
use function count; use function count;
class DefaultGamemodeCommand extends VanillaCommand{ class DefaultGamemodeCommand extends VanillaCommand{
@ -52,7 +53,7 @@ class DefaultGamemodeCommand extends VanillaCommand{
return true; return true;
} }
$sender->getServer()->getConfigGroup()->setConfigString("gamemode", $gameMode->name()); $sender->getServer()->getConfigGroup()->setConfigString(ServerProperties::GAME_MODE, $gameMode->name());
$sender->sendMessage(KnownTranslationFactory::commands_defaultgamemode_success($gameMode->getTranslatableName())); $sender->sendMessage(KnownTranslationFactory::commands_defaultgamemode_success($gameMode->getTranslatableName()));
return true; return true;
} }

View File

@ -28,6 +28,7 @@ use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory; use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames; use pocketmine\permission\DefaultPermissionNames;
use pocketmine\ServerProperties;
use pocketmine\world\World; use pocketmine\world\World;
use function count; use function count;
@ -54,7 +55,7 @@ class DifficultyCommand extends VanillaCommand{
} }
if($difficulty !== -1){ if($difficulty !== -1){
$sender->getServer()->getConfigGroup()->setConfigInt("difficulty", $difficulty); $sender->getServer()->getConfigGroup()->setConfigInt(ServerProperties::DIFFICULTY, $difficulty);
//TODO: add per-world support //TODO: add per-world support
foreach($sender->getServer()->getWorldManager()->getWorlds() as $world){ foreach($sender->getServer()->getWorldManager()->getWorlds() as $world){

View File

@ -30,6 +30,7 @@ use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames; use pocketmine\permission\DefaultPermissionNames;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\ServerProperties;
use function count; use function count;
use function implode; use function implode;
use function sort; use function sort;
@ -71,7 +72,7 @@ class WhitelistCommand extends VanillaCommand{
case "on": case "on":
if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_ENABLE)){ if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_ENABLE)){
$server = $sender->getServer(); $server = $sender->getServer();
$server->getConfigGroup()->setConfigBool("white-list", true); $server->getConfigGroup()->setConfigBool(ServerProperties::WHITELIST, true);
$this->kickNonWhitelistedPlayers($server); $this->kickNonWhitelistedPlayers($server);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_enabled()); Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_enabled());
} }
@ -79,7 +80,7 @@ class WhitelistCommand extends VanillaCommand{
return true; return true;
case "off": case "off":
if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_DISABLE)){ if($this->testPermission($sender, DefaultPermissionNames::COMMAND_WHITELIST_DISABLE)){
$sender->getServer()->getConfigGroup()->setConfigBool("white-list", false); $sender->getServer()->getConfigGroup()->setConfigBool(ServerProperties::WHITELIST, false);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_disabled()); Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_whitelist_disabled());
} }

View File

@ -119,6 +119,7 @@ use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissibleDelegateTrait; use pocketmine\permission\PermissibleDelegateTrait;
use pocketmine\player\chat\StandardChatFormatter; use pocketmine\player\chat\StandardChatFormatter;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\ServerProperties;
use pocketmine\timings\Timings; use pocketmine\timings\Timings;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;
@ -1844,7 +1845,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
if(!$this->canInteract($entity->getLocation(), self::MAX_REACH_DISTANCE_ENTITY_INTERACTION)){ if(!$this->canInteract($entity->getLocation(), self::MAX_REACH_DISTANCE_ENTITY_INTERACTION)){
$this->logger->debug("Cancelled attack of entity " . $entity->getId() . " due to not currently being interactable"); $this->logger->debug("Cancelled attack of entity " . $entity->getId() . " due to not currently being interactable");
$ev->cancel(); $ev->cancel();
}elseif($this->isSpectator() || ($entity instanceof Player && !$this->server->getConfigGroup()->getConfigBool("pvp"))){ }elseif($this->isSpectator() || ($entity instanceof Player && !$this->server->getConfigGroup()->getConfigBool(ServerProperties::PVP))){
$ev->cancel(); $ev->cancel();
} }