How to Check OnlineMode!!

Discussion in 'Resources' started by Creepapa, Apr 6, 2014.

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

    Creepapa

    Do you know check online-mode?
    I made this for ServerList!!
    If you make something out of this please comment it!!

    Code:java
    1. public static String checkOnlinemode(String adress, int port, int protocol){
    2. if(protocol == 4){
    3. try {
    4. System.out.println("Adress: "+adress);
    5. System.out.println("Port: "+port);
    6. System.out.println("Protocol: "+protocol);
    7. System.out.println("Now Connecting...");
    8.  
    9. //Socket
    10. Socket socket = new Socket();
    11. socket.connect(new InetSocketAddress(adress, port), 200);
    12. DataInputStream dis = new DataInputStream(socket.getInputStream());
    13. DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    14.  
    15. DataOutputStream handshake = new DataOutputStream(b);
    16. handshake.writeByte(0x00);
    17. writeVarInt(handshake, 4);
    18. writeVarInt(handshake, adress.length());
    19. handshake.writeBytes(adress);
    20. handshake.writeShort(port);
    21. writeVarInt(handshake, 2);
    22. writeVarInt(dos, b.size());
    23. dos.write(b.toByteArray());
    24.  
    25. login.writeByte(0x00);
    26. writeVarInt(login, "Bot".length());
    27. login.writeBytes("Bot");
    28. writeVarInt(dos, b2.size());
    29. dos.write(b2.toByteArray());
    30.  
    31. int size = readVarInt(dis);
    32. int id = readVarInt(dis);
    33.  
    34. if(id == 0x01){
    35. socket.close();
    36. return "OnlineMode true!!";
    37. }else{
    38. socket.close();
    39. return "OnlineMode false!!";
    40. }
    41. }catch(Exception e){
    42. e.printStackTrace();
    43. }
    44. }else{
    45. try {
    46. System.out.println("Adress: "+adress);
    47. System.out.println("Port: "+port);
    48. System.out.println("Protocol: "+protocol);
    49. Socket socket = new Socket();
    50. System.out.println("Now Connecting...");
    51. socket.connect(new InetSocketAddress(adress, port), 200);
    52. DataInputStream dis = new DataInputStream(socket.getInputStream());
    53. DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    54. dos.writeByte(0x02);
    55. dos.writeByte(protocol);
    56. writeString("Bot", dos);
    57. writeString(adress, dos);
    58. dos.writeInt(port);
    59. int hash = dis.read();
    60. if(hash == 0xFF){
    61. socket.close();
    62. return "Disconnect Server!";
    63. }
    64. short word = dis.readShort();
    65. StringBuilder stringbuilder = new StringBuilder();
    66. for (int i = 0; i < word; i++) {
    67. char sdf = dis.readChar();
    68. stringbuilder.append(sdf);
    69. }
    70. if(stringbuilder.toString().equalsIgnoreCase("-")){
    71. socket.close();
    72. return "OnlineMode false!!";
    73. }else{
    74. socket.close();
    75. return "OnlineMode true!!";
    76. }
    77. }catch (Exception e){
    78. e.printStackTrace();
    79. }
    80. }
    81. return null;
    82. }
    83.  
    84. public static void writeString(String str, DataOutputStream dataoutputstream){
    85. try {
    86. dataoutputstream.writeShort(str.length());
    87. dataoutputstream.writeChars(str);
    88. } catch (IOException e){
    89. e.printStackTrace();
    90. }
    91. }
    92.  
    93. public static int readVarInt(DataInputStream in) throws IOException {
    94. int i = 0;
    95. int j = 0;
    96. while (true) {
    97. int k = in.readByte();
    98. i |= (k & 0x7F) << j++ * 7;
    99. if (j > 5) throw new RuntimeException("VarInt too big");
    100. if ((k & 0x80) != 128) break;
    101. }
    102. return i;
    103. }
    104.  
    105. public static void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
    106. while (true) {
    107. if ((paramInt & 0xFFFFFF80) == 0) {
    108. out.writeByte(paramInt);
    109. return;
    110. }
    111. out.writeByte(paramInt & 0x7F | 0x80);
    112. paramInt >>>= 7;
    113. }
    114. }
     
  2. Offline

    L33m4n123

    You know there's a method?

    Code:
    Bukkit.getServer().getOnlineMode();
    so 117 line of codes in 1 line of code and 5 seconds looking at the JavaDoc of Bukkit

    Edit: nvm. After looking over it again I realise this is to check if OTHER servers are onlinemode true or not. my bad
     
  3. Offline

    Creepapa

    I know.
    I made this for ServerList!!
     
  4. Offline

    Garris0n

    But...but who would write a server list in Java?
     
  5. Offline

    RawCode

    java is OK, not as ok as c# or c++ but still OK.

    this have very limited use, also there is project about complete protocol ripoff available in opensource and can be used for much more advanced actions like logging to minecraft server without client.
     
  6. Offline

    Plo124

    I think this has 2 purposes:
    1. If you wanted to poll another server altogether, e.g. BungeeCord requires all servers connecting to to have online-mode false
    2. If you wanted to make a Java application in which users can check if a server is online or offline.

    Creepapa this would be nice if you made it get the MOTD too.
    Also, make it return a BOOLEAN, not a STRING
     
Thread Status:
Not open for further replies.

Share This Page