@@ -15,7 +15,7 @@ public final class MCServerPingResponse {
15
15
private final JsonArray descriptionExtras ;
16
16
private final String serverIcon ;
17
17
18
- public MCServerPingResponse (int ping , String name , int protocol , int playerMax , int playerOnline , String motd , JsonArray descriptionExtras , String serverIcon ) {
18
+ public MCServerPingResponse (int ping , String name , int protocol , Integer playerMax , Integer playerOnline , String motd , JsonArray descriptionExtras , String serverIcon ) {
19
19
this .ping = ping ;
20
20
this .version = name ;
21
21
this .protocol = protocol ;
@@ -27,15 +27,20 @@ public MCServerPingResponse(int ping, String name, int protocol, int playerMax,
27
27
}
28
28
29
29
public static MCServerPingResponse serverPingFromJsonObj (JsonObject jsonObj ) {
30
+ int serverPing = jsonObj .get ("ping" ).getAsInt ();
31
+ String versionName = jsonObj .get ("version" ).getAsString ();
32
+ int serverProtocol = jsonObj .get ("protocol" ).getAsInt ();
33
+ Integer playerMax = null ;
34
+ Integer playerOnline = null ;
35
+ if (jsonObj .has ("players" )) { // Players object is optional somehow
36
+ playerMax = jsonObj .get ("players" ).getAsJsonObject ().get ("max" ).getAsInt ();
37
+ playerOnline = jsonObj .get ("players" ).getAsJsonObject ().get ("online" ).getAsInt ();
38
+ }
39
+ String serverMOTD = jsonObj .get ("description" ).getAsJsonObject ().get ("text" ).getAsString ();
40
+ JsonArray serverDescriptionExtra = (jsonObj .get ("description" ).getAsJsonObject ().get ("extra" ) == null ) ? null : jsonObj .get ("description" ).getAsJsonObject ().get ("extra" ).getAsJsonArray ();
41
+ String favIcon = jsonObj .get ("favicon" ).getAsString ();
30
42
return new MCServerPingResponse (
31
- jsonObj .get ("ping" ).getAsInt (),
32
- jsonObj .get ("version" ).getAsJsonObject ().get ("name" ).getAsString (),
33
- jsonObj .get ("version" ).getAsJsonObject ().get ("protocol" ).getAsInt (),
34
- jsonObj .get ("players" ).getAsJsonObject ().get ("max" ).getAsInt (),
35
- jsonObj .get ("players" ).getAsJsonObject ().get ("online" ).getAsInt (),
36
- jsonObj .get ("description" ).getAsJsonObject ().get ("text" ).getAsString (),
37
- (jsonObj .get ("description" ).getAsJsonObject ().get ("extra" ) == null ) ? null : jsonObj .get ("description" ).getAsJsonObject ().get ("extra" ).getAsJsonArray (),
38
- (jsonObj .get ("favicon" ) == null ) ? null : jsonObj .get ("favicon" ).getAsString ()
43
+ serverPing , versionName , serverProtocol , playerMax , playerOnline , serverMOTD , serverDescriptionExtra , favIcon
39
44
);
40
45
}
41
46
@@ -51,11 +56,11 @@ public int getProtocol() {
51
56
return protocol ;
52
57
}
53
58
54
- public int getPlayerMax () {
59
+ public Integer getPlayerMax () {
55
60
return maxPlayers ;
56
61
}
57
62
58
- public int getPlayerOnline () {
63
+ public Integer getPlayerOnline () {
59
64
return onlinePlayers ;
60
65
}
61
66
0 commit comments