PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
SetupWizard.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
29
34use pocketmine\player\GameMode;
42use Symfony\Component\Filesystem\Path;
43use function fgets;
44use function sleep;
45use function strtolower;
46use function trim;
47use const PHP_EOL;
48use const STDIN;
49
51 private Language $lang;
52
53 public function __construct(
54 private string $dataPath
55 ){}
56
57 public function run() : bool{
58 $this->message(VersionInfo::NAME . " set-up wizard");
59
60 try{
61 $langs = Language::getLanguageList();
63 $this->error("No language files found, please use provided builds or clone the repository recursively.");
64 return false;
65 }
66
67 $this->message("Please select a language");
68 foreach(Utils::stringifyKeys($langs) as $short => $native){
69 $this->writeLine(" $native => $short");
70 }
71
72 do{
73 $lang = strtolower($this->getInput("Language", "eng"));
74 if(!isset($langs[$lang])){
75 $this->error("Couldn't find the language");
76 $lang = null;
77 }
78 }while($lang === null);
79
80 $this->lang = new Language($lang);
81
82 $this->message($this->lang->translate(KnownTranslationFactory::language_has_been_selected()));
83
84 if(!$this->showLicense()){
85 return false;
86 }
87
88 //this has to happen here to prevent user avoiding agreeing to license
89 $config = new Config(Path::join($this->dataPath, "server.properties"), Config::PROPERTIES);
90 $config->set(ServerProperties::LANGUAGE, $lang);
91 $config->save();
92
93 if(strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::skip_installer()), "n", "y/N")) === "y"){
94 $this->printIpDetails();
95 return true;
96 }
97
98 $this->writeLine();
99 $this->welcome();
100
101 $this->generateBaseConfig($config);
102 $this->generateUserFiles($config);
103 $this->networkFunctions($config);
104 $config->save();
105
106 $this->printIpDetails();
107
108 $this->endWizard();
109
110 return true;
111 }
112
113 private function showLicense() : bool{
114 $this->message($this->lang->translate(KnownTranslationFactory::welcome_to_pocketmine(VersionInfo::NAME)));
115 echo <<<LICENSE
116
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.
121
122LICENSE;
123 $this->writeLine();
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)));
126 sleep(5);
127
128 return false;
129 }
130
131 return true;
132 }
133
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()));
138 }
139
140 private function askPort(Translatable $prompt, int $default) : int{
141 while(true){
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()));
145 continue;
146 }
147
148 return $port;
149 }
150 }
151
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)));
154
155 $this->message($this->lang->translate(KnownTranslationFactory::port_warning()));
156
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));
159
160 $this->message($this->lang->translate(KnownTranslationFactory::gamemode_info()));
161
162 do{
163 $input = (int) $this->getInput($this->lang->translate(KnownTranslationFactory::default_gamemode()), "0");
164 $gamemode = match($input){
165 0 => GameMode::SURVIVAL,
166 1 => GameMode::CREATIVE,
167 default => null
168 };
169 }while($gamemode === null);
170 //TODO: this probably shouldn't use the enum name directly
171 $config->set(ServerProperties::GAME_MODE, $gamemode->name);
172
173 $config->set(ServerProperties::MAX_PLAYERS, (int) $this->getInput($this->lang->translate(KnownTranslationFactory::max_players()), (string) Server::DEFAULT_MAX_PLAYERS));
174
175 $config->set(ServerProperties::VIEW_DISTANCE, (int) $this->getInput($this->lang->translate(KnownTranslationFactory::view_distance()), (string) Server::DEFAULT_MAX_VIEW_DISTANCE));
176 }
177
178 private function generateUserFiles(Config $config) : void{
179 $this->message($this->lang->translate(KnownTranslationFactory::op_info()));
180
181 $op = strtolower($this->getInput($this->lang->translate(KnownTranslationFactory::op_who()), ""));
182 if($op === ""){
183 $this->error($this->lang->translate(KnownTranslationFactory::op_warning()));
184 }else{
185 $ops = new Config(Path::join($this->dataPath, "ops.txt"), Config::ENUM);
186 $ops->set($op, true);
187 $ops->save();
188 }
189
190 $this->message($this->lang->translate(KnownTranslationFactory::whitelist_info()));
191
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);
195 }else{
196 $config->set(ServerProperties::WHITELIST, false);
197 }
198 }
199
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);
205 }else{
206 $config->set(ServerProperties::ENABLE_QUERY, true);
207 }
208 }
209
210 private function printIpDetails() : void{
211 $this->message($this->lang->translate(KnownTranslationFactory::ip_get()));
212
213 $externalIP = Internet::getIP();
214 if($externalIP === false){
215 $externalIP = "unknown (server offline)";
216 }
217 try{
218 $internalIP = Internet::getInternalIP();
219 }catch(InternetException $e){
220 $internalIP = "unknown (" . $e->getMessage() . ")";
221 }
222
223 $this->error($this->lang->translate(KnownTranslationFactory::ip_warning($externalIP, $internalIP)));
224 $this->error($this->lang->translate(KnownTranslationFactory::ip_confirm()));
225 $this->readLine();
226 }
227
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)));
232
233 $this->writeLine();
234 $this->writeLine();
235
236 sleep(4);
237 }
238
239 private function writeLine(string $line = "") : void{
240 echo $line . PHP_EOL;
241 }
242
243 private function readLine() : string{
244 return trim((string) fgets(STDIN));
245 }
246
247 private function message(string $message) : void{
248 $this->writeLine("[*] " . $message);
249 }
250
251 private function error(string $message) : void{
252 $this->writeLine("[!] " . $message);
253 }
254
255 private function getInput(string $message, string $default = "", string $options = "") : string{
256 $message = "[?] " . $message;
257
258 if($options !== "" || $default !== ""){
259 $message .= " (" . ($options === "" ? $default : $options) . ")";
260 }
261 $message .= ": ";
262
263 echo $message;
264
265 $input = $this->readLine();
266
267 return $input === "" ? $default : $input;
268 }
269}