BeshoGraphix

Untitled

Jun 28th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package me.beshographix.tsigns;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.net.InetSocketAddress;
  6. import java.net.Socket;
  7.  
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.Location;
  10. import org.bukkit.block.Sign;
  11.  
  12. public class Signs {
  13.    
  14.    
  15.     private Location location;
  16.     private Sign sign;
  17.     private String name, ip;
  18.     private int port;
  19.     private static boolean online;
  20.    
  21.     public Signs (Location location, String name, String ip, int port){
  22.         this.location = location;
  23.         this.sign = (Sign) location.getBlock().getState();
  24.         this.name = name;
  25.         this.ip = ip;
  26.         this.port = port;
  27.        
  28.     }
  29.     /*
  30.      * <join>
  31.      * <name>
  32.      * <motd>
  33.      * <current/max>
  34.      */
  35.     public Location getLocation(){
  36.         return location;
  37.     }
  38.     public String getName(){
  39.         return name;
  40.     }
  41.     public String getIP(){
  42.         return ip;
  43.     }
  44.     public int getPort(){
  45.         return port;
  46.     }
  47.     public boolean isOnline(){
  48.         return online;
  49.     }
  50.    
  51.     public void update(){
  52.        
  53.         try {
  54.             Socket socket = new Socket();
  55.             socket.connect(new InetSocketAddress(ip, port), 1 * 1000);
  56.            
  57.             DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  58.             DataInputStream in = new DataInputStream(socket.getInputStream());
  59.            
  60.             out.write(0xFE);
  61.            
  62.             StringBuilder str = new StringBuilder();
  63.            
  64.             int b;
  65.             while ((b = in.read()) != -1) {
  66.                     if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
  67.                             str.append((char) b);
  68.                     }
  69.             }
  70.            
  71.             /*
  72.             <Name>
  73.             <IP>
  74.             <Current/Max>
  75.             <MOTD>
  76.              */
  77.             online.set(true);
  78.             String[] data = str.toString().split("§");
  79.             String motd = data[0];
  80.             int onlinePlayers = Integer.valueOf(data[1]);
  81.             int maxPlayers = Integer.valueOf(data[2]);
  82.        
  83.             sign.setLine(0, "§1[Join]");
  84.             sign.setLine(1, name);
  85.             sign.setLine(2, (motd).replace("&", "§"));
  86.             sign.setLine(3, "§a" +  onlinePlayers + "/" + maxPlayers);
  87.  
  88.            
  89.             socket.close();
  90.     } catch (Exception e) {
  91.            
  92.             sign.setLine(0, "§4\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588");
  93.             sign.setLine(1, "§4Restarting");
  94.             sign.setLine(2, name);
  95.             sign.setLine(3, "§4\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588");
  96.     }
  97.    
  98.     sign.update();
  99.     }
  100.    
  101.    
  102.    
  103.    
  104. }
Advertisement
Add Comment
Please, Sign In to add comment