Solved Getting online players from BugeeCord

Discussion in 'Spigot Plugin Development' started by digitox, Apr 21, 2019.

Thread Status:
Not open for further replies.
  1. Hi,
    In my latest spigot plugin I got an issue where I could not get the online players from BugeeCord to a Scoreboard.
    Here is the code:D
    Code (Text):
    public class main extends JavaPlugin implements PluginMessageListener {

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

    @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((main.getPlugin(main.class), "BungeeCord", out.toByteArray());
     
        }
    }
    Code (Text):
    public class scoreboard {

        static main plugin = main.getPlugin(main.class);

        public void getScoreBoard(Player p) {
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();

            Objective objective = board.registerNewObjective("bbbb", "aaaa");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("   ONLINE PLAYERS   ");

            Score oline = objective.getScore(main.getCount(p, "ALL"));
            oline.setScore(1);

            p.setScoreboard(board);

    }
    }
    @
    Score oline = objective.getScore(main.getCount(p, "ALL"));
    "The method getScore(OfflinePlayers) in the type Objective is not applicable the arguments (void)"

    Thank you.
     
    #1 digitox, Apr 21, 2019
    Last edited: Apr 22, 2019
  2. Your method getCount return void and not an integer
     
    • Agree Agree x 1
  3. You also seem to be missing a bracket at the end;

    Code (Text):

    Score oline = objective.getScore(main.getCount(p, "ALL") -->)<--;
     
     
  4. Sorry forgot too add it here, but it is in the code.
     
  5. Could I create a method that returns an int?
     
  6. Yes, you can define a variable who change when your plugin receive a message.
     
  7. How do i do that?
     
  8. Initialise a variable "totalPlayerCount" and in this code:

    Code (Text):
            if (subchannel.equals("PlayerCount")) {
                String server = in.readUTF();
                int playerCount = in.readInt();
       
                player.sendMessage("Player count on server " + server + " is equal to " + playerCount);
       
            }
    Update the variable
     
  9. If I do that will I need to call the onPluginMessageReceived in the scoreboard? and get it to update the number every time someone leave or join the server?
     
  10. Update the variable when you call the "onPluginMessageReceived"
     
  11. And update the scoreboard too with a runnable
     
  12. But what do I write in the onPluginMesageRecived(String, Player, byte[])
    And how would I Initialise the variable in the onPluginMesageRecived, I have not worked with onPluginMesageRecived before.
     
  13. Try this:

    Code (Text):
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;

    public class scoreboard {


        public void getScoreBoard(Player p) {
            main.getInstance().getCount(p, null);
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();

            Objective objective = board.registerNewObjective("bbbb", "aaaa");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("   ONLINE PLAYERS   ");

            Score oline = objective.getScore(main.getInstance().getPlayerCount() + "");
            oline.setScore(1);

            p.setScoreboard(board);

        }
    }
    Code (Text):
    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    public class main extends JavaPlugin implements PluginMessageListener {

        private int playerCount = 0;
        private static main instance;

        public void onEnable() {
            instance = this;

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

        public static main getInstance() {
            return instance;
        }


        @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();
                this.playerCount = in.readInt();
            }

        }

        public int getPlayerCount() {
            return 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());

        }
    }
     
     
  14. I get this message when I try to run the code.
    [22:04:01 ERROR]: Could not pass event PlayerJoinEvent to blockgames-Lobby v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:298) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:157) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:814) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
    Caused by: java.lang.NullPointerException
    at me.digitox.com.util.scoreboard.getScoreBoard(scoreboard.java:20) ~[?:?]
    at me.digitox.com.events.playerJoinandLeave.onJoinEvent(playerJoinandLeave.java:36) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    ... 14 more

    scoreboard.java: 20 = main.getInstance().getCount(p, null);
    playerJoinandLeave.java:36 = sc.getScoreBoard(player);
     
  15. Send all your code. Maybe you have changed something
     
  16. Code (Text):
    public class main extends JavaPlugin implements PluginMessageListener {
       private int playerCount = 0;
        private static main instance;

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

    public static main getInstance() {
            return instance;
        }


        @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();
                this.playerCount = in.readInt();
            }

        }

        public int getPlayerCount() {
            return 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());

        }
    }
     
     
    Code (Text):
    public class scoreboard {

        static main plugin = main.getPlugin(main.class);

        public void getScoreBoard(Player p) {
           
            main.getInstance().getCount(p, null);
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();

            Objective objective = board.registerNewObjective("bbbb", "aaaa");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("   ONLINE PLAYERS   ");

            Score oline = objective.getScore(main.getInstance().getPlayerCount() + "");
            oline.setScore(1);

            p.setScoreboard(board);

    }
    }
     
  17. And your join listener please
     
    • Informative Informative x 1
  18. Code (Text):

    public void onJoinEvent(PlayerJoinEvent e) {

           

            scoreboard sc = new scoreboard();

            for(Player player : Bukkit.getOnlinePlayers()) {

                sc.getScoreBoard(player);



            }
     
     
  19. Is there "@EventHandler" above your "onJoinEvent" method ?
     
  20. You don't initialise the variable "instance" in your onEnable -_-
     
Thread Status:
Not open for further replies.

Share This Page