PlayerCount BungeeCord in my scorebord

Discussion in 'Spigot Help' started by raphigames31, Aug 12, 2018.

  1. hello, i want to make a playercount on my sb, but i don't know how i can

    Code (Text):
    package fr.karna;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import fr.karna.Event.Scoreboard.LobbbyScoreboard;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import fr.karna.Event.EventManager;
    import fr.karna.Event.Utils.ChangeServeur;
    import org.bukkit.plugin.messaging.PluginMessageListener;
    import org.bukkit.scheduler.BukkitRunnable;
    import sun.applet.Main;

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;

    public class Lobby extends JavaPlugin implements PluginMessageListener{
        public static Lobby instance;
        @Override
        public void onEnable() {

            instance = this;
            ChangeServeur.init(this);
            EventManager.registerListener(this);
            Bukkit.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
            Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");

        }
        @Override
        public void onDisable() {

        }

        int nb_pl = 0;

        public static Lobby 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();
                nb_pl = in.readInt();
            }
        }

        public void getPlayerCount(Player player, String server) {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);
            try {
                out.writeUTF("PlayerCount");
                out.writeUTF(server);
            } catch (IOException e) {
                e.printStackTrace();
            }
            player.sendPluginMessage(this, "BungeeCord", b.toByteArray());
        }

        public void requestPlayercount(String server) {
            ByteArrayDataOutput output = ByteStreams.newDataOutput();

            output.writeUTF("PlayerCount");
            output.writeUTF(server);

            getServer().sendPluginMessage(this, "BungeeCord", output.toByteArray());
        }
    }
    Code (Text):
    package fr.karna.Event.Scoreboard;

    import com.google.common.collect.Iterables;
    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    import fr.itsalexousd.synergyrank.gestion.Account;
    import fr.karna.Lobby;
    import fr.karna.Event.Utils.title;

    public class LobbbyScoreboard {


        public static void sendScoreboard(Player player) {


            ScoreboardManager sb = new ScoreboardManager(player, "§eSynergyUHC");

            sb.create();

            sb.setLine(6, "");
            sb.setLine(5, "§6Players en ligne: §f"+ Bukkit.getOnlinePlayers().size()+ Bukkit.getOnlinePlayers().size());
            sb.setLine(4, "");
            sb.setLine(3, "§6Grade: " +Account.getAccount(player).getRank().getColoredName());
            sb.setLine(2, "");
            sb.setLine(1, "§emc.synergyuhc.eu");

            Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(Lobby.getInstance(), new Runnable() {
                public void run() {
                    sb.setLine(5, "§6Players en ligne: §f"+ Bukkit.getOnlinePlayers().size());
                    sb.setLine(3, "§6Grade: " +Account.getAccount(player).getRank().getColoredName());
                    title.sendActionBar(player, "§bhttps://discord.gg/hT6u6X");
                }
            }, 10L, 10L);

        }



    }
     
     
  2. I'm not the best programmer, but if a player has joined the bungeecord server, the player executes the command "/scoreboardplayers (Playercount)". The command is registered in the spigot plugin and the first argument gets implemented in the scoreboard, now the scoreboard needs to get updated for all players.

    - Jakob | Austria, 12.08.18

    The Code:
    Code (Text):
    int playercount = 0;
    for(ProxiedPlayer all : BungeeCord.getInstance().getPlayers() {
           playercount++;
    }
    player.chat("/scoreboardplayers "+playercount)