Player Count

Discussion in 'BungeeCord Plugin Development' started by DisCoder212, Mar 20, 2017.

  1. Hey, I posted this on Spigot Plugin Development first but they said to transfer over here.

    This is a use of SpigotAPI with messaging channels to connect with bungee

    So basically I have this set in my main class:

    Code (Text):

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

    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if (!channel.equals("BungeeCord")) {
        ByteArrayDataInput in = ByteStreams.newDataInput(message);
        String subchannel = in.readUTF();
        if (subchannel.equals("PlayerCount")) {

            String server = in.readUTF();
            int playercount = in.readInt();

            getPlayerCount.playercount = playercount;
            getPlayerCount.server = server;

    And I have my class to get the player count:

    Code (Text):

    public class getPlayerCount {

        public static String server;
        public static int playercount;

        public static int getServerPlayerCount(String servername, Player player){

            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);

            try {
                player.sendPluginMessage(Main.getPlugin(), "BungeeCord", b.toByteArray());
            } catch (Exception e1) {
            return playercount;
    For some reason this doesn't get the player count, I tried many things but I still can't seem to get this to work. And this is posted under spigot development because this is still the SpigotMCAPI just using Bungee Messaging to connect with bungee(for anyone who says this should be posted on bungee development)
  2. This might help

    Code (Text):

    BungeeChannelApi api = BungeeChannelApi.of(this); // this = Plugin instance.

    api.getPlayerCount("ALL") // ALL = all servers, you can change to a specific server name
      .whenComplete((result, error) -> {
        // result = number of online players
  3. Keep in mind, that you can only use channels, if there is at least 1 Player online.
  4. I don't like the pluginmessagechannel, because there are many errors when i try it. I use Sockets to send data :)
    • Agree Agree x 3
    • Like Like x 2
  5. I would use Sockets, or if possible use Redis as it's a lot easier.
  6. Using the Plugin Messaging API requires a player online. Using sockets is much simpler and it doesn't require a player to be online.
    This is what I use (or you can create your own API) ->

    Maybe this will make your code look cleaner and better?
    Code (Text):
    private static HashMap<String, Integer> players = new HashMap<>();

    public static void setPlayerAmount(String server, int amount){
        players.put(server, amount);

    public static int getPlayerAmount(String server){
        if (players.contains(server)){
            return players.get(server);
        return 0;
    #6 ZyrkRan, Apr 14, 2017
    Last edited: Apr 14, 2017
  7. if (players.contains(server)) here error

Share This Page