Advertisement
Guest User

MrJuiceboy

a guest
Jun 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package fr.warfight.api.utils.bungee;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.net.ConnectException;
  8. import java.net.InetSocketAddress;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11.  
  12. import org.bukkit.entity.Player;
  13.  
  14. import com.google.common.io.ByteArrayDataOutput;
  15. import com.google.common.io.ByteStreams;
  16.  
  17. import fr.warfight.api.API;
  18.  
  19. /**
  20.  * @author MrJuiceboy
  21.  */
  22.  
  23. public class BungeeUtils
  24. {
  25.     private static API pl;
  26.     public BungeeUtils(final API pl)
  27.     {
  28.         BungeeUtils.pl = pl;
  29.         pl.getServer().getMessenger().registerOutgoingPluginChannel(pl, "BungeeCord");
  30.     }
  31.    
  32.     public static void sendPlayer(Player p, String server)
  33.     {
  34.         ByteArrayOutputStream b = new ByteArrayOutputStream();
  35.         DataOutputStream out = new DataOutputStream(b);  
  36.         try
  37.         {    
  38.             out.writeUTF("Connect");
  39.             out.writeUTF(server);
  40.         }catch(IOException eee){}
  41.         p.sendPluginMessage(pl, "BungeeCord", b.toByteArray());
  42.     }
  43.    
  44.     public static Object getCountPlayerBungeeServer(String server) {
  45.          if (server == null) {
  46.                 server = "ALL";
  47.             }
  48.          
  49.             ByteArrayDataOutput out = ByteStreams.newDataOutput();
  50.                 out.writeUTF("PlayerCount");
  51.                 out.writeUTF(server);
  52.                
  53.              return server;            
  54.         }
  55.    
  56.     public static Object getCountPlayerServer(String ip, int port) {
  57.          String returnString = null;
  58.             try {
  59.                 Socket socket = new Socket();
  60.                 socket.setSoTimeout(100);
  61.                 socket.connect(new InetSocketAddress(ip, port), 1 * 1000);
  62.                  
  63.                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  64.                 DataInputStream in = new DataInputStream(socket.getInputStream());
  65.                  
  66.                 out.write(0xFE);
  67.                
  68.                 StringBuilder str = new StringBuilder();
  69.                  
  70.                 int b;         
  71.                 while ((b = in.read()) != -1){
  72.                     if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24){                   
  73.                         str.append((char) b);          
  74.                     }
  75.                 }
  76.                
  77.                 socket.close();
  78.                  
  79.                 String[] data = str.toString().split("§");
  80.                 return Integer.parseInt(data[1]);
  81.                
  82.             } catch (ConnectException e) {
  83.                 returnString = "§4N/A§6";
  84.             } catch (UnknownHostException e) {
  85.                 returnString = "§4N/A§6";
  86.             } catch (IOException e) {
  87.                 returnString = "§4N/A§6";
  88.             }
  89.              return returnString;          
  90.         }  
  91.    
  92.     public static String getMotdServer(String ip, int port) {    
  93.          String returnString = null;
  94.             try {
  95.                 Socket socket = new Socket();
  96.                 socket.setSoTimeout(100);
  97.                 socket.connect(new InetSocketAddress(ip, port), 1 * 1000);
  98.                  
  99.                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  100.                 DataInputStream in = new DataInputStream(socket.getInputStream());
  101.                  
  102.                 out.write(0xFE);
  103.                  
  104.                 int b;
  105.                 StringBuffer str = new StringBuffer();
  106.                 while ((b = in .read()) != -1) {
  107.                     if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
  108.                         str.append((char) b);
  109.                     }
  110.                 }
  111.  
  112.                 String[] data = str.toString().split("§");
  113.                 String serverMotd = data[0];               
  114.  
  115.                 returnString = String.format(serverMotd);
  116.                
  117.                 socket.close();
  118.  
  119.             } catch (ConnectException e) {
  120.                 returnString = "§c§lHors Ligne";
  121.             } catch (UnknownHostException e) {
  122.                 returnString = "§c§lHors Ligne";
  123.             } catch (IOException e) {
  124.                 returnString = "§c§lHors Ligne";
  125.             }
  126.             return returnString;      
  127.         }  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement