Socket problem

Discussion in 'Spigot Plugin Help' started by jarnoboy404, Jun 24, 2018.

  1. Hello,

    i am making a plugin where i get the server information of a ip.
    But when i run my code it give me the motd when i try to get the online players of the socket server.
    Maybe you don't understand what i mean. So i let you show my code.


    This is my ServerGetter.json object for getting server information.

    Code (Java):
    package me.jarnoboy404.ItsLarsWorld.Menus.Objects;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.SocketTimeoutException;

    public class ServerGetter {
        String host;
        Integer port;
       
        Socket socket;
        DataOutputStream out;
        DataInputStream in;
       
        public ServerGetter(String host, Integer port) {
            this.host = host;
            this.port = port;
        }
        public Boolean connect() {
            Boolean output = false;
            if(socket == null) {
                try {
                    socket = new Socket();
                    socket.connect(new InetSocketAddress(host, port), 1 * 1000);
                   
                    out = new DataOutputStream(socket.getOutputStream());
                    in = new DataInputStream(socket.getInputStream());
                   
                    out.write(0xFE);
                   
                    output = true;
                   
                } catch(Exception e) {
                    output = false;
                }
            }else {
                output = false;
            }
            return output;
        }
        public Boolean disconnect() {
            Boolean output = false;
            try {
               
                if((!socket.isClosed()) && socket.isConnected()) {
                    socket.close();
                    in.close();
                    out.close();
                   
                    output = true;
               
                }else {
                    output = false;
                }
               
            } catch(Exception e) {
                output = false;
            }
            return output;
        }
        public String getOnlinePlayers() {
            try {
               
                StringBuilder str = new StringBuilder();
               
                int b;
               
                while ((b = in.read()) != -1) {
                    if(b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
                        str.append((char) b);
                    }
                }
                String[] data = str.toString().split("§");
                String onlinePlayers = data[1];
               
                socket.close();
                out.close();
                in.close();
                return onlinePlayers;
            } catch(SocketTimeoutException e) {
                return "Offline";
            } catch(Exception e) {
                e.printStackTrace();
            }
            return "Offline";
        }
       
        public String getMaxPlayers() {
            try {
               
                StringBuilder str = new StringBuilder();
               
                int b;
               
                while ((b = in.read()) != -1) {
                    if(b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
                        str.append((char) b);
                    }
                }
                String[] data = str.toString().split("§");
                String maxPlayers = data[2];
           
                return maxPlayers;
            } catch(SocketTimeoutException e) {
                return "Offline";
            } catch(Exception e) {
                e.printStackTrace();
            }
            return "Offline";
        }

        public String getMotd() {
            try {
               
                StringBuilder str = new StringBuilder();
               
                int b;
               
                while ((b = in.read()) != -1) {
                    if(b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
                        str.append((char) b);
                    }
                }
                String[] data = str.toString().split("§");
                String motd = data[0];
               
                return motd;
            } catch(SocketTimeoutException e) {
                return "Offline";
            } catch(Exception e) {
                e.printStackTrace();
            }
            return "Offline";
        }

    }
     

    And here i my code when i try to get the onlinePlayers.

    Code (Java):
    package me.jarnoboy404.ItsLarsWorld.Menus;

    import org.bukkit.plugin.java.JavaPlugin;

    import me.jarnoboy404.ItsLarsWorld.Menus.Objects.ServerGetter;

    public class Main extends JavaPlugin {
       
        public void onEnable() {
            ServerGetter s = new ServerGetter("localhost", 25565);
            if(s.connect()) {
                System.out.println("Connected");
            }
            System.out.println("OnlinePlayers: " + s.getOnlinePlayers());
            System.out.println("MaxPlayers: " + s.getMaxPlayers());
            System.out.println("Motd: " + s.getMotd());
        }
    }
     

    And here is my console:

    Code (Text):
    OnlinePlayers: &6PluginTest Server
    [18:03:24 WARN]: java.lang.ArrayIndexOutOfBoundsException: 2
    [18:03:24 WARN]:        at me.jarnoboy404.ItsLarsWorld.Menus.Objects.ServerGette
    r.getMaxPlayers(ServerGetter.java:103)
    [18:03:24 WARN]:        at me.jarnoboy404.ItsLarsWorld.Menus.Main.onEnable(Main.
    java:65)
    [18:03:24 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:264)
    [18:03:24 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:337)
    [18:03:24 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:403)
    [18:03:24 WARN]:        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlu
    gin(CraftServer.java:382)
    [18:03:24 WARN]:        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlu
    gins(CraftServer.java:331)
    [18:03:24 WARN]:        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(Cr
    aftServer.java:753)
    [18:03:24 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:525)
    [18:03:24 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
    oadCommand.java:27)
    [18:03:24 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:141)
    [18:03:24 WARN]:        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchC
    ommand(CraftServer.java:649)
    [18:03:24 WARN]:        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchS
    erverCommand(CraftServer.java:635)
    [18:03:24 WARN]:        at net.minecraft.server.v1_12_R1.DedicatedServer.aP(Dedi
    catedServer.java:444)
    [18:03:24 WARN]:        at net.minecraft.server.v1_12_R1.DedicatedServer.D(Dedic
    atedServer.java:407)
    [18:03:24 WARN]:        at net.minecraft.server.v1_12_R1.MinecraftServer.C(Minec
    raftServer.java:679)
    [18:03:24 WARN]:        at net.minecraft.server.v1_12_R1.MinecraftServer.run(Min
    ecraftServer.java:577)
    [18:03:24 WARN]:        at java.lang.Thread.run(Unknown Source)
    MaxPlayers: Offline
    Motd:
     

    As you see it gives me the motd by the online players and a error. And as you can see by MaxPlayers is offline and by Motd nothing.

    So idk what to do? Can please anyone help me?