Advertisement
thebmanswan541

HubPlugin - ServerInfo (Bukkit)

Sep 20th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package me.thebmanswan541;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.net.InetSocketAddress;
  8. import java.net.Socket;
  9. import java.nio.charset.Charset;
  10.  
  11. import org.bukkit.ChatColor;
  12.  
  13. public class ServerInfo {
  14.  
  15.     private String host;
  16.     private int port;
  17.    
  18.     public ServerInfo(String host, int port) {
  19.         this.host = host;
  20.         this.port = port;
  21.     }
  22.    
  23.     public ServerInfo(String host) {
  24.         this.host = host;
  25.         this.port = 25565;
  26.     }
  27.    
  28.     public ServerInfo() {
  29.         this.host = "0.0.0.0";
  30.         this.port = 25565;
  31.     }
  32.    
  33.     public int parseData(Connection connection) {
  34.         try{
  35.         @SuppressWarnings("resource")
  36.         Socket socket = new Socket();
  37.         OutputStream os;
  38.         DataOutputStream dos;
  39.         InputStream is;
  40.         InputStreamReader isr;
  41.        
  42.         socket.setSoTimeout(2500);
  43.         socket.connect(new InetSocketAddress(host, port), 2500);
  44.        
  45.         os = socket.getOutputStream();
  46.         dos = new DataOutputStream(os);
  47.        
  48.         is = socket.getInputStream();
  49.         isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
  50.        
  51.         dos.write(new byte[] { (byte) 0xFE, (byte) 0x01});
  52.        
  53.         int packetID = is.read();
  54.        
  55.         if (packetID == -1)
  56.             System.out.println("EOS");
  57.        
  58.         if (packetID != 0xFF)
  59.             System.out.println("Invalid Packet ID! "+packetID);
  60.        
  61.         int length = isr.read();
  62.        
  63.         if (length == -1)
  64.             System.out.println("EOS");
  65.        
  66.         if (length == 0)
  67.             System.out.println("Invalid length!");
  68.        
  69.         char[] chars = new char[length];
  70.        
  71.         if (isr.read(chars, 0, length) != length)
  72.             System.out.println("EOS");
  73.        
  74.         String string = new String(chars);
  75.         String[] data = string.split("\0");
  76.        
  77.         if (connection == Connection.PLAYERS_ONLINE) {
  78.             return Integer.parseInt(data[4]);
  79.         }
  80.         }catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.         return 0;
  84.     }
  85.    
  86.     public String parseData1(Connection connection) {
  87.         try{
  88.         @SuppressWarnings("resource")
  89.         Socket socket = new Socket();
  90.         OutputStream os;
  91.         DataOutputStream dos;
  92.         InputStream is;
  93.         InputStreamReader isr;
  94.        
  95.         socket.setSoTimeout(2500);
  96.         socket.connect(new InetSocketAddress(host, port), 2500);
  97.        
  98.         os = socket.getOutputStream();
  99.         dos = new DataOutputStream(os);
  100.        
  101.         is = socket.getInputStream();
  102.         isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
  103.        
  104.         dos.write(new byte[] { (byte) 0xFE, (byte) 0x01});
  105.        
  106.         int packetID = is.read();
  107.        
  108.         if (packetID == -1)
  109.             System.out.println("EOS");
  110.        
  111.         if (packetID != 0xFF)
  112.             System.out.println("Invalid Packet ID! "+packetID);
  113.        
  114.         int length = isr.read();
  115.        
  116.         if (length == -1)
  117.             System.out.println("EOS");
  118.        
  119.         if (length == 0)
  120.             System.out.println("Invalid length!");
  121.        
  122.         char[] chars = new char[length];
  123.        
  124.         if (isr.read(chars, 0, length) != length)
  125.             System.out.println("EOS");
  126.        
  127.         String string = new String(chars);
  128.         String[] data = string.split("\0");
  129.        
  130.         if (connection == Connection.PLAYERS_FRACTION) {
  131.             return Integer.parseInt(data[4])+"/"+Integer.parseInt(data[5]);
  132.         }
  133.        
  134.         }catch (Exception e) {
  135.             return ChatColor.RED+"0";
  136.         }
  137.         return host;
  138.     }
  139.    
  140.     public String parseData2(Connection connection) {
  141.         try{
  142.         @SuppressWarnings("resource")
  143.         Socket socket = new Socket();
  144.         OutputStream os;
  145.         DataOutputStream dos;
  146.         InputStream is;
  147.         InputStreamReader isr;
  148.        
  149.         socket.setSoTimeout(2500);
  150.         socket.connect(new InetSocketAddress(host, port), 2500);
  151.        
  152.         os = socket.getOutputStream();
  153.         dos = new DataOutputStream(os);
  154.        
  155.         is = socket.getInputStream();
  156.         isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
  157.        
  158.         dos.write(new byte[] { (byte) 0xFE, (byte) 0x01});
  159.        
  160.         int packetID = is.read();
  161.        
  162.         if (packetID == -1)
  163.             System.out.println("EOS");
  164.        
  165.         if (packetID != 0xFF)
  166.             System.out.println("Invalid Packet ID! "+packetID);
  167.        
  168.         int length = isr.read();
  169.        
  170.         if (length == -1)
  171.             System.out.println("EOS");
  172.        
  173.         if (length == 0)
  174.             System.out.println("Invalid length!");
  175.        
  176.         char[] chars = new char[length];
  177.        
  178.         if (isr.read(chars, 0, length) != length)
  179.             System.out.println("EOS");
  180.        
  181.         if (connection == Connection.ONLINE) {
  182.             return ChatColor.GREEN+"Online";
  183.         }
  184.        
  185.         }catch (Exception e) {
  186.             return ChatColor.RED+"Offline";
  187.         }
  188.         return host;
  189.     }
  190.    
  191.    
  192.    
  193.     public enum Connection {
  194.         PLAYERS_ONLINE, PLAYERS_FRACTION, ONLINE;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement