Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. package me.puriyaspielt.razepvp.CoreUtils;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
  5. import org.bukkit.craftbukkit.libs.com.google.gson.JsonParser;
  6.  
  7. import java.io.*;
  8. import java.net.ConnectException;
  9. import java.net.InetSocketAddress;
  10. import java.net.Socket;
  11. import java.util.logging.Level;
  12.  
  13. public final class PingServer{
  14. private InetSocketAddress address;
  15. private int timeout = 1000;
  16.  
  17. private int pingVersion = -1;
  18. private int protocolVersion = -1;
  19. private String gameVersion;
  20. private String motd = "-1";
  21. private int playersOnline = -1;
  22. private int maxPlayers = -1;
  23. private boolean isonline = false;
  24.  
  25. public PingServer(String ip,int port){
  26. InetSocketAddress inet = new InetSocketAddress(ip, port);
  27. this.setAddress(inet);
  28. this.fetchData();
  29. }
  30.  
  31.  
  32. public boolean fetchData() {
  33. Socket socket = new Socket();
  34. try {
  35.  
  36. socket.setSoTimeout(this.timeout);
  37.  
  38. socket.connect(this.address, getTimeout());
  39.  
  40. try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) {
  41. try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
  42. try (ByteArrayOutputStream frame = new ByteArrayOutputStream()) {
  43. try (DataOutputStream frameOut = new DataOutputStream(frame)) {
  44.  
  45. // Handshake
  46. writeVarInt(0x00, frameOut);
  47. writeVarInt(4, frameOut);
  48. writeString(this.address.getHostString(), frameOut);
  49. frameOut.writeShort(this.address.getPort());
  50. writeVarInt(1, frameOut);
  51. // Handshake
  52. writeVarInt(frame.size(), out);
  53. frame.writeTo(out);
  54. frame.reset();
  55.  
  56. // Ping
  57. writeVarInt(0x00, frameOut);
  58. // Ping-Senden
  59. writeVarInt(frame.size(), out);
  60. frame.writeTo(out);
  61. frame.reset();
  62.  
  63. int len = readVarInt(in);
  64. byte[] packet = new byte[len];
  65. in.readFully(packet);
  66.  
  67. try (ByteArrayInputStream inPacket = new ByteArrayInputStream(packet)) {
  68. try (DataInputStream inFrame = new DataInputStream(inPacket)) {
  69. int id = readVarInt(inFrame);
  70. if (id != 0x00) {
  71. Bukkit.getLogger().log(Level.SEVERE, "Error[0] fetching data from server " + this.address.toString());
  72. this.isonline = false;
  73. return false;
  74. }
  75.  
  76. JsonParser parser = new JsonParser();
  77. String json = readString(inFrame);
  78. JsonObject jsonObject = parser.parse(json).getAsJsonObject();
  79. JsonObject jsonPlayers = jsonObject.get("players").getAsJsonObject();
  80.  
  81. this.playersOnline = Integer.parseInt(jsonPlayers.get("online").toString());
  82. this.maxPlayers = Integer.parseInt(jsonPlayers.get("max").toString());
  83. this.motd = jsonObject.get("description").toString().replaceAll("\"", "");
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90.  
  91.  
  92. } catch (Exception exception) {
  93. if (!(exception instanceof ConnectException))
  94. Bukkit.getLogger().log(Level.SEVERE, "Error[1] fetching data from server " + this.address.toString());
  95. this.isonline = false;
  96. return false;
  97. } finally {
  98. try {
  99. socket.close();
  100. } catch (IOException exception) {
  101. exception.printStackTrace();
  102. }
  103. }
  104. this.isonline = true;
  105. return true;
  106.  
  107. }
  108.  
  109. public boolean isFull() {
  110. return getPlayersOnline()>=getMaxPlayers();
  111. }
  112.  
  113. public boolean isOnline() {
  114. return this.isonline;
  115. }
  116.  
  117. public static void writeString(String s, DataOutput out) throws IOException {
  118. byte[] b = s.getBytes("UTF-8");
  119. writeVarInt(b.length, out);
  120. out.write(b);
  121. }
  122.  
  123. public static String readString(DataInput in) throws IOException {
  124. int len = readVarInt(in);
  125. byte[] b = new byte[len];
  126. in.readFully(b);
  127.  
  128. return new String(b, "UTF-8");
  129. }
  130.  
  131. public static int readVarInt(DataInput input) throws IOException {
  132. int out = 0;
  133. int bytes = 0;
  134. byte in;
  135. while (true) {
  136. in = input.readByte();
  137.  
  138. out |= (in & 0x7F) << (bytes++ * 7);
  139.  
  140. if (bytes > 32) {
  141. throw new RuntimeException("VarInt too big");
  142. }
  143.  
  144. if ((in & 0x80) != 0x80) {
  145. break;
  146. }
  147. }
  148.  
  149. return out;
  150. }
  151.  
  152. public static void writeVarInt(int value, DataOutput output) throws IOException {
  153. int part;
  154. while (true) {
  155. part = value & 0x7F;
  156.  
  157. value >>>= 7;
  158. if (value != 0) {
  159. part |= 0x80;
  160. }
  161.  
  162. output.writeByte(part);
  163.  
  164. if (value == 0) {
  165. break;
  166. }
  167. }
  168. }
  169.  
  170. public InetSocketAddress getAddress()
  171. {
  172. return this.address;
  173. }
  174.  
  175. public int getTimeout()
  176. {
  177. return this.timeout;
  178. }
  179.  
  180. public int getPingVersion()
  181. {
  182. return this.pingVersion;
  183. }
  184.  
  185. public int getProtocolVersion()
  186. {
  187. return this.protocolVersion;
  188. }
  189.  
  190. public String getGameVersion()
  191. {
  192. return this.gameVersion;
  193. }
  194.  
  195. public String getMotd()
  196. {
  197. return this.motd;
  198. }
  199.  
  200. public int getPlayersOnline()
  201. {
  202. return this.playersOnline;
  203. }
  204.  
  205. public int getMaxPlayers()
  206. {
  207. return this.maxPlayers;
  208. }
  209.  
  210. public void setAddress(InetSocketAddress address) {
  211. this.address = address;
  212. }
  213.  
  214. public void setTimeout(int timeout)
  215. {
  216. this.timeout = timeout;
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement