Solved How do I get how many people are on another server?

Discussion in 'Plugin Development' started by ledship, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ledship

    Hi, Im working on a bukkit plugin for my network and I need to see if another server is online or offline and if it is online then how many people are on it, so how would I go about doing this?
     
  2. Offline

    xJeremyCx

    Send a ping request to that server just like Minecraft does
     
  3. Offline

    ledship

    Example of code?
     
  4. Offline

    xJeremyCx

    Here is it
    Code:java
    1. import java.io.ByteArrayOutputStream;
    2. import java.io.DataInputStream;
    3. import java.io.DataOutputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.io.InputStreamReader;
    7. import java.io.OutputStream;
    8. import java.net.InetSocketAddress;
    9. import java.net.Socket;
    10. import java.util.List;
    11.  
    12. import net.minecraft.util.com.google.gson.Gson;
    13.  
    14. public class ServerInfo
    15. {
    16. private static Gson gson = new Gson();
    17.  
    18. public static int readVarInt(DataInputStream in) throws IOException
    19. {
    20. int i = 0;
    21. int j = 0;
    22. while (true)
    23. {
    24. int k = in.readByte();
    25. i |= (k & 0x7F) << j++ * 7;
    26. if (j > 5) throw new RuntimeException("VarInt too big");
    27. if ((k & 0x80) != 128) break;
    28. }
    29. return i;
    30. }
    31.  
    32. public static void writeVarInt(DataOutputStream out, int paramInt) throws IOException
    33. {
    34. while (true)
    35. {
    36. if ((paramInt & 0xFFFFFF80) == 0)
    37. {
    38. out.writeByte(paramInt);
    39. return;
    40. }
    41.  
    42. out.writeByte(paramInt & 0x7F | 0x80);
    43. paramInt >>>= 7;
    44. }
    45. }
    46.  
    47. public static StatusResponse getPing(String ip, int port)
    48. {
    49. try
    50. {
    51. Socket socket = new Socket();
    52. OutputStream outputStream;
    53. DataOutputStream dataOutputStream;
    54. InputStream inputStream;
    55. InputStreamReader inputStreamReader;
    56.  
    57. socket.setSoTimeout(7000);
    58. socket.connect(new InetSocketAddress(ip, port), 7000);
    59.  
    60.  
    61. outputStream = socket.getOutputStream();
    62. dataOutputStream = new DataOutputStream(outputStream);
    63.  
    64. inputStream = socket.getInputStream();
    65. inputStreamReader = new InputStreamReader(inputStream);
    66.  
    67. DataOutputStream handshake = new DataOutputStream(b);
    68. handshake.writeByte(0x00); //packet id for handshake
    69. writeVarInt(handshake, 4); //protocol version
    70. writeVarInt(handshake, ip.length()); //host length
    71. handshake.writeBytes(ip); //host string
    72. handshake.writeShort(port); //port
    73. writeVarInt(handshake, 1); //state (1 for handshake)
    74.  
    75. writeVarInt(dataOutputStream, b.size()); //prepend size
    76. dataOutputStream.write(b.toByteArray()); //write handshake packet
    77.  
    78.  
    79. dataOutputStream.writeByte(0x01); //size is only 1
    80. dataOutputStream.writeByte(0x00); //packet id for ping
    81. DataInputStream dataInputStream = new DataInputStream(inputStream);
    82. int size = readVarInt(dataInputStream); //size of packet
    83. int id = readVarInt(dataInputStream); //packet id
    84.  
    85. if (id == -1) {
    86. dataOutputStream.close();
    87. outputStream.close();
    88. inputStreamReader.close();
    89. inputStream.close();
    90. socket.close();
    91. return null;
    92. }
    93.  
    94. if (id != 0x00) { //we want a status response
    95. dataOutputStream.close();
    96. outputStream.close();
    97. inputStreamReader.close();
    98. inputStream.close();
    99. socket.close();
    100. return null;
    101. }
    102. int length = readVarInt(dataInputStream); //length of json string
    103.  
    104. if (length == -1) {
    105. dataOutputStream.close();
    106. outputStream.close();
    107. inputStreamReader.close();
    108. inputStream.close();
    109. socket.close();
    110. return null;
    111. }
    112.  
    113. if (length == 0) {
    114. dataOutputStream.close();
    115. outputStream.close();
    116. inputStreamReader.close();
    117. inputStream.close();
    118. socket.close();
    119. return null;
    120. }
    121.  
    122. byte[] in = new byte[length];
    123. dataInputStream.readFully(in); //read json string
    124. String json = new String(in);
    125.  
    126.  
    127. long now = System.currentTimeMillis();
    128. dataOutputStream.writeByte(0x09); //size of packet
    129. dataOutputStream.writeByte(0x01); //0x01 for ping
    130. dataOutputStream.writeLong(now); //time!?
    131.  
    132. readVarInt(dataInputStream);
    133. id = readVarInt(dataInputStream);
    134. if (id == -1) {
    135. dataOutputStream.close();
    136. outputStream.close();
    137. inputStreamReader.close();
    138. inputStream.close();
    139. socket.close();
    140. return null;
    141. }
    142.  
    143. if (id != 0x01) {
    144. dataOutputStream.close();
    145. outputStream.close();
    146. inputStreamReader.close();
    147. inputStream.close();
    148. socket.close();
    149. return null;
    150. }
    151. long pingtime = dataInputStream.readLong(); //read response
    152.  
    153. StatusResponse response = gson.fromJson(json, StatusResponse.class);
    154. response.setTime((int) (now - pingtime));
    155.  
    156. dataOutputStream.close();
    157. outputStream.close();
    158. inputStreamReader.close();
    159. inputStream.close();
    160. socket.close();
    161.  
    162. return response;
    163. }
    164. catch(IOException e)
    165. {
    166. return null;
    167. }
    168. }
    169.  
    170. public class StatusResponse {
    171. private String description;
    172. private Players players;
    173. private Version version;
    174. private String favicon;
    175. private int time;
    176.  
    177. public String getDescription() {
    178. return description;
    179. }
    180.  
    181. public Players getPlayers() {
    182. return players;
    183. }
    184.  
    185. public Version getVersion() {
    186. return version;
    187. }
    188.  
    189. public String getFavicon() {
    190. return favicon;
    191. }
    192.  
    193. public int getTime() {
    194. return time;
    195. }
    196.  
    197. public void setTime(int time) {
    198. this.time = time;
    199. }
    200.  
    201. }
    202.  
    203. public class Players {
    204. private int max;
    205. private int online;
    206. private List<Player> sample;
    207.  
    208. public int getMax() {
    209. return max;
    210. }
    211.  
    212. public int getOnline() {
    213. return online;
    214. }
    215.  
    216. public List<Player> getSample() {
    217. return sample;
    218. }
    219. }
    220.  
    221. public class Player {
    222. private String name;
    223. private String id;
    224.  
    225. public String getName() {
    226. return name;
    227. }
    228.  
    229. public String getId() {
    230. return id;
    231. }
    232.  
    233. }
    234.  
    235. public class Version {
    236. private String name;
    237. private String protocol;
    238.  
    239. public String getName() {
    240. return name;
    241. }
    242.  
    243. public String getProtocol() {
    244. return protocol;
    245. }
    246. }
    247. }

    So you just need to to
    Code:java
    1. ServerInfo.StatusResponse sp = ServerInfo.getPing("serverip", port);
     
    rfsantos1996 likes this.
  5. Offline

    ledship


    Thanks!!!!!!
     
  6. Offline

    IdiotBoxGuyMC

    xJeremyCx
    Lol you basically feeded him code xD

    Also ledship
    Mark it as solved so that people in the future find it easily :)
     
  7. Offline

    Plo124

    xJeremyCx
    This code looks good tk query a server. Mind if I use it in a plugin or 2 of mine?
     
  8. Offline

    xJeremyCx

    Sorry for late reply, of course you can
     
Thread Status:
Not open for further replies.

Share This Page