Solved Getting players from another bungeecord server

Discussion in 'Spigot Plugin Development' started by Roddern, Apr 4, 2020.

  1. This is my problem, I'm trying to get the online players from another bungeecord server and this is my function:
    Code (Java):
        public static int getPlayercount(Player p, String server){
            final ByteArrayDataOutput out = ByteStreams.newDataOutput();
            ByteArrayDataInput in = ByteStreams.newDataInput(server.getBytes());
            out.writeUTF("PlayerCount");
            out.writeUTF(server);
            int playercount = in.readInt();
            p.sendPluginMessage(getInstance(), "BungeeCord", out.toByteArray());
            return playercount;
        }
    I've tried to search in different docs, tried different methods and basically just everything but I'm really lost, it returns a value like "128536396". I would appreciate any help, thank you!
     
  2. PlayerCount
    Get the amount of players on a certain server, or on ALL the servers.

    Arguments
    String the name of the server to get the player count of, or ALL to get the global player count

    Receiver
    Any player

    Example

    Code (Java):
    out.writeUTF("PlayerCount");
    out.writeUTF("pvp");
    Response

    Code (Java):
    String server = in.readUTF(); // Name of server, as given in the arguments
    int playercount = in.readInt();
    taken from https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#playercount
     
  3. Yeah I saw that one, tried to use it but didn't work out :/
     
  4. are you registering the plugin's message channel?
     
  5. I don't think I am, I'm really new to bungeecord messaging channels btw^
    This is the only bungeecord registering I have, it's inside onEnable()
    Code (Java):
    getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
     
  6. still need help^
     
  7. Ok, I've read through and came up with this function:
    Code (Text):
       
    public static void getPlayercount(Player p, String server){
        final ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF("PlayerCount");
        out.writeUTF(server);
        p.sendPluginMessage(getInstance(), "BungeeCord", out.toByteArray());
    }
     
    How may I display the amount in chat now tho?
    Sorry for being a noob at this btw, really appreciate the help.
     
  8. Code (Text):
    serverCount.get("ServerName");
    this will return an integer, this should help you out
     
  9. May I ask where did you get the "serverCount" from? I haven't made a variable called serverCount yet.
     
  10. Create a Hashmap, store the servername, and playercount.

    store the servername, and playercount to the Map in here.

    then you can do
    Code (Text):
    player.sendMesasage("Example has " + serverCount.get("ServerName")  + " online players!")
     
  11. I'm doing this
    Code (Java):
               
      Bukkit.broadcastMessage(PlayerList.serverCount.get(server) + "");
      PlayerList.serverCount.put(server, in.readInt());
    Bukkit.broadcastMessage(PlayerList.serverCount.get(server) + "");
    and this is what I have in my command class:
    Code (Text):
     PlayerList.getPlayercount("MiniSG-1");
    and this is the results:
    [​IMG]
    (It should have been 2 broadcasts, 1 that says 0 and another that says 2 because my friends was online, but only 0 is coming)

    This is happening because in my getplayercount class, I'm setting the integer in the hashmap to 0.
    Code (Java):
      serverCount.put(server, 0);
    And at this one I'm trying to put the in.readInt()); but then it won't broadcast the second one, it just broadcasts the first one, after I've "changed" the integer.
    Code (Java):
             
      Bukkit.broadcastMessage(PlayerList.serverCount.get(server) + "");
      PlayerList.serverCount.put(server, in.readInt());
    Bukkit.broadcastMessage(PlayerList.serverCount.get(server) + "");
    That code is in the onPluginMessageReceived function.

    It's a IllegalStateException error.
     
  12. Yes, of course it would be, you need to get it like this:
    Code (Text):
    if (subchannel.equals("PlayerCount")) {
                String server = in.readUTF();
                int playerCount = in.readInt();
                if (serverCount.containsKey(server)) {
                    serverCount.remove(server);
                }
                serverCount.put(server, playerCount);
            }