Players sharing same scoreboard?

Discussion in 'Spigot Plugin Development' started by Qruet, Aug 8, 2018.

  1. Not sure why this is happening, but the last player who's scoreboard is updated/created forces everyone else's scoreboards to be exactly the same. I don't see how this is possible since I've checked to make sure the scoreboards aren't setup with the same objective, teams/entries etc. but perhaps I need a second pair of eyes. I haven't spent a whole lot of time with scoreboards therefore perhaps I'm forgetting something basic.

    Code (Text):
    package net.pyrix.mc.pvm.arena.scoreboard;

    import net.pyrix.mc.pvm.arena.Arena;
    import net.pyrix.mc.pvm.arena.ArenaPlayer;
    import net.pyrix.mc.pvm.utils.ArenaUtils;
    import net.pyrix.mc.pvm.utils.T;
    import net.pyrix.mc.pvm.utils.ToolBox;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.*;

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    public class ScoreboardManager {

        private static Map<UUID, String> registeredObjectives = new HashMap<>();

        private static final String[][] teams = {
                {"&eMob Arena"},
                {"arena", ChatColor.BLACK + "", " &7Arena ", "&f%arena%"},
                {"wave", ChatColor.WHITE + "", " &7Wave ", "&f%wave%"},
                {"highscore", ChatColor.YELLOW + "", " &7Highscore ", "&f%highscore%"}};

        public static void createScoreboard(ArenaPlayer aplayer) {
            Player player = aplayer.getPlayer();
            Scoreboard scoreboard = player.getScoreboard();
            Objective test = scoreboard.getObjective("pvm:" + shortenName("" + player.getUniqueId(), 10));
            if (test != null) {
                test.unregister();
            }
            Objective obj = scoreboard.registerNewObjective("pvm:" + shortenName("" + player.getUniqueId(), 10), shortenName("" + player.getUniqueId(), 10));
            registeredObjectives.put(player.getUniqueId(), obj.getName());
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName(T.c(" &9Prominent &8Gaming "));

            int score = 15;
            for (String[] line : teams) {
                if (line.length == 1) {
                    Score l = obj.getScore(T.p(aplayer, line[0]));
                    l.setScore(score);
                } else if (line.length == 4) {
                    Team t = scoreboard.getTeam(line[0]);
                    if (t == null) {
                        t = scoreboard.registerNewTeam(line[0]);
                        t.addEntry(line[1]);
                    }
                    t.setPrefix(T.p(aplayer, line[2]));
                    t.setSuffix(T.p(aplayer, line[3]));
                    obj.getScore(line[1]).setScore(score);
                }
                score--;
            }

            if (aplayer.getArena().getPlayers().size() > 1) {
                Score blank = obj.getScore("            ");
                blank.setScore(score--);
                Score teamplayers = obj.getScore(T.c("&eTeam"));
                teamplayers.setScore(score--);

                int pnum = 0;
                for (Player p : aplayer.getArena().getPlayers()) {

                    ArenaPlayer ap = ArenaPlayer.getArenaPlayerFromPlayer(p);

                    if (p.getUniqueId().equals(player.getUniqueId())) {
                        continue;
                    }

                    if ((score - pnum) < 0) {
                        break;
                    }

                    Team players = scoreboard.getTeam(p.getName() + "health");

                    if (players != null) {
                        players.unregister();
                    }

                    players = scoreboard.registerNewTeam(p.getName() + "health");

                    players.addEntry(ChatColor.RED + "" + ap.getAssignedColor() + "");

                    players.setPrefix(T.c(" " + ap.getAssignedColor() + shortenName(p.getName(), 14)));
                    players.setSuffix(T.c(" &c" + getHealthMarker(ap, 10)));


                    obj.getScore(ChatColor.RED + "" + ap.getAssignedColor() + "").setScore(score - pnum);

                    pnum++;
                }
            }
            player.setScoreboard(scoreboard);

        }

        public static void updateScoreboard(ArenaPlayer aplayer) {
            Player player = aplayer.getPlayer();
            Scoreboard scoreboard = player.getScoreboard();

            if (registeredObjectives.containsKey(player.getUniqueId())) {
                createScoreboard(aplayer);
                return;
            }

            for (String[] line : teams) {
                if (line.length == 4) {
                    Team team = scoreboard.getTeam(line[0]);
                    team.setPrefix(T.p(aplayer, line[2]));
                    team.setSuffix(T.p(aplayer, line[3]));
                }
            }


            if (aplayer.getArena().getOriginalPlayers().size() > 1) {
                for (Player p : aplayer.getArena().getOriginalPlayers()) {
                    if (p.getUniqueId().equals(player.getUniqueId())) {
                        continue;
                    }

                    Team players = scoreboard.getTeam(p.getName() + "health");

                    if (players == null) {
                        continue;
                    }

                    ArenaPlayer ap = ArenaPlayer.getArenaPlayerFromPlayer(p);

                    if (ap.getArena() == null) {
                        players.setPrefix(T.c(" &7&o" + shortenName(p.getName(), 12)));
                    }

                    players.setSuffix(T.c(" &c" + getHealthMarker(ap, 10)));
                }
            }
        }

        private static String shortenName(String name, int max_length) {
            if (name.length() <= max_length) {
                return name;
            }
            String[] chars = name.split("");
            String shortname = "";
            for (int c = 0; c < max_length; c++) {
                shortname += chars[c];
            }
            return shortname;
        }

        private static String getHealthMarker(ArenaPlayer aplayer, int max) {
            if (aplayer.getArena() != null) {
                Player player = aplayer.getPlayer();
                double ratio = player.getHealth() / player.getMaxHealth();
                int cmax = (int) (max * ratio);
                String health = "";
                for (int h = 0; h < cmax; h++) {
                    health += "❤";
                }
                return health;
            }
            return "";
        }

        public static void removeScoreboard(Player player) {
            Scoreboard scoreboard = player.getScoreboard();
            if (registeredObjectives.containsKey(player.getUniqueId())) {
                String objective = registeredObjectives.get(player.getUniqueId());
                Objective obj = scoreboard.getObjective(objective);
                if (obj != null) {
                    obj.unregister();
                }
                registeredObjectives.remove(player.getUniqueId());
            }
        }

    }
     
     
  2. Might be because instead of getting a new Scoreboard, you're just getting the scoreboard of the Player in the createScoreboard method.

    I don't use Scoreboards much at all myself, but I think ScoreboardManager#getNewScoreboard() might be preferable to getting the one on the Player, since the default one is probably the same for all the players. So just create a new one per-player, you can probably store it in a map if you don't want to keep creating new boards for each player. Then set the player's scoreboard to the new one.
     
    #2 OffLuffy, Aug 8, 2018
    Last edited: Aug 8, 2018