Solved Give Each Player an Independent Scoreboard

Discussion in 'Spigot Plugin Development' started by Dusk_2_Dawn, Feb 1, 2020.

  1. So I am trying to make an automatically updating scoreboard that will show each player their stats on the sidebar. Everything works great. The scoreboard updates, as well as the tab list too. The only problem I am encountering is everyone sees one person's sidebar, and not their own. I tested it out, and it seems that the newest person that joins, it sets everyone's scoreboard to theirs. Here is the code that I am using:
    Code (Java):
    package net.uprisemc.brawl.Events;

    import net.uprisemc.brawl.Managers.Chat.InternalServerRank;
    import net.uprisemc.brawl.Managers.Chat.RankFinder;
    import net.uprisemc.brawl.Stats.Methoden;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Team;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Scoreboard;
    import net.uprisemc.brawl.Main;

    public class ScoreboardListener implements Listener {

        private Main main;

        public ScoreboardListener(Main main) {
            this.main = main;
        }

        @EventHandler
        public void joinServer(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            ScoreboardManager scoreboardManager = Bukkit.getServer().getScoreboardManager();
            Scoreboard scoreboard = scoreboardManager.getNewScoreboard();
            Objective objective = scoreboard.registerNewObjective("Brawl", "Dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);

            Objective health = scoreboard.registerNewObjective("health", "Health");
            health.setDisplaySlot(DisplaySlot.BELOW_NAME);
            health.setDisplayName(ChatColor.RED + "❤");

            Team xp = scoreboard.registerNewTeam("xp");
            xp.addEntry("§6§b • §7");

            Team prestige = scoreboard.registerNewTeam("prestige");
            prestige.addEntry("§c§c • §7");

            Team levelT = scoreboard.registerNewTeam("level");
            levelT.addEntry("§9§d • §7");

            objective.getScore("§e§5").setScore(14);
            objective.getScore("§d§c§lPrestige").setScore(13);
            objective.getScore("§c§c • §7").setScore(12);
            objective.getScore("§b§4").setScore(11);
            objective.getScore("§a§d§lLevel").setScore(10);
            objective.getScore("§9§d • §7").setScore(9);
            objective.getScore("§8§3").setScore(8);
            objective.getScore("§7§b§lNeeded XP").setScore(7);
            objective.getScore("§6§b • §7").setScore(6);
            objective.getScore("§5§2").setScore(5);
            objective.getScore("§4§a§lPlayers").setScore(4);
            objective.getScore("§3§a • §7...").setScore(3);
            objective.getScore("§2§1").setScore(2);
            objective.getScore("§1§6www.uprisemc.net").setScore(1);

            new BukkitRunnable() {

                @Override
                public void run() {
                    Integer level = Methoden.getLevel(p);
                    Integer nextLevel = level + 1;
                    Integer needed = ((nextLevel * 50) + (nextLevel * 15)) - Methoden.getXP(p);
                    Integer required = 0;
                    String levelString = Chat.manageLevel(Methoden.getLevel(p), Methoden.getPrestige(p));
                    String prest = Methoden.getPrestige(p).toString();

                    if (needed <= 0) {
                        required = 0;
                    } else if (needed > 0) {
                        required = needed;
                    }

                    objective.setDisplayName(main.format("&d&lBRAWL"));
                    xp.setSuffix(required + "");
                    prestige.setSuffix(prest + "");
                    levelT.setSuffix(levelString + "");

                    // THIS IS FOR THE TABLIST WHICH DOESNT CAUSE ANY PROBLEMS CURRENTLY
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        InternalServerRank playerRank = RankFinder.GetRank(all);
                        Integer height = playerRank.Weight;

                        String prefix = Chat.manageLevel(Methoden.getLevel(all), Methoden.getPrestige(all)) + " " + playerRank.ShortPrefix;

                        Team team = null;
                        for(Team team2 : scoreboard.getTeams()) {
                            if(team2.getName().contains(all.getUniqueId().toString().replace("-", "").substring(0, 10))) {
                                team = team2;
                            }
                        }
                        if(team == null || !team.getPrefix().equalsIgnoreCase(prefix)) {
                            if(team != null) team.unregister();
                            team = scoreboard.registerNewTeam(height+ "-" + all.getUniqueId().toString().replace("-", "").substring(0, 10));
                            team.setPrefix(prefix);
                            team.addEntry(all.getName());
                            team.addPlayer(all);

                            all.setScoreboard(scoreboard);
                            return;
                        }
                    }
                }
            }.runTaskTimer(main, 0, 10);

            p.setScoreboard(scoreboard);
        }
    }
     
    Any ideas?
     
  2. Correct me if I'm wrong but does this not set EVERY player's scoreboard on join? Shouldn't you only be setting the player who joins?
     
  3. Wow, I didn't even see that there. I completely forgot that it was even there. I will try that and see if it works.
     
  4. It works now! Thank you for catching that. I'm just bid dumb and can't read. Thank you so much because this has been driving me crazy for days
     
    • Like Like x 1