Get player count of server returning 422002

Discussion in 'BungeeCord Plugin Development' started by MinecraftKnightz, May 29, 2017.

  1. Hey. I'm trying to get the player count of a BungeeCord server, by using Plugin Messages.
    However, whether the server is empty or not, for some reason the in.readInt() returns 422002.
    Any ideas why this is happening?

    Thanks
     
  2. This has been a problem with my bungee network in the past, I found out the answer is the Bungee Cord API for creating modules. 422002 is the number that Bungee Cord stops at. What I did was I used a int.GetBungeePlayers() located in the API.
     
  3. @Braden_J but I'm not making a BungeeCord plugin. I'm making a Spigot plugin that connects to BungeeCord
     
  4. Oh, my bad, I'm not sure, try looking at the Bukkit to Bungee Relations.
     
  5. Where?
     
  6. There should be a part of the bukkit API that goes with Bungee. The only way I think of doing it is making a Bungee Plugin.
     
  7. Would it not just be easier to use Plugin Messages? I've looked on Google and haven't found anything related to Bukkit and Bungee that doesn't use them. Is there no fix for this bug? :(
     
  8. Well, the problem is that it's harder to call it for only one server within the Bungee Cord. What I would do is make a Bungee Cord Plugin that makes a command like /list that lists all the players online. Using the Bungee Cord API, I don't think it is possible through spigot/bukkit.
     
  9. Darn :(
    I wonder how plugins like HolographicDisplays do it though. Are you sure that this won't work? https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#playercount
     
  10. MiniDigger

    Supporter

    what exactly are you trying to do? do you want to get the count of a server, connected to bungee? thats not a bungee server :D
    for spigot->bungee->spigot communications the messaging channel should work just right, as long as there are players on the requesting and requested servers which can forward the messages for you
     
  11. My bad, I didn't explain the question properly. Yes, I'm trying to get the player count of a server connected to BungeeCord, to put in a hub plugin I'm making. I'm using the Messaging Channel, but for some reason it's returning 422002
     
  12. MiniDigger

    Supporter

    are you writing a bungeecord plugin? because then you could just use the api.
    if you are writing a spigot plugin, the messaging channel should work just fine. it just calls the api internally: https://github.com/SpigotMC/BungeeC.../bungee/connection/DownstreamBridge.java#L363
    are you sure that you don't have anything running on that server that might change the count?
     
  13. Not that I can think of. The server I'm getting the details from has no plugins on it, so nothing should be interfering. Most probably, I've done something wrong with my code, even though I don't think I have :D
     
    #13 MinecraftKnightz, May 31, 2017
    Last edited: May 31, 2017
  14. Can you post a snippet of your code?
     
  15. Code (Text):
    public class ServerSelector{
    public static int playerCount = 0;
    public static void openGui(Player p) throws NumberFormatException {
            String path = "server-selector.";
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF("PlayerCount");//for debugging
            out.writeUTF("prison");//for debugging
            p.sendMessage(playerCount + ""); //for debugging
            inv = Bukkit.createInventory(p, 9, color(config.getString(path + "name")));
            for (String item : config.getConfigurationSection(path + "items").getKeys(false)) {
                String itemPath = path + "items." + item;
                out.writeUTF("PlayerCount");
                out.writeUTF(config.getString(itemPath + ".server-name"));
                p.sendPluginMessage(ServerSelector.p, "BungeeCord", out.toByteArray());
    }
    }
    public class Hub extends JavaPlugin implements PluginMessageListener{
    @Override
    public void onEnable() {
    //stuff
    this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
    }
    public void onPluginMessageReceived(String channel, Player p, byte[] message) {
            if (!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
            if (subchannel.equals("PlayerCount")) {
                ServerSelector.playerCount = in.readInt();
            }
        }
    EDIT: I removed the debugging bit from my code, and reset playerCount back to 0 at the end of every iteration. Now, there's apparently 553835 players online
     
  16. MiniDigger

    Supporter

    do a minimal example to test it.
    just send a plugin message for the player count of prison and print out the result in the plugin message listener. that should work.
    from the look of it you have problems to understand how async communication works.
     
  17. I've made a tiny testing plugin with this code:
    Code (Text):
    package uk.knightz.test;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerToggleSneakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;

    public class Main extends JavaPlugin implements PluginMessageListener, Listener {

        @Override
        public void onEnable() {
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
            this.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onPluginMessageReceived(String channel, Player p, byte[] message) {
            if (!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
            if (subchannel.equals("PlayerCount")) {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    player.sendMessage(in.readInt() + "");
                }
            }
        }

        @EventHandler
        public void sneak(PlayerToggleSneakEvent e) {
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF("PlayerCount");
            out.writeUTF("skyblock");
            e.setCancelled(true);
            e.getPlayer().sendPluginMessage(this, "BungeeCord", out.toByteArray());
        }

    }
     
    It's still returning 553835
     
  18. MiniDigger

    Supporter

    uuuuuh, I think I know whats going on.
    you need to read the server name first. currently you are reading the first few bits of the server name as integer, thats why you get that strange number.
    bungee writes the server name first into your response, followed by the number of players https://github.com/SpigotMC/BungeeC...ee/connection/DownstreamBridge.java#L362-L363
    its actually explained like that on the wiki
     
    • Agree Agree x 2
  19. That's fixed it, thanks! I thought it was optional, and just for getting the server name :D
     
  20. MiniDigger

    Supporter

    Yeah, that's not how streams work, nothing is optional. you need to read everything back in the same order as it was written.
     
    • Useful Useful x 1