1. Waftly

    Waftly Well-Known Member

    Waftly
    MVP+
    Solace SOLACE
    Member
    Messages:
    1,809
    Howdy,
    I'm messing around with BungeeCord cuz it's something I want to learn more about, and a lot of servers have their hub scoreboards showing the amount of players on the entire network, so I wanted to see if I could replicate that and try it out.

    I've looked at the BungeeCord messaging channel on Spigot's website, showing what I need to add, but I'm having some trouble returning the value into a Scoreboard and updating it. I'm pretty new to Java, so I'm sure it's some problem regarding my knowledge of Java.

    Code:
    package bungeecordproject.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    
    public class Main extends JavaPlugin implements Listener, PluginMessageListener {
        //§
        
        String server;
        
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
            getCommand("players").setExecutor(this);
        }
    
        @EventHandler
        public void playerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            
            ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
            Scoreboard sideScoreboard = scoreboardManager.getNewScoreboard();
            
            Objective sideObjective = sideScoreboard.registerNewObjective("Sidebar", "dummy");
            sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
            sideObjective.setDisplayName("§f§lScoreboard Title");
            
    
        
            Score serverCount = sideObjective.getScore("§fOnline Players: §a" + getCount(player, server));
            Score server1Count = sideObjective.getScore("§fPlayers on server1: §a" + getCount(player, server1 );
            Score server2Count = sideObjective.getScore("§fPlayers on serve2: §a" + getCount(player, server2);
            
            serverCount.setScore(3);
            server1Count.setScore(2);
            server2Count.setScore(1);
            player.setScoreboard(sideScoreboard);
                    
        }
        
        
        @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();
               
                //Add to scoreboard
                player.sendMessage("Player count on server " + server + " is equal to " + playerCount);
               //I can send the message to myself using a command, but I'm not sure how to redirect playerCount into the scoreboard.
            }
           
        }
       
        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());
        }
        
    

    Any help is highly appreciated, and if you have any questions feel free to let me know. Thanks! :)
     
    #1
  2. Hello!
    Do you have any errors showing up?
    What does the scoreboard actually shows?
     
    #2
  3. Shprqness

    Shprqness Member

    8a3
    MVP
    Messages:
    66
    Try this, i normally code with spigot not bungee but heh - Bukkit.getServer().getOnlinePlayers().size() (make sure to add 'size()' or scoreboard will crash)
     
    #3
  4. He wants to show how many players there are all over the server (not just in one single server).
    You need less coding and more figuring things out. You have a nice start, try to debug your code and see if there is actually a communication between the Spigot and the Bungeecord server. Good luck!
     
    #4
  5. use featherboard
     
    #5

Share This Page