Error while getting all online players of a bungee server

Discussion in 'Spigot Plugin Development' started by Lookups, Jul 30, 2018.

  1. I don't know if this is the right place; but i think it is.
    I'm trying to make a Hub Core, and i'm trying to get ALL the online players on a specific server,

    Getting all the online players on the bungeecord server does work, but getting all players on a specific server doesn't

    Code:

    Code (Text):
    package hub.main.bungee;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;

    public class Players {
     
        private String host = null;
        private int port = 0;
        private Socket socket = new Socket();
        private String[] data = new String[999];
     
        public Players(String host,int port){
                this.host = host;
                this.port = port;
             
                try{
                         socket.connect(new InetSocketAddress(host,port));
                        OutputStream out = socket.getOutputStream();
                        InputStream in = socket.getInputStream();
                        out.write(0xFE);
                     
                        int b;
                        StringBuffer str = new StringBuffer();
                        while((b = in.read()) != -1){
                                if(b != 0 && b > 16 && b != 255 && b != 23 && b != 24){
                                        str.append((char)b);
                                }
                        }
                     
                        data = str.toString().split("§");
                        data[0] = data[0].substring(1,data[0].length());
                }catch(IOException e){
                        e.printStackTrace();
                }
        }
     
        public String getMotd(){
                return data[0];
        }
     
        public int getOnline(){
                return Integer.parseInt(data[1]);
        }

        public int getMax(){
                return Integer.parseInt(data[2]);
        }
     
     
        public void update(){
                try{
                        socket.close();
                        socket = new Socket();
                        socket.connect(new InetSocketAddress(host,port));
                        OutputStream out = socket.getOutputStream();
                        InputStream in = socket.getInputStream();
                        out.write(0xFE);
                     
                        int b;
                        StringBuffer str = new StringBuffer();
                        while((b = in.read()) != -1){
                                if(b != 0 && b > 16 && b != 255 && b != 23 && b != 24){
                                        str.append((char)b);
                                }
                        }
                     
                        data = str.toString().split("§");
                        data[0] = data[0].substring(1,data[0].length());
                }catch(IOException e){
                        e.printStackTrace();
                }
        }
     
     
     
    }
    Using by:

    Code (Text):
    Players serverp = new Players("localhost", 25566);

    // code

            tab.left(9, "§b§lPractice §8»");
            tab.left(10, "§bStatus: §aOnline");
            tab.left(11, "§bOnline: §f" + serverp.getOnline());
    EDIT:
    Note this is Bukkit / Spigot coding, not Bungee.
     
  2. Please send us the log/error. And is this an spigot plugin or an bungeecord plugin? I asking, because:
     
  3. Yup, forgot the error, sorry about that,
    And yes, this is a SPIGOT plugin, not a bungee plugin
    Error:

    Code (Text):
    [18:55:58 WARN]: org.apache.commons.lang.UnhandledException: Plugin PornHub vB1 generated an exception while executing task 11
            at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:57)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
            at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NumberFormatException: For input string: "b"
            at java.lang.NumberFormatException.forInputString(Unknown Source)
            at java.lang.Integer.parseInt(Unknown Source)
            at java.lang.Integer.parseInt(Unknown Source)
            at hub.main.bungee.Players.getOnline(Players.java:46)
            at hub.main.TablistAdapter.getTemplate(TablistAdapter.java:67)
            at nv6.tablist.fork.XernosTask.run(XernosTask.java:36)
            at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71)
            at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
            ... 3 more
     
    getOnline 46:

    Code (Text):
                return Integer.parseInt(data[1]);
     
     
  4. Update:
    The method DOES work, but the server i'm trying to connect with isn't working;
    The server is enabled, it's connected with my bungeecord & it should work.
     
  5. You can only bump once every 24 hours. If you continue to bump your thread may get removed.
     
  6. uhh, nice plugin name.
    Anyways, fairly simple idea, no? The error is thrown by Integer.parseInt(data[1])
    Either you're counting at the wrong number (when referring to an element in a list/set/array, you start from 0, not one) or you just need to make sure that the input value actually is a number, otherwise it's only natural to get an error for trying to convert something that isn't a number into a number.
     
  7. Altough, it's weird cause my other connections do work, My connection with BungeeCord & HCF do work, altough Practice doesn't work.
    .getOnline() is a int, so shouldn't be any problem.

    My practice server IS online, my practice server DOES work with my bungeecord & the port also DOES work.
     
  8. Like I said Plugin Messaging Channel is an easier way about going about it -

    Code (Text):

    public class Plugin extends JavaPlugin implements Listener, PluginMessageListener {

        private Map<String, Integer> serverCount = new HashMap<String, Integer>();

        @Override
        public void onEnable() {
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);

            new BukkitRunnable() {

                @Override
                public void run() {
                    for (Player player : getServer().getOnlinePlayers()) {

                        if (player != null) {

                            getPlayerCount(player, "pvp"); // --> You can alternatively use "ALL" to get the entire BungeeCord player count

                        }
                    }
                }
            }.runTaskTimer(this, 0, 40); --> Updates player count every 2 seconds

        }

        public void onPluginMessageReceived(String channel, Player player, byte[] message) {

            if (!channel.equals("BungeeCord")) {
                return;
            }

            ByteArrayDataInput in = ByteStreams.newDataInput(message);

            String subchannel = in.readUTF();

            if (subchannel.equals("PlayerCount")) {

                String server = in.readUTF();

                int playerCount = in.readInt();

                if (serverCount.containsKey(server)) {
                    serverCount.remove(server);
                }

                serverCount.put(server, playerCount);
            }
        }

        public void getPlayerCount(Player player, String server) {

            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF("PlayerCount");
            out.writeUTF(server);

            player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("test")) {

                if (sender instanceof Player) {

                    Player player = (Player) sender;

                    player.sendMessage(ChatColor.GREEN + "The player count for pvp is " + serverCount.get("pvp"));

                }
            }
            return false;
        }
    }
     
    --> Hastebin of the code above: https://hastebin.com/uyifuyaxoy.java

    d8a7300b761dd7fbc55ef724d4287b37.png 0de314296982316e9750feac87001878.png
     

    Attached Files:

  9. Have you seen that i said "It does work", altough it doesn't work with that particilair host?
     
  10. Yes and I have no idea why, I just thought you'd like to see a different way about doing it (a much simpler way in my opinion) :/
     
  11. Yup, i just tried it, altough it just does nothing; also no error; sorry for responding a bit harsh last time.
     
    • Friendly Friendly x 1
  12. Mhm weird it seems to be working just fine on my end, do you have discord or anything where I can contact you within an easier way and support you further with this..?