Solved Player specific scoreboard

Discussion in 'Spigot Plugin Development' started by zt38475tz, Feb 9, 2020.

  1. Hello guys, first of all I hate scoreboards. Second I want to program a scoreboard which displays the players rank in one its scores. My problem is when a player joins the server everybody gets the rank of the new player displayed. I already tried a hashmap on this but I think I did something wrong:

    Code (Text):
    package de.tsunderelover.lobbysystem.scoreboard;

    import de.tsunderelover.lobbysystem.LobbySystem;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.*;

    import java.util.HashMap;
    import java.util.UUID;

    public class ScoreboardSetup {

        private static final Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();

        private static Objective objective;

        private static Team onlineCounter;

        private static Team rank;

        public static HashMap<UUID, Scoreboard> scoreboards = new HashMap<>();

        public void setScoreboard(Player player) {
            String serverName = ChatColor.translateAlternateColorCodes('&', LobbySystem.baseConfigYaml.getString("scoreboard.serverName"));

            if (scoreboard.getObjective(serverName) == null) {
                objective = scoreboard.registerNewObjective(serverName, "dummy");

            }

            objective.setDisplaySlot(DisplaySlot.SIDEBAR);

            Score rankDisplay = objective.getScore(ChatColor.GRAY + "» Rank");
            rankDisplay.setScore(12);

            Score spacer1 = objective.getScore("§f§r----------");
            spacer1.setScore(16);

            Score spacer2 = objective.getScore("§0§r----------");

            if (LobbySystem.baseConfigYaml.getBoolean("scoreboard.showOnlinePlayers")) {
                Score onlineName = objective.getScore(ChatColor.GRAY + "» Online");
                onlineName.setScore(15);

                if (scoreboard.getTeam("onlineCounter") == null) {
                    onlineCounter = scoreboard.registerNewTeam("onlineCounter");
                }

                onlineCounter.addEntry(ChatColor.BLACK + "" + ChatColor.WHITE);

                if (Bukkit.getOnlinePlayers().size() == 0) {
                    onlineCounter.setPrefix(ChatColor.DARK_RED + "0" + ChatColor.RED + "/" + ChatColor.DARK_RED + Bukkit.getMaxPlayers());
                } else {
                    onlineCounter.setPrefix("" + ChatColor.DARK_RED + Bukkit.getOnlinePlayers().size() + ChatColor.RED + "/" + ChatColor.DARK_RED + Bukkit.getMaxPlayers());
                }

                objective.getScore(ChatColor.BLACK + "" + ChatColor.WHITE).setScore(14);

            }

            spacer2.setScore(13);

            if (scoreboard.getTeam("rankDisplay") == null) {
                rank = scoreboard.registerNewTeam("rankDisplay");
            }

            if (!scoreboards.isEmpty()) {
                scoreboards.remove(player.getUniqueId());

            }

            rank.addEntry(ChatColor.RED + "" + ChatColor.WHITE);

            if (player.hasPermission("lobbysystem.admin")) {
                rank.setPrefix(ChatColor.DARK_RED + "Admin");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else if (player.hasPermission("lobbysystem.srdev")) {
                rank.setPrefix(ChatColor.AQUA + "SrDev");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else if (player.hasPermission("lobbysystem.dev")) {
                rank.setPrefix(ChatColor.AQUA + "Dev");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else if (player.hasPermission("lobbysystem.srmod")) {
                rank.setPrefix(ChatColor.RED + "SrMod");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else if (player.hasPermission("lobbysystem.mod")) {
                rank.setPrefix(ChatColor.RED + "Mod");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else if (player.hasPermission("lobbysystem.supp")) {
                rank.setPrefix(ChatColor.GREEN + "Supp");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else if (player.hasPermission("lobbysystem.builder")) {
                rank.setPrefix(ChatColor.YELLOW + "Builder");
                scoreboards.put(player.getUniqueId(), scoreboard);

            } else {
                rank.setPrefix(ChatColor.GRAY + "Player");
                scoreboards.put(player.getUniqueId(), scoreboard);

            }

            objective.getScore(ChatColor.RED + "" + ChatColor.WHITE).setScore(11);

            player.setScoreboard(scoreboards.get(player.getUniqueId()));

        }

    }
     
    And in the JoinEvent I am setting/updating the scoreboard for everyone. I also tried to update it for only the person who joins the server but still the same result. Please help!
     
  2. You reference the objective, online counter and teams from outside your setScoreboard method. So when you change them, you change them for everyone. You should move these into your setScoreboard method.
     
    • Friendly Friendly x 1
  3. Omg, totally forgot that (trying since yesterday and with the hashmap I forgot to move them). Thanks man
     
    • Like Like x 1