Bungee Plugin Messaging (PlayerCount)

Discussion in 'Spigot Plugin Development' started by Erouax, Jun 18, 2015.

  1. Read my 2nd post for an update.

    How would i connect a player to a different server using a spigot plugin? Is there a way to use bungee and spigot APIs in 1 plugin?
    Also, how would i check if a bungee player has a bukkit/spigot perm. I want to send all players online the whole network a message but if they have a perm, the perms in bungee are too hard to setup for each specific player as you need to add them all in the config, that's why i wanted to do the bungee thingo :)
     
    #1 Erouax, Jun 18, 2015
    Last edited: Jun 18, 2015
  2. This is a tutorial on the bungeecord wiki that helps you send players to other servers. http://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/

    For permissions, I would recommend to have permission plugins on every server and have everything stored in the same database (this is an option that PermissionsEX has, but I don't know if others have it), although this could be a bit hard to setup. Another option is to use that bukkit-bungee plugin messaging channel and send a message requesting the permissions, and on the other end have a plugin that gets the request and sends the permissions back.
    This is just an idea, and I don't know if it works, because I don't make these kinds of bungee-bukkit plugins
     
  3. Thank you for the link :)
    I need to use this now for something:
    Code (Text):
    PlayerCount(top)
    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();

    I need to make an int that returns the online players of a specific server but i'm so lost with the response part. Do you know how i could do it?
    Thanks :)

    EDIT: I have done this so far, but i don't know how to select which server it gets the Output from...
    Code (Text):

        public int getOnlinePlayers(String server) {
            ByteArrayInputStream b = new ByteArrayInputStream(new byte[0]);
            DataInputStream in = new DataInputStream(b);

            try {
                return in.readInt();
            } catch (IOException e) {}
            return 0;
        }
     
    #3 Erouax, Jun 18, 2015
    Last edited: Jun 18, 2015
  4. If you read the article on the wiki again, you would have found in one of the first code snippets this
    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("SomeSubChannel")) {
          // Use the code sample in the 'Response' sections below to read
          // the data.
        }
      }
    That's what you need to use to get the amount of players in a server. Just read the whole article carefully
     
  5. Ok thanks.
    I am now going to store the players in a HashMap<String(Servername), Integer(OnlinePlayers)>.
    This is what i did:
    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();
                serverPlayersSize.put(server, in.readInt());
                Bukkit.broadcastMessage(server); // For testing
            }
        }
    But when i try to get a server from the hashmap, i get an npe. Do you know how often/when onPluginMessageRecieved is called?
     
  6. the onPluginMessageReceived is called whenever a plugin (or bungeecord) sends you a message. Bungeecord will send you a message when you send bungeecord a message requesting the amount for the server. So send the playercount request for every server, and on the receive add them.