Created
May 7, 2013 12:57
-
-
Save molenzwiebel/5532348 to your computer and use it in GitHub Desktop.
Code to poll a minecraft server
This file contains 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
private String MOTD; | |
private String gameVersion; //Currently 1.5.2 | |
private int onlinePlayers; | |
private int maxPlayers; | |
public static String readString(DataInputStream par0DataInputStream, int par1) throws IOException | |
{ | |
short short1 = par0DataInputStream.readShort(); | |
if (short1 > par1) | |
{ | |
throw new IOException("Received string length longer than maximum allowed (" + short1 + " > " + par1 + ")"); | |
} | |
else if (short1 < 0) | |
{ | |
throw new IOException("Received string length is less than zero! Weird string!"); | |
} | |
else | |
{ | |
StringBuilder stringbuilder = new StringBuilder(); | |
for (int j = 0; j < short1; ++j) | |
{ | |
stringbuilder.append(par0DataInputStream.readChar()); | |
} | |
return stringbuilder.toString(); | |
} | |
} | |
private String allowedChars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø׃áíóúñѪº¿®¬½¼¡«»"; | |
private void pollServer(String ip, int port) throws IOException | |
{ | |
Socket socket = null; | |
DataInputStream datainputstream = null; | |
DataOutputStream dataoutputstream = null; | |
try | |
{ | |
socket = new Socket(); | |
socket.setSoTimeout(3000); | |
socket.setTcpNoDelay(true); | |
socket.setTrafficClass(18); | |
socket.connect(new InetSocketAddress(ip, port), 3000); | |
datainputstream = new DataInputStream(socket.getInputStream()); | |
dataoutputstream = new DataOutputStream(socket.getOutputStream()); | |
dataoutputstream.write(254); | |
dataoutputstream.write(1); | |
if (datainputstream.read() != 255) | |
{ | |
throw new IOException("Bad message"); | |
} | |
String s = readString(datainputstream, 256); | |
char[] achar = s.toCharArray(); | |
for (int i = 0; i < achar.length; ++i) | |
{ | |
if (achar[i] != 167 && achar[i] != 0 && allowedChars.indexOf(achar[i]) < 0) | |
{ | |
achar[i] = 63; | |
} | |
} | |
s = new String(achar); | |
int j; | |
int k; | |
String[] astring; | |
if (s.startsWith("\u00a7") && s.length() > 1) | |
{ | |
astring = s.substring(1).split("\u0000"); | |
if (Integer.parseInt(astring[0]) == 1) | |
{ | |
this.MOTD = astring[3]; | |
this.gameVersion = astring[2]; | |
j = Integer.parseInt(astring[4]); | |
k = Integer.parseInt(astring[5]); | |
if (j >= 0 && k >= 0) | |
{ | |
onlinePlayers = j; | |
maxPlayers = k; | |
} | |
else | |
{ | |
//Players unknown | |
} | |
} | |
else | |
{ | |
gameVersion = "???"; | |
MOTD = "???"; | |
onlinePlayers = -1; | |
maxPlayers = -1; | |
} | |
} | |
else | |
{ | |
astring = s.split("\u00a7"); | |
s = astring[0]; | |
j = -1; | |
k = -1; | |
try | |
{ | |
j = Integer.parseInt(astring[1]); | |
k = Integer.parseInt(astring[2]); | |
} | |
catch (Exception exception) | |
{ | |
; | |
} | |
MOTD = s; | |
if (j >= 0 && k > 0) | |
{ | |
onlinePlayers = j; | |
maxPlayers = k; | |
} | |
else | |
{ | |
//Players unknown | |
} | |
gameVersion = "1.3"; | |
} | |
} | |
finally | |
{ | |
try | |
{ | |
if (datainputstream != null) | |
{ | |
datainputstream.close(); | |
} | |
} | |
catch (Throwable throwable) | |
{ | |
; | |
} | |
try | |
{ | |
if (dataoutputstream != null) | |
{ | |
dataoutputstream.close(); | |
} | |
} | |
catch (Throwable throwable1) | |
{ | |
; | |
} | |
try | |
{ | |
if (socket != null) | |
{ | |
socket.close(); | |
} | |
} | |
catch (Throwable throwable2) | |
{ | |
; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment