40 public const GAME_ID =
"MINECRAFTPE";
42 private string $serverName;
43 private bool $listPlugins;
45 private array $plugins;
47 private array $players;
49 private string $gametype;
50 private string $version;
51 private string $server_engine;
53 private int $numPlayers;
54 private int $maxPlayers;
55 private string $whitelist;
63 private array $extraData = [];
65 private ?
string $longQueryCache =
null;
66 private ?
string $shortQueryCache =
null;
68 public function __construct(
Server $server){
69 $this->serverName = $server->getMotd();
70 $this->listPlugins = $server->getConfigGroup()->getPropertyBool(YmlServerProperties::SETTINGS_QUERY_PLUGINS,
true);
71 $this->plugins = $server->getPluginManager()->getPlugins();
74 $this->gametype = ($server->getGamemode() === GameMode::SURVIVAL || $server->getGamemode() === GameMode::ADVENTURE) ?
"SMP" :
"CMP";
75 $this->version = $server->getVersion();
76 $this->server_engine = $server->getName() .
" " . $server->getPocketMineVersion();
77 $world = $server->getWorldManager()->getDefaultWorld();
78 $this->map = $world ===
null ?
"unknown" : $world->getDisplayName();
79 $this->numPlayers = count($this->players);
80 $this->maxPlayers = $server->getMaxPlayers();
81 $this->whitelist = $server->hasWhitelist() ?
"on" :
"off";
82 $this->port = $server->getPort();
83 $this->ip = $server->getIp();
87 private function destroyCache() :
void{
88 $this->longQueryCache =
null;
89 $this->shortQueryCache =
null;
92 public function getServerName() :
string{
93 return $this->serverName;
96 public function setServerName(
string $serverName) :
void{
97 $this->serverName = $serverName;
98 $this->destroyCache();
101 public function canListPlugins() :
bool{
102 return $this->listPlugins;
105 public function setListPlugins(
bool $value) :
void{
106 $this->listPlugins = $value;
107 $this->destroyCache();
114 return $this->plugins;
121 Utils::validateArrayValueType($plugins, function(
Plugin $_) : void{});
122 $this->plugins = $plugins;
123 $this->destroyCache();
130 return $this->players;
137 Utils::validateArrayValueType($players, function(string $_) : void{});
138 $this->players = $players;
139 $this->destroyCache();
142 public function getPlayerCount() : int{
143 return $this->numPlayers;
146 public function setPlayerCount(
int $count) : void{
147 $this->numPlayers = $count;
148 $this->destroyCache();
151 public function getMaxPlayerCount() : int{
152 return $this->maxPlayers;
155 public function setMaxPlayerCount(
int $count) : void{
156 $this->maxPlayers = $count;
157 $this->destroyCache();
160 public function getWorld() : string{
164 public function setWorld(
string $world) : void{
166 $this->destroyCache();
176 return $this->extraData;
185 $this->extraData = $extraData;
186 $this->destroyCache();
189 public function getLongQuery() : string{
190 if($this->longQueryCache !== null){
191 return $this->longQueryCache;
195 $plist = $this->server_engine;
196 if(count($this->plugins) > 0 && $this->listPlugins){
198 foreach($this->plugins as $p){
199 $d = $p->getDescription();
200 $plist .=
" " . str_replace([
";",
":",
" "], [
"",
"",
"_"], $d->getName()) .
" " . str_replace([
";",
":",
" "], [
"",
"",
"_"], $d->getVersion()) .
";";
202 $plist = substr($plist, 0, -1);
206 "splitnum" => chr(128),
207 "hostname" => $this->serverName,
208 "gametype" => $this->gametype,
209 "game_id" => self::GAME_ID,
210 "version" => $this->version,
211 "server_engine" => $this->server_engine,
214 "numplayers" => $this->numPlayers,
215 "maxplayers" => $this->maxPlayers,
216 "whitelist" => $this->whitelist,
217 "hostip" => $this->ip,
218 "hostport" => $this->port
221 foreach($KVdata as $key => $value){
222 $query .= $key .
"\x00" . $value .
"\x00";
225 foreach(Utils::stringifyKeys($this->extraData) as $key => $value){
226 $query .= $key .
"\x00" . $value .
"\x00";
229 $query .=
"\x00\x01player_\x00\x00";
230 foreach($this->players as $player){
231 $query .= $player .
"\x00";
235 return $this->longQueryCache = $query;
238 public function getShortQuery() : string{
239 return $this->shortQueryCache ?? ($this->shortQueryCache = $this->serverName .
"\x00" . $this->gametype .
"\x00" . $this->map .
"\x00" . $this->numPlayers .
"\x00" . $this->maxPlayers .
"\x00" . Binary::writeLShort($this->port) . $this->ip .
"\x00");