Created
December 17, 2012 11:57
-
-
Save goosewoman/4317750 to your computer and use it in GitHub Desktop.
Minecraft Server Query CLI program.
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
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.Socket; | |
public class Main { | |
public static void main(String[] args) { | |
if (args.length < 2) { | |
String[] query = minecraftQuery(args[0]); | |
System.out.println(query[0]); | |
System.out.println(query[1]); | |
System.out.println(query[2]); | |
System.out.println(query[3]); | |
System.out.println(query[4]); | |
} else if (args.length > 1) { | |
String[] query = minecraftQuery(args[0], Integer.parseInt(args[1])); | |
System.out.println(query[0]); | |
System.out.println(query[1]); | |
System.out.println(query[2]); | |
System.out.println(query[3]); | |
System.out.println(query[4]); | |
} | |
} | |
public static String[] minecraftQuery(String address) { | |
return minecraftQuery(address, 25565); | |
} | |
public static 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: " + output[0]; | |
output[1] = "Version: " + output[1]; | |
output[2] = "MOTD: " + output[2]; | |
output[3] = "Players: " + output[3]; | |
output[4] = "Maxplayers: " + output[4]; | |
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