Created
March 24, 2019 20:34
-
-
Save Skillkiller/ed685d1acbf6548fa2d9b1bdfd44589a to your computer and use it in GitHub Desktop.
Java Klasse zum Abfragen von Server Description, Online Spieler und Maximale Spieleranzahl
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
@Getter | |
public class Status { | |
private int port; | |
private String host; | |
private int players; | |
private int maxPlayers; | |
private String description; | |
public Status( String host, int port ) throws IOException { | |
this.port = port; | |
this.host = host; | |
rescann(); | |
} | |
private void rescann() throws IOException { | |
Socket socket = new Socket(host, 25565); | |
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); | |
DataInputStream in = new DataInputStream(socket.getInputStream()); | |
out.write(hexStringToByteArray("FE")); | |
byte[] b = new byte[241]; | |
in.read(b, 0, 241); | |
StringBuffer buffer = new StringBuffer(); | |
for (int i = 4; i < b.length; i++) { | |
if (b[i] != 0) { | |
buffer.append((char) b[i]); | |
} | |
} | |
String[] split = buffer.toString().split(String.valueOf((char) -89)); | |
description = split[0]; | |
players = Integer.parseInt( split[1] ); | |
maxPlayers = Integer.parseInt( split[2] ); | |
out.close(); | |
in.close(); | |
socket.close(); | |
} | |
private static byte[] hexStringToByteArray(String s) { | |
int len = s.length(); | |
byte[] data = new byte[len / 2]; | |
for (int i = 0; i < len; i += 2) { | |
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment