98 lines
2.3 KiB
Java
98 lines
2.3 KiB
Java
package eu.univento.core.api;
|
|
|
|
import java.io.DataOutputStream;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.Socket;
|
|
import java.nio.charset.Charset;
|
|
|
|
public class ServerPinger
|
|
{
|
|
private String host;
|
|
private int port;
|
|
|
|
public ServerPinger(String host, int port)
|
|
{
|
|
this.host = host;
|
|
this.port = port;
|
|
}
|
|
|
|
public ServerPinger(String host) {
|
|
this.host = host;
|
|
this.port = 25565;
|
|
}
|
|
|
|
public ServerPinger() {
|
|
this.host = "127.0.0.1";
|
|
this.port = 25565;
|
|
}
|
|
|
|
@SuppressWarnings("resource")
|
|
public String parseData(Connection connection)
|
|
{
|
|
try
|
|
{
|
|
Socket socket = new Socket();
|
|
|
|
socket.setSoTimeout(2500);
|
|
socket.connect(new InetSocketAddress(this.host, this.port));
|
|
|
|
OutputStream os = socket.getOutputStream();
|
|
DataOutputStream dos = new DataOutputStream(os);
|
|
|
|
InputStream is = socket.getInputStream();
|
|
InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
|
|
|
|
dos.write(new byte[] { -2, 1 });
|
|
|
|
int packetID = is.read();
|
|
|
|
if (packetID == -1) {
|
|
System.out.println("Invalid Packet ID! (End Of Stream)");
|
|
}
|
|
if (packetID != 255) {
|
|
System.out.println("Invalid Packet Id! " + packetID);
|
|
}
|
|
|
|
int length = isr.read();
|
|
|
|
if (length == -1) {
|
|
System.out.println("End Of Stream");
|
|
}
|
|
|
|
if (length == 0) {
|
|
System.out.println("Invalid length");
|
|
}
|
|
|
|
char[] chars = new char[length];
|
|
|
|
if (isr.read(chars, 0, length) != length) {
|
|
System.out.println("End Of Stream");
|
|
}
|
|
|
|
String string = new String(chars);
|
|
String[] data = string.split("");
|
|
|
|
if (connection == Connection.ONLINE_PLAYERS)
|
|
return data[4];
|
|
if (connection == Connection.MOTD)
|
|
return data[3];
|
|
if (connection == Connection.MAX_PLAYERS) {
|
|
return data[5];
|
|
}
|
|
System.out.println("Connection value not handled!");
|
|
}
|
|
catch (Exception localException)
|
|
{
|
|
localException.printStackTrace();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static enum Connection {
|
|
ONLINE_PLAYERS, MAX_PLAYERS, MOTD;
|
|
}
|
|
} |