Server ping

Discussion in 'Plugin Development' started by Funergy, Jul 5, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Funergy

    Hey guys,
    First of all I wanna say in this post I will not talk about the modded server thing and offline server stuff.

    So I wanna ping the server. it works but it doesn't say the motd of that server they use the serverlist ping event to set the motd.
    I use the new server ping protocol stuff for 1.7

    my code
    Code:
    package com.Funergy.ss;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.SocketException;
    import java.nio.charset.Charset;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Sign;
     
    public class StatusSign {
     
            private Location location;
            private Sign sign;
            private String name, ip,status;
           
            private int port;
            private int playercount;
            private int maxpcount;
         
            public StatusSign(Location location, String name, String ip, int port) {
                    this.location = location;
                    this.sign = (Sign) location.getBlock().getState();
                    this.name = name;
                    this.ip = ip;
                    this.port = port;
            }
         
            public Location getLocation() {
                    return location;
            }
         
            public String getName() {
                    return name;
            }
         
            public String getIP() {
                    return ip;
            }
         
            public int getPort() {
                    return port;
            }
            public int getPlayercount() {
                return playercount;
        }
            public int getMaxpcount() {
                return maxpcount;
        }
            public String getstatus() {
                return status;
        }
     
              public void update(){
                          try{
                            Socket socket = new Socket();
                            OutputStream outputStream;
                            DataOutputStream dataOutputStream;
                            InputStream inputStream;
                            InputStreamReader inputStreamReader;
                            socket.setSoTimeout(1000);
                            socket.connect(new InetSocketAddress(ip, port),              1000);
                            outputStream = socket.getOutputStream();
                            dataOutputStream = new DataOutputStream(outputStream);
                            inputStream = socket.getInputStream();
                            inputStreamReader = new InputStreamReader(inputStream,Charset.forName("UTF-16BE"));
                            dataOutputStream.write(new byte[]{(byte) 0xFE,(byte) 0x01});
                            int packetId = inputStream.read();
                            if(packetId == -1){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Premature end of stream.");
                            }
                            if(packetId != 0xFF){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Invalid packet ID (" + packetId + ").");
                            }
                            int length = inputStreamReader.read();
                            if(length == -1){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Premature end of stream.");
                            }
                            if(length == 0){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Invalid string length.");
                            }
                            char[] chars = new char[length];
                            if(inputStreamReader.read(chars,0,length) != length){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Premature end of stream.");
                            }
                            String string = new String(chars);
                            if(string.startsWith("§")){
                                String[] data = string.split("\0");
                                String motd = data[3];
                             
                                int onlinePlayers = Integer.valueOf(data[4]);
                                int maxPlayers = Integer.valueOf(data[5]);
                                this.status = motd;
                                this.playercount = onlinePlayers;
                                this.maxpcount = maxPlayers;
                                if(!status.equalsIgnoreCase("Ingame")){
                                     
                              if(onlinePlayers != maxPlayers){
                                  sign.setLine(0, "§a§l[Join]");
                              }else{
                                  sign.setLine(0, "§4§l[Full]"); 
                              }
                                }else{
                                       
                                    sign.setLine(0, "§4§l[Ingame]");
                                }
                             
                                sign.setLine(1, name);
         
                                if(status.equalsIgnoreCase("Joinable")){
                                    sign.setLine(2, "Lobby");
                                }else
                                if(status.equalsIgnoreCase("Ingame")){
                                    sign.setLine(2, "Ingame");
                                }else{
                                    sign.setLine(2, status);
                                }
                 
                                sign.setLine(3, "§5"+onlinePlayers + "/" + maxPlayers);
                            }
                            else{
                                String[] data = string.split("§");
                         
                                String motd = data[0];
                               
                                int onlinePlayers = Integer.valueOf(data[1]);
                                int maxPlayers = Integer.valueOf(data[2]);
                               
                                this.playercount = onlinePlayers;
                                this.maxpcount = maxPlayers;
                                this.status = motd;
                                if(!status.equalsIgnoreCase("Ingame")){
                                     
                              if(onlinePlayers != maxPlayers){
                                  sign.setLine(0, "§a§l[Join]");
                              }else{
                                  sign.setLine(0, "§4§l[Full]"); 
                              }
                                }else{
                                       
                                    sign.setLine(0, "§4§l[Ingame]");
                                }
                             
                                sign.setLine(1, name);
                             
                                //Motd doesn't work...
                               
                       
                             
                                if(status.equalsIgnoreCase("Joinable")){
                                    sign.setLine(2, "Lobby");
                                }
                                if(status.equalsIgnoreCase("Ingame")){
                                    sign.setLine(2, "Ingame");
                                }
                 
                                sign.setLine(3, "§5"+onlinePlayers + "/" + maxPlayers);
                               
                            }
                            dataOutputStream.close();
                            outputStream.close();
                            inputStreamReader.close();
                            inputStream.close();
                            socket.close();
                          } catch (SocketException exception) {
                              this.status = "Restarting";
                              sign.setLine(0, ChatColor.DARK_RED+"§l■■■■■■■■■■■■■");
                              sign.setLine(1, "§4[Restarting]");
                              sign.setLine(2, name);
                              sign.setLine(3, ChatColor.DARK_RED + "§l■■■■■■■■■■■■■");
                          } catch (IOException exception) {
                              this.status = "Restarting";
                              sign.setLine(0, ChatColor.DARK_RED+"§l■■■■■■■■■■■■■");
                              sign.setLine(1, "§4[Restarting]");
                              sign.setLine(2, name);
                              sign.setLine(3, ChatColor.DARK_RED + "§l■■■■■■■■■■■■■");
                          }
                          sign.update();
                      }
           
       
           
    }
     
  2. Offline

    xTigerRebornx

  3. Offline

    Funergy

    can you help me with that JSON thing?

    BTW: your one of my fav devs :D
     
  4. Offline

    xTigerRebornx

    Funergy I believe Google's GSON library is packaged with CB, you could look into using that to go from JSON to Object and vise-versa. Even if it isn't packaged, you can still use it in your project and package it yourself.
     
  5. Offline

    Funergy

    I've found something about this but Idk how to use it in my code :/
    https://gist.github.com/zh32/2b8f00ba6f7cdabef74d

    thanks.
    and sorry that I took that as sarcasm.

    if you can make a vid how to use the json and the googles gson stuff or if you give me an answer in this thread, I would really appreciate it.
    and this is some information I found but Idk how to use it in my code
    https://gist.github.com/thinkofdeath/6927216
    https://gist.github.com/zh32/2b8f00ba6f7cdabef74d

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    PogoStick29

    I've only used JSON a few times, and very limited. Once I figure out how the 1.7 protocol works, I'll make a video explaining it.
     
    Funergy likes this.
  7. Offline

    xTigerRebornx

    Funergy Create a class to store the information provided from the JSON Response, then use the methods provided by GSON to convert the JSON to that class. You can find the structure of the JSON Response on the link I provided, and there are pleanty of examples and resources provided by GSON's website to figure it out. Edit: That 2nd link provides a good way of doing it (uses some other libraries though)
     
  8. Offline

    Funergy

    Thank you.
    and grats on your 4000 subs :D I almost watch every video that you post of bukkit coding

    Okay I made this but I don't know how I actually get the information and display on the signs.
    Code:
    package com.Funergy.ss;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.SocketException;
    import java.nio.charset.Charset;
    import java.util.List;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Sign;
    import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
     
    public class StatusSign {
     
            private Location location;
            private Sign sign;
            private String name, ip,status;
           
            private int port;
            private int playercount;
            private int maxpcount;
            private final static Gson gson = new Gson();
         
            public StatusSign(Location location, String name, String ip, int port) {
                    this.location = location;
                    this.sign = (Sign) location.getBlock().getState();
                    this.name = name;
                    this.ip = ip;
                    this.port = port;
            }
         
            public Location getLocation() {
                    return location;
            }
         
            public String getName() {
                    return name;
            }
         
            public String getIP() {
                    return ip;
            }
         
            public int getPort() {
                    return port;
            }
            public int getPlayercount() {
                return playercount;
        }
            public int getMaxpcount() {
                return maxpcount;
        }
            public String getstatus() {
                return status;
        }
     
              public void update(){
                          try{
                            Socket socket = new Socket();
                            OutputStream outputStream;
                            DataOutputStream dataOutputStream;
                            InputStream inputStream;
                            InputStreamReader inputStreamReader;
                            socket.setSoTimeout(1000);
                            socket.connect(new InetSocketAddress(ip, port),              1000);
                            outputStream = socket.getOutputStream();
                            dataOutputStream = new DataOutputStream(outputStream);
                            inputStream = socket.getInputStream();
                            inputStreamReader = new InputStreamReader(inputStream,Charset.forName("UTF-16BE"));
                            dataOutputStream.write(new byte[]{(byte) 0xFE,(byte) 0x01});
                            int packetId = inputStream.read();
                            if(packetId == -1){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Premature end of stream.");
                            }
                            if(packetId != 0xFF){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Invalid packet ID (" + packetId + ").");
                            }
                            int length = inputStreamReader.read();
                            if(length == -1){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Premature end of stream.");
                            }
                            if(length == 0){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Invalid string length.");
                            }
                            char[] chars = new char[length];
                            if(inputStreamReader.read(chars,0,length) != length){
                                dataOutputStream.close();
                                outputStream.close();
                                inputStreamReader.close();
                                inputStream.close();
                                socket.close();
                                throw new IOException("Premature end of stream.");
                            }
                            String string = new String(chars);
                            response(string);
                            if(string.startsWith("§")){
                                String[] data = string.split("\0");
                                String motd = data[3];
                             
                                int onlinePlayers = Integer.valueOf(data[4]);
                                int maxPlayers = Integer.valueOf(data[5]);
                                this.status = motd;
                                this.playercount = onlinePlayers;
                                this.maxpcount = maxPlayers;
                                if(!status.equalsIgnoreCase("Ingame")){
                                     
                              if(onlinePlayers != maxPlayers){
                                  sign.setLine(0, "§a§l[Join]");
                              }else{
                                  sign.setLine(0, "§4§l[Full]"); 
                              }
                                }else{
                                       
                                    sign.setLine(0, "§4§l[Ingame]");
                                }
                             
                                sign.setLine(1, name);
         
                                if(status.equalsIgnoreCase("Joinable")){
                                    sign.setLine(2, "Lobby");
                                }else
                                if(status.equalsIgnoreCase("Ingame")){
                                    sign.setLine(2, "Ingame");
                                }else{
                                    sign.setLine(2, status);
                                }
                 
                                sign.setLine(3, "§5"+onlinePlayers + "/" + maxPlayers);
                            }
                            else{
                                String[] data = string.split("§");
                         
                                String motd = data[0];
                               
                                int onlinePlayers = Integer.valueOf(data[1]);
                                int maxPlayers = Integer.valueOf(data[2]);
                               
                                this.playercount = onlinePlayers;
                                this.maxpcount = maxPlayers;
                                this.status = motd;
                                if(!status.equalsIgnoreCase("Ingame")){
                                     
                              if(onlinePlayers != maxPlayers){
                                  sign.setLine(0, "§a§l[Join]");
                              }else{
                                  sign.setLine(0, "§4§l[Full]"); 
                              }
                                }else{
                                       
                                    sign.setLine(0, "§4§l[Ingame]");
                                }
                             
                                sign.setLine(1, name);
                             
                                //Motd doesn't work...
                               
                       
                             
                                if(status.equalsIgnoreCase("Joinable")){
                                    sign.setLine(2, "Lobby");
                                }
                                if(status.equalsIgnoreCase("Ingame")){
                                    sign.setLine(2, "Ingame");
                                }
                 
                                sign.setLine(3, "§5"+onlinePlayers + "/" + maxPlayers);
                               
                            }
                            dataOutputStream.close();
                            outputStream.close();
                            inputStreamReader.close();
                            inputStream.close();
                            socket.close();
                          } catch (SocketException exception) {
                              this.status = "Restarting";
                              sign.setLine(0, ChatColor.DARK_RED+"§l■■■■■■■■■■■■■");
                              sign.setLine(1, "§4[Restarting]");
                              sign.setLine(2, name);
                              sign.setLine(3, ChatColor.DARK_RED + "§l■■■■■■■■■■■■■");
                          } catch (IOException exception) {
                              this.status = "Restarting";
                              sign.setLine(0, ChatColor.DARK_RED+"§l■■■■■■■■■■■■■");
                              sign.setLine(1, "§4[Restarting]");
                              sign.setLine(2, name);
                              sign.setLine(3, ChatColor.DARK_RED + "§l■■■■■■■■■■■■■");
                          }
                          sign.update();
                      }
              public static StatusResponse response(String jsonString){
                  StatusResponse response = gson.fromJson(jsonString, StatusResponse.class);
                  return response;
              }
              public class StatusResponse {
                    private String description;
                    private Players players;
                    private Version version;
                    private String favicon;
                    private int time;
                   
                   
                    public class Players {
                        private int max;
                        private int online;
                        private List<Player> sample;   
                    }
                   
                    public class Player {
                        private String name;
                        private String id;
                       
                    }
                   
                    public class Version {
                        private String name;
                        private String protocol;
                    }
                }
           
       
           
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    PogoStick29 likes this.
  9. Offline

    xTigerRebornx

    Funergy Considering all you've done is just copy-pasted what that one guy did instead of actually taking the time to understand it, I wouldn't be surprised if you are confused. It seems you've just taken code from multiple tutorials and threw them together expecting to them work, and have ended up with a pile of code that doesn't work.

    I'd restart, and stop copying code from tutorials. Take the time to understand what they are doing, and then write your own code. Also, take the time to understand how to protocol for pinging works, the structure of JSON, and how you can use GSON to convert the Json provided into an Object that you can use, such as custom (de)serialization.

    Edit: For reference, the same user has written https://github.com/zh32/Pingtest/blob/master/src/main/java/de/zh32/pingtest/ServerListPing17.java that only uses google's GSON, read over that.
     
  10. Offline

    TnT

    Locked. Duplicate post.
     
Thread Status:
Not open for further replies.

Share This Page