Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.comniemeer.pingapi;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInput;
- import java.io.DataInputStream;
- import java.io.DataOutput;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- import com.eclipsesource.json.JsonObject;
- public class PingResult {
- private String motd;
- private int onlinePlayers;
- private int maxPlayers;
- PingResult(String host, int port, int timeout) {
- try {
- this.ping(host, port);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void ping(String host, int port) throws IOException {
- try (Socket socket = new Socket(host, port)) {
- try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) {
- try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
- try (ByteArrayOutputStream frame = new ByteArrayOutputStream()) {
- try (DataOutputStream frameOut = new DataOutputStream(frame)) {
- // Handshake
- this.writeVarInt(0x00, frameOut);
- this.writeVarInt(4, frameOut);
- this.writeString(host, frameOut);
- frameOut.writeShort(port);
- this.writeVarInt(1, frameOut);
- // Write handshake
- this.writeVarInt(frame.size(), out);
- frame.writeTo(out);
- frame.reset();
- // Ping
- this.writeVarInt(0x00, frameOut);
- // Write ping
- this.writeVarInt(frame.size(), out);
- frame.writeTo(out);
- frame.reset();
- int len = this.readVarInt(in);
- byte[] packet = new byte[len];
- in.readFully(packet);
- try (ByteArrayInputStream inPacket = new ByteArrayInputStream(packet)) {
- try (DataInputStream inFrame = new DataInputStream(inPacket)) {
- int id = this.readVarInt(inFrame);
- if (id != 0x00) {
- throw new IllegalStateException("Wrong ping response");
- }
- JsonObject jsonObject = JsonObject.readFrom(this.readString(inFrame));
- JsonObject jsonPlayers = jsonObject.get("players").asObject();
- this.onlinePlayers = jsonPlayers.get("online").asInt();
- this.maxPlayers = jsonPlayers.get("max").asInt();
- this.motd = jsonObject.get("description").asString();
- }
- }
- }
- }
- }
- }
- } catch (Exception e) {
- ;
- }
- }
- private void writeString(String s, DataOutput out) throws IOException {
- byte[] b = s.getBytes("UTF-8");
- this.writeVarInt(b.length, out);
- out.write(b);
- }
- private String readString(DataInput in) throws IOException {
- int len = this.readVarInt(in);
- byte[] b = new byte[len];
- in.readFully(b);
- return new String(b, "UTF-8");
- }
- private int readVarInt(DataInput input) throws IOException {
- int out = 0;
- int bytes = 0;
- byte in;
- while (true) {
- in = input.readByte();
- out |= (in & 0x7F) << (bytes++ * 7);
- if (bytes > 32) {
- throw new RuntimeException("VarInt too big");
- }
- if ((in & 0x80) != 0x80) {
- break;
- }
- }
- return out;
- }
- private void writeVarInt(int value, DataOutput output) throws IOException {
- int part;
- while (true) {
- part = value & 0x7F;
- value >>>= 7;
- if (value != 0) {
- part |= 0x80;
- }
- output.writeByte(part);
- if (value == 0) {
- break;
- }
- }
- }
- public int getOnlinePlayers() {
- return this.onlinePlayers;
- }
- public int getMaxPlayers() {
- return this.maxPlayers;
- }
- public String getMotd() {
- return this.motd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement