Handle incorrect ping.

This commit is contained in:
filoghost 2015-02-07 18:54:42 +01:00
parent 52e6b1ed97
commit db03559136

View File

@ -4,6 +4,8 @@ import java.lang.String;
import org.json.simple.JSONObject;
import com.gmail.filoghost.holographicdisplays.util.DebugHandler;
public class PingResponse
{
private boolean isOnline;
@ -20,11 +22,31 @@ public class PingResponse
public PingResponse(JSONObject json) {
isOnline = true;
motd = ((String) json.get("description"));
Object descriptionObject = json.get("description");
if (descriptionObject != null) {
motd = descriptionObject.toString();
} else {
motd = "Invalid ping response";
DebugHandler.logToConsole("Received invalid ping response: " + json.toString());
}
JSONObject playersJson = (JSONObject) json.get("players");
onlinePlayers = ((Long) playersJson.get("online")).intValue();
maxPlayers = ((Long) playersJson.get("max")).intValue();
Object playersObject = json.get("players");
if (playersObject instanceof JSONObject) {
JSONObject playersJson = (JSONObject) playersObject;
Object onlineObject = playersJson.get("online");
if (onlineObject instanceof Number) {
onlinePlayers = ((Number) onlineObject).intValue();
}
Object maxObject = playersJson.get("max");
if (maxObject instanceof Number) {
maxPlayers = ((Number) maxObject).intValue();
}
}
}
public boolean isOnline() {