Created
December 17, 2012 12:12
-
-
Save goosewoman/4317858 to your computer and use it in GitHub Desktop.
Minecraft query method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String[] minecraftQuery(String address) { | |
return minecraftQuery(address, 25565); | |
} | |
public String[] minecraftQuery(String address, int port) { | |
byte fe1 = (byte) 0xFE; | |
byte fe2 = (byte) 0x01; | |
byte[] fe = {fe1, fe2}; | |
Socket socket = null; | |
try { | |
socket = new Socket(address, port); | |
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream()); | |
os.write(fe); | |
os.flush(); | |
String response = is.readLine(); | |
response = response.substring(9); | |
String[] data = response.split("\\u0000\\u0000"); | |
String[] output = new String[5]; | |
int i = 0; | |
for (String Data : data) { | |
String after = Data.trim().replaceAll("\u0000+", ""); | |
if (after == null || after == "") { | |
continue; | |
} | |
output[i] = after; | |
i++; | |
} | |
//output[0] = protocol version | |
//output[1] = server version | |
//output[2] = Motd | |
//output[3] = Amount of players | |
//output[4] = Maximum amount of players. | |
return output; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment