Skip to content

Commit ee117ed

Browse files
committed
small cleanup
1 parent 8a1a325 commit ee117ed

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

player-counter/src/Extensions/Query/Schemas/MinecraftJavaQueryTypeSchema.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,9 @@ public function process(string $ip, int $port): ?array
2727
return $query;
2828
}
2929

30-
try {
31-
$ping = new MinecraftPing($ip, $port, 5, true);
32-
33-
$data = $ping->Query();
34-
35-
if (!$data) {
36-
return null;
37-
}
38-
39-
return [
40-
'hostname' => is_string($data['description']) ? $data['description'] : $data['description']['text'],
41-
'map' => 'world', // No map from MinecraftPing
42-
'current_players' => $data['players']['online'],
43-
'max_players' => $data['players']['max'],
44-
'players' => $data['players']['sample'] ?? [],
45-
];
46-
} catch (Exception $exception) {
47-
report($exception);
48-
} finally {
49-
if (isset($ping)) {
50-
$ping->Close();
51-
}
30+
$ping = $this->tryPing($ip, $port);
31+
if ($ping) {
32+
return $ping;
5233
}
5334

5435
return null;
@@ -82,4 +63,34 @@ protected function tryQuery(string $ip, int $port): false|array
8263

8364
return false;
8465
}
66+
67+
/** @return false|array{hostname: string, map: string, current_players: int, max_players: int, players: array<array{id: string, name: string}>} */
68+
protected function tryPing(string $ip, int $port): false|array
69+
{
70+
try {
71+
$ping = new MinecraftPing($ip, $port, 5, true);
72+
73+
$data = $ping->Query();
74+
75+
if (!$data) {
76+
return false;
77+
}
78+
79+
return [
80+
'hostname' => is_string($data['description']) ? $data['description'] : $data['description']['text'],
81+
'map' => 'world', // No map from MinecraftPing
82+
'current_players' => $data['players']['online'],
83+
'max_players' => $data['players']['max'],
84+
'players' => $data['players']['sample'] ?? [],
85+
];
86+
} catch (Exception $exception) {
87+
report($exception);
88+
} finally {
89+
if (isset($ping)) {
90+
$ping->Close();
91+
}
92+
}
93+
94+
return false;
95+
}
8596
}

0 commit comments

Comments
 (0)