Solved Scoreboard displaying other players score upon joining

Discussion in 'Plugin Development' started by sgavster, Feb 16, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    sgavster

    Hello! I have a scoreboard for my server with your stats on it but when another player joins you see their stats.. How can I fix this? I can not figure it out. Thanks for any help, here's my code:


    Code:
    package me.sgavster.qmc.func;
    
    import me.sgavster.qmc.util.MSG;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    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;
    
    public class ScoreBoard implements Listener {
    
        public static ScoreboardManager man = Bukkit.getScoreboardManager();
        public static Scoreboard sameBoard;
    
        public static void setNewBoard(Player p) {
    
            if (sameBoard == null) {
    
                Scoreboard b = man.getNewScoreboard();
                Objective o = b.registerNewObjective("QuadularMC", "dummy");
                o.setDisplaySlot(DisplaySlot.SIDEBAR);
    
                o.setDisplayName(MSG.quadularmc);
    
                Score votes = o
                        .getScore(LoginHandler.getThings(p).getInt("votes") == 0 ? "§2None. /vote"
                                : "§2" + LoginHandler.getThings(p).getInt("votes"));
                votes.setScore(1);
    
                Score votez = o.getScore("§bVotes:");
                votez.setScore(2);
    
                Score blank = o.getScore(" ");
                blank.setScore(3);
    
                Score deaths = o.getScore(LoginHandler.getThings(p)
                        .getInt("deaths") == 0 ? "§2None!" : "§2"
                        + LoginHandler.getThings(p).getInt("deaths"));
                deaths.setScore(4);
    
                Score deathz = o.getScore("§bDeaths:");
                deathz.setScore(5);
    
                p.setScoreboard(b);
    
                sameBoard = p.getScoreboard();
    
            }
        }
    
        public static void updateBoard(Player p) {
            if (p.getScoreboard() == null) {
                setNewBoard(p);
            } else {
                if (LoginHandler.getThings(p).getString("settings.scoreboard")
                        .equalsIgnoreCase("yes")) {
                    sameBoard = p.getScoreboard();
                } else {
                    p.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
                }
    
                if (sameBoard != null) {
    
                    Objective oldO = sameBoard.getObjective(DisplaySlot.SIDEBAR);
                    if (oldO == null) {
                        if (sameBoard.getObjective("QuadularMC") == null)
                            oldO = sameBoard.registerNewObjective("QuadularMC",
                                    "dummy");
                        else {
                            oldO = sameBoard.getObjective("QuadularMC");
                        }
                    }
    
                    if (sameBoard.getObjective("QuadularMC") != null
                            && sameBoard != null || sameBoard.getEntries() != null) {
                        for (String players : sameBoard.getEntries()) {
                            sameBoard.resetScores(players);
                        }
                    }
    
                    oldO.setDisplayName(MSG.quadularmc);
                    Score votes = oldO.getScore(LoginHandler.getThings(p).getInt(
                            "votes") == 0 ? "§2None. /vote" : "§2"
                            + LoginHandler.getThings(p).getInt("votes"));
                    votes.setScore(1);
    
                    Score votez = oldO.getScore("§bVotes:");
                    votez.setScore(2);
    
                    Score blank = oldO.getScore(" ");
                    blank.setScore(3);
    
                    Score deaths = oldO.getScore(LoginHandler.getThings(p).getInt(
                            "deaths") == 0 ? "§2None!" : "§2"
                            + LoginHandler.getThings(p).getInt("deaths"));
                    deaths.setScore(4);
    
                    Score deathz = oldO.getScore("§bDeaths:");
                    deathz.setScore(5);
    
                    p.setScoreboard(sameBoard);
                    oldO.setDisplaySlot(DisplaySlot.SIDEBAR);
                }
            }
        }
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (sameBoard == null) {
                setNewBoard(p);
            } else {
                updateBoard(p);
            }
        }
    }
     
  2. Offline

    MaTaMoR_

    You're using the same scoreboard for every player, you must create a new scoreboard for each player.
     
  3. Offline

    sgavster

    @MaTaMoR_ I almost thought I was doing that.. What would be the easiest way to make a seperate one for each player?
     
  4. Offline

    MaTaMoR_

    You can try using this class :

    Code:
    //Set scoreboard
    Board board = new Board();
    board.setLine(0, "Text");
    
    player.setScoreboard(board);
    
    //Update scoreboard - Add checks for null
    Board board = new Board(player);
    board.setLine(0, "Updated");
    
     
  5. Offline

    sgavster

    @MaTaMoR_
     
  6. Offline

    MaTaMoR_

    board.getScoreboard();
     
  7. Offline

    sgavster

    @MaTaMoR_ Yeah I just figured that out; thanks. It seems to work (as far as I can tell) I'll let you know if it works when another player logs on, thanks a bunch!
    EDIT: Works like a charm! Thanks a bunch.
     
    Last edited: Feb 16, 2016
Thread Status:
Not open for further replies.

Share This Page