53 public function __construct(
54 private string $dataPath
57 public function run() :
bool{
58 $this->message(VersionInfo::NAME .
" set-up wizard");
61 $langs = Language::getLanguageList();
63 $this->error(
"No language files found, please use provided builds or clone the repository recursively.");
67 $this->message(
"Please select a language");
68 foreach(Utils::stringifyKeys($langs) as $short => $native){
69 $this->writeLine(
" $native => $short");
73 $lang = strtolower($this->getInput(
"Language",
"eng"));
74 if(!isset($langs[$lang])){
75 $this->error(
"Couldn't find the language");
78 }
while($lang ===
null);
82 $this->message($this->lang->translate(KnownTranslationFactory::language_has_been_selected()));
84 if(!$this->showLicense()){
89 $config =
new Config(Path::join($this->dataPath,
"server.properties"), Config::PROPERTIES);
90 $config->set(ServerProperties::LANGUAGE, $lang);
93 if(strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::skip_installer()),
"n",
"y/N")) ===
"y"){
94 $this->printIpDetails();
101 $this->generateBaseConfig($config);
102 $this->generateUserFiles($config);
103 $this->networkFunctions($config);
106 $this->printIpDetails();
113 private function showLicense() :
bool{
114 $this->message($this->lang->translate(KnownTranslationFactory::welcome_to_pocketmine(VersionInfo::NAME)));
117 This program is free software: you can redistribute it and/or modify
118 it under the terms of the GNU Lesser General Public License as published by
119 the Free Software Foundation, either version 3 of the License, or
120 (at your option) any later version.
124 if(strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::accept_license()),
"n",
"y/N")) !==
"y"){
125 $this->error($this->lang->translate(KnownTranslationFactory::you_have_to_accept_the_license(VersionInfo::NAME)));
134 private function welcome() :
void{
135 $this->message($this->lang->translate(KnownTranslationFactory::setting_up_server_now()));
136 $this->message($this->lang->translate(KnownTranslationFactory::default_values_info()));
137 $this->message($this->lang->translate(KnownTranslationFactory::server_properties()));
140 private function askPort(
Translatable $prompt,
int $default) :
int{
142 $port = (int) $this->getInput($this->lang->translate($prompt), (
string) $default);
143 if($port <= 0 || $port > 65535){
144 $this->error($this->lang->translate(KnownTranslationFactory::invalid_port()));
152 private function generateBaseConfig(
Config $config) :
void{
153 $config->set(ServerProperties::MOTD, ($name = $this->getInput($this->lang->translate(KnownTranslationFactory::name_your_server()), Server::DEFAULT_SERVER_NAME)));
155 $this->message($this->lang->translate(KnownTranslationFactory::port_warning()));
157 $config->set(ServerProperties::SERVER_PORT_IPV4, $this->askPort(KnownTranslationFactory::server_port_v4(), Server::DEFAULT_PORT_IPV4));
158 $config->set(ServerProperties::SERVER_PORT_IPV6, $this->askPort(KnownTranslationFactory::server_port_v6(), Server::DEFAULT_PORT_IPV6));
160 $this->message($this->lang->translate(KnownTranslationFactory::gamemode_info()));
163 $input = (int) $this->getInput($this->lang->translate(KnownTranslationFactory::default_gamemode()),
"0");
164 $gamemode = match($input){
165 0 => GameMode::SURVIVAL,
166 1 => GameMode::CREATIVE,
169 }
while($gamemode ===
null);
171 $config->set(ServerProperties::GAME_MODE, $gamemode->name);
173 $config->set(ServerProperties::MAX_PLAYERS, (
int) $this->getInput($this->lang->translate(KnownTranslationFactory::max_players()), (
string) Server::DEFAULT_MAX_PLAYERS));
175 $config->set(ServerProperties::VIEW_DISTANCE, (
int) $this->getInput($this->lang->translate(KnownTranslationFactory::view_distance()), (
string) Server::DEFAULT_MAX_VIEW_DISTANCE));
178 private function generateUserFiles(
Config $config) :
void{
179 $this->message($this->lang->translate(KnownTranslationFactory::op_info()));
181 $op = strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::op_who()),
""));
183 $this->error($this->lang->translate(KnownTranslationFactory::op_warning()));
185 $ops =
new Config(Path::join($this->dataPath,
"ops.txt"), Config::ENUM);
186 $ops->set($op,
true);
190 $this->message($this->lang->translate(KnownTranslationFactory::whitelist_info()));
192 if(strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::whitelist_enable()),
"n",
"y/N")) ===
"y"){
193 $this->error($this->lang->translate(KnownTranslationFactory::whitelist_warning()));
194 $config->set(ServerProperties::WHITELIST,
true);
196 $config->set(ServerProperties::WHITELIST,
false);
200 private function networkFunctions(
Config $config) :
void{
201 $this->error($this->lang->translate(KnownTranslationFactory::query_warning1()));
202 $this->error($this->lang->translate(KnownTranslationFactory::query_warning2()));
203 if(strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::query_disable()),
"n",
"y/N")) ===
"y"){
204 $config->set(ServerProperties::ENABLE_QUERY,
false);
206 $config->set(ServerProperties::ENABLE_QUERY,
true);
210 private function printIpDetails() :
void{
211 $this->message($this->lang->translate(KnownTranslationFactory::ip_get()));
213 $externalIP = Internet::getIP();
214 if($externalIP ===
false){
215 $externalIP =
"unknown (server offline)";
218 $internalIP = Internet::getInternalIP();
220 $internalIP =
"unknown (" . $e->getMessage() .
")";
223 $this->error($this->lang->translate(KnownTranslationFactory::ip_warning($externalIP, $internalIP)));
224 $this->error($this->lang->translate(KnownTranslationFactory::ip_confirm()));
228 private function endWizard() :
void{
229 $this->message($this->lang->translate(KnownTranslationFactory::you_have_finished()));
230 $this->message($this->lang->translate(KnownTranslationFactory::pocketmine_plugins()));
231 $this->message($this->lang->translate(KnownTranslationFactory::pocketmine_will_start(VersionInfo::NAME)));
239 private function writeLine(
string $line =
"") :
void{
240 echo $line . PHP_EOL;
243 private function readLine() :
string{
244 return trim((
string) fgets(STDIN));
247 private function message(
string $message) :
void{
248 $this->writeLine(
"[*] " . $message);
251 private function error(
string $message) :
void{
252 $this->writeLine(
"[!] " . $message);
255 private function getInput(
string $message,
string $default =
"",
string $options =
"") :
string{
256 $message =
"[?] " . $message;
258 if($options !==
"" || $default !==
""){
259 $message .=
" (" . ($options ===
"" ? $default : $options) .
")";
265 $input = $this->readLine();
267 return $input ===
"" ? $default : $input;