Do you know check online-mode? I made this for ServerList!! If you make something out of this please comment it!! Code:java public static String checkOnlinemode(String adress, int port, int protocol){ if(protocol == 4){ try { System.out.println("Adress: "+adress); System.out.println("Port: "+port); System.out.println("Protocol: "+protocol); System.out.println("Now Connecting..."); //Socket Socket socket = new Socket(); socket.connect(new InetSocketAddress(adress, port), 200); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream handshake = new DataOutputStream(b); handshake.writeByte(0x00); writeVarInt(handshake, 4); writeVarInt(handshake, adress.length()); handshake.writeBytes(adress); handshake.writeShort(port); writeVarInt(handshake, 2); writeVarInt(dos, b.size()); dos.write(b.toByteArray()); ByteArrayOutputStream b2 = new ByteArrayOutputStream(); DataOutputStream login = new DataOutputStream(b2); login.writeByte(0x00); writeVarInt(login, "Bot".length()); login.writeBytes("Bot"); writeVarInt(dos, b2.size()); dos.write(b2.toByteArray()); int size = readVarInt(dis); int id = readVarInt(dis); if(id == 0x01){ socket.close(); return "OnlineMode true!!"; }else{ socket.close(); return "OnlineMode false!!"; } }catch(Exception e){ e.printStackTrace(); } }else{ try { System.out.println("Adress: "+adress); System.out.println("Port: "+port); System.out.println("Protocol: "+protocol); Socket socket = new Socket(); System.out.println("Now Connecting..."); socket.connect(new InetSocketAddress(adress, port), 200); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeByte(0x02); dos.writeByte(protocol); writeString("Bot", dos); writeString(adress, dos); dos.writeInt(port); int hash = dis.read(); if(hash == 0xFF){ socket.close(); return "Disconnect Server!"; } short word = dis.readShort(); StringBuilder stringbuilder = new StringBuilder(); for (int i = 0; i < word; i++) { char sdf = dis.readChar(); stringbuilder.append(sdf); } if(stringbuilder.toString().equalsIgnoreCase("-")){ socket.close(); return "OnlineMode false!!"; }else{ socket.close(); return "OnlineMode true!!"; } }catch (Exception e){ e.printStackTrace(); } } return null; } public static void writeString(String str, DataOutputStream dataoutputstream){ try { dataoutputstream.writeShort(str.length()); dataoutputstream.writeChars(str); } catch (IOException e){ e.printStackTrace(); } } public static int readVarInt(DataInputStream in) throws IOException { int i = 0; int j = 0; while (true) { int k = in.readByte(); i |= (k & 0x7F) << j++ * 7; if (j > 5) throw new RuntimeException("VarInt too big"); if ((k & 0x80) != 128) break; } return i; } public static void writeVarInt(DataOutputStream out, int paramInt) throws IOException { while (true) { if ((paramInt & 0xFFFFFF80) == 0) { out.writeByte(paramInt); return; } out.writeByte(paramInt & 0x7F | 0x80); paramInt >>>= 7; } }
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
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.
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