SHARE
TWEET

Untitled

a guest Jan 20th, 2020 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.devjoao.lobby.utils;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.InetSocketAddress;
  7. import java.net.Socket;
  8. import java.net.UnknownHostException;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.entity.Player;
  12.  
  13. public class ServerUtils
  14. {
  15.     private int online = 0;
  16.     private int maximo = 0;
  17.     private boolean status = false;
  18.     private int port;
  19.     private String address;
  20.    
  21.     public ServerUtils(final String address, final int port) {
  22.         this.address = address;
  23.         this.port = port;
  24.         if (this.isOnline()) {
  25.             try {
  26.                 final Socket socket = new Socket();
  27.                 socket.connect(new InetSocketAddress(address, port), 1000);
  28.                 final DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  29.                 final DataInputStream in = new DataInputStream(socket.getInputStream());
  30.                 out.write(254);
  31.                 final StringBuilder str = new StringBuilder();
  32.                 int b;
  33.                 while ((b = in.read()) != -1) {
  34.                     if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
  35.                         str.append((char)b);
  36.                     }
  37.                 }
  38.        
  39.                 final String[] data = str.toString().split("ยง");
  40.                 final String motd = data[0];
  41.                 final int onlinePlayers = Integer.valueOf(data[1]);
  42.                 final int maxPlayers = Integer.valueOf(data[2]);
  43.                 this.online = onlinePlayers;
  44.                 this.maximo = maxPlayers;
  45.             }
  46.             catch (Exception e) {
  47.                
  48.             }
  49.         }
  50.     }
  51.    
  52.     public int getOnline() {
  53.         return this.online;
  54.     }
  55.    
  56.     public boolean isOnline() {
  57.           final int timeout = 10;
  58.           try {
  59.               final Socket s = new Socket();
  60.               s.connect(new InetSocketAddress(this.address, this.port), 10);
  61.               s.close();
  62.               return true;
  63.           }
  64.           catch (UnknownHostException e) {
  65.               return false;
  66.           }
  67.           catch (IOException ex) {
  68.               return false;
  69.           }
  70.     }
  71.    
  72.     public int getMax() {
  73.         return this.maximo;
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top