Get player count from BungeeCord server

Discussion in 'Spigot Plugin Development' started by HAMMERxPL, May 29, 2016.

  1. Hi, I found this code:
    Code (Text):
        @Override
        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();
               
                player.sendMessage("Player count on server " + server + " is equal to " + playerCount);
               
            }
           
        }
       
        public void getCount(Player player, String server) {
           
            if (server == null) {
                server = "ALL";
            }
           
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF("PlayerCount");
            out.writeUTF(server);

            player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
           
        }
    Also i have in onEnable method this:
    Code (Text):
            getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
            getCommand("players").setExecutor(this);
    and
    Code (Text):
    public class Main extends JavaPlugin implements PluginMessageListener {
    It works, but It spams the console with this error:
    Code (Text):
    [18:03:14 WARN]: Could not pass incoming plugin message to lobbyPlugin v1.0
    java.lang.IllegalStateException: java.io.EOFException
            at com.google.common.io.ByteStreams$ByteArrayDataInputStream.readUTF(ByteStreams.java:408) ~[PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at me.lobbyplugin.Main.onPluginMessageReceived(Main.java:138) ~[?:?]
            at org.bukkit.plugin.messaging.StandardMessenger.dispatchIncomingMessage(StandardMessenger.java:427) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:1983) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.PacketPlayInCustomPayload.a(PacketPlayInCustomPayload.java:39) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.PacketPlayInCustomPayload.handle(PacketPlayInCustomPayload.java:51) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:189) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:795) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:307) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:643) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:549) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [PaperSpigot.jar:git-PaperSpigot-1f7d532]
    Caused by: java.io.EOFException
            at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340) ~[?:1.8.0_91]
            at java.io.DataInputStream.readUTF(DataInputStream.java:589) ~[?:1.8.0_91]
            at java.io.DataInputStream.readUTF(DataInputStream.java:564) ~[?:1.8.0_91]
            at com.google.common.io.ByteStreams$ByteArrayDataInputStream.readUTF(ByteStreams.java:406) ~[PaperSpigot.jar:git-PaperSpigot-1f7d532]
            ... 12 more
     
    Can you guys help me with this?
     
  2. Use a try catch then for "EOFException"
     
  3. Where? This it will spam my console with this message when catched and error... Players are not even using the command to spam the console.
     
  4. True, if you are still stuck in a problem that's becuase some stuff have changed in bungee with player's count way
     
  5. its shouldn't spam your console at all, if it is then something wrong with your code
     
  6. If there is a problem with my code why don't you help me with fixing it? :)
    Where do i put this try/catch method?
     
  7. lemme show you Example:
    Code (Text):
            @Override
            public void onPluginMessageReceived(String channel, Player player, byte[] message) {
                if (!channel.equals("BungeeCord")) return;
               
                try {
                    DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
                    String command = in.readUTF();
                   
                    if (command.equals("PlayerCount")) {
                        String server = in.readUTF();
                        int playerCount = in.readInt();
                       
                        }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
     
  8. Same.
    Code (Text):

    [18:17:32 WARN]: java.lang.IllegalStateException: java.io.EOFException
    [18:17:32 WARN]:        at com.google.common.io.ByteStreams$ByteArrayDataInputStream.readUTF(ByteStreams.java:408)
    [18:17:32 WARN]:        at pl.lobbyplugin.Main.onPluginMessageReceived(Main.java:135)
    [18:17:32 WARN]:        at org.bukkit.plugin.messaging.StandardMessenger.dispatchIncomingMessage(StandardMessenger.java:427)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:1983)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.PacketPlayInCustomPayload.a(PacketPlayInCustomPayload.java:39)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.PacketPlayInCustomPayload.handle(PacketPlayInCustomPayload.java:51)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:189)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:795)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:307)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:643)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:549)
    [18:17:32 WARN]:        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628)
    [18:17:32 WARN]: Caused by: java.io.EOFException
    [18:17:32 WARN]:        at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
    [18:17:32 WARN]:        at java.io.DataInputStream.readUTF(DataInputStream.java:589)
    [18:17:32 WARN]:        at java.io.DataInputStream.readUTF(DataInputStream.java:564)
    [18:17:32 WARN]:        at com.google.common.io.ByteStreams$ByteArrayDataInputStream.readUTF(ByteStreams.java:406)
    [18:17:32 WARN]:        ... 12 more
     
  9. try changing the Exception to IOException
     
  10. https://scr.hu/JVLv5B
    The error is being thrown when HolographicDisplays is refreshing the number of players online.
     
  11. i cannot really think of other soloution... try to remove holographic display reload the server check if there Errors and if it is not printing out any error then it is HolographicDisplays
     
  12. My plugin is getting the message from HolographicDisplays. I think that is causing the error. How to fix this?
     
  13. Quote from my last Post: "i cannot really think of other soloution... try to remove holographic display reload the server check if there Errors and if it is not printing out any error then it is HolographicDisplays"
     
  14. --
    There are no errors, but it sends me messages with online players on servers that HolographicDisplays refreshes. I have couple holograms and it just sends me messages from my plugin with the player count.
     
  15. Can someone help?
     
  16. What wiki and on what should i look?
     
  17. You're problem code is on line 138. The exception is I think an end of file exception. This i think meaNS that the bytes have stopped but it's still trying to read it.
     
  18. @HAMMERxPL That code will obviously spam your console if an error occurs, do you know why?; no you don't. Because you're ******* PRINTING (e.printStackTrace()) it to the console ... seriously -.-

    (@OfirTIM Nothing wrong with your post)