if (queryResult == null || queryResult.length() == 0) {
return null;
}
ServerInfo serverInfo = new ServerInfo();
try {
// verify we got what we wanted
int[] offsetHolder = {0};
if (queryResult.charAt(0) == 128) {
offsetHolder[0] = 4;
}
if (queryResult.charAt(offsetHolder[0]) != 0) {
return null;
}
offsetHolder[0]++;
serverInfo.setIp(ipStr);
getInt(queryResult, offsetHolder); // ignore server id
getString(queryResult, offsetHolder); // ignore server ip (because it's not reliable)
serverInfo.setPort(Integer.toString(getInt(queryResult, offsetHolder))); // ignore game port
getInt(queryResult, offsetHolder); // ignore query port
serverInfo.setName(getString(queryResult, offsetHolder)); // server name
serverInfo.setMap(getString(queryResult, offsetHolder)); // map
serverInfo.setGame(getString(queryResult, offsetHolder)); // game type
serverInfo.setPlayerCount(Integer.toString(getInt(queryResult, offsetHolder))); // currently playing
serverInfo.setMaxPlayers(Integer.toString(getInt(queryResult, offsetHolder))); // max players
getInt(queryResult, offsetHolder); // ignore ping
serverInfo.setFullResponse(queryResult);
} catch (StringIndexOutOfBoundsException e) {
return null;
}
// attempt to get game version also.. it's available through
// the game info query
queryResult =
Util.getInfo(localPort, ipStr, port, "\u0078\u0000\u0000\u0000\u0001", infoType, queryType, gameType);
if (queryResult == null || queryResult.length() == 0) {
return serverInfo;
}
try {
// verify we got what we wanted
int[] offsetHolder = {0};
if (queryResult.charAt(0) == 128) {
offsetHolder[0] = 4;
}
if (queryResult.charAt(offsetHolder[0]) == 1) {
offsetHolder[0]++;
// loop through the key-value pairs
while (offsetHolder[0] < queryResult.length()) {
String key = getString(queryResult, offsetHolder);
String value = getString(queryResult, offsetHolder);
if (key.equals("ServerVersion")) {
serverInfo.setGameVersion(value);
break;
}
}
}