Solved ScoreBoard Problems Refreshing

Discussion in 'Plugin Development' started by Trap Lul G, Jan 16, 2017.

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

    Trap Lul G

    Hello. I'm having some problems refreshing my scoreboard. When M or P level is changed, the scoreboards adds the new number under the old number. I understand this is confusing so I posted pictures of the problem below. I'm calling my function every time a player joins the game.

    Pictures (open)

    Picture 1: "P Lvl" is 66. Everything is ok.
    [​IMG]
    Picture 2: "P Lvl" is changed to 80. Instead of 66 being replaced it's now under 66.
    [​IMG]


    Here is my code:
    Code:
    public class ScoreBoardUtil {
    
        private static final ScoreboardManager MANAGER = Bukkit.getScoreboardManager();
        private static final Scoreboard BOARD = MANAGER.getNewScoreboard();
        private static Objective objective = BOARD.registerNewObjective("server", "server123");
    
        public static void createScoreboard(Player player) {
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("§c§l servername 10.1.1");
    
            Score line14 = objective.getScore("§0-===============- ");
            Score line13 = objective.getScore("  ");
            Score line12 = objective.getScore("§c§lM Lvl:");
            Score line11 = objective.getScore("§5§l" + Level.getMLevel(player));
            Score line10 = objective.getScore("   ");
            Score line9 = objective.getScore("    ");
            Score line8 = objective.getScore("§c§P Lvl:");
            Score line7 = objective.getScore("§5§l" + Level.getPLevel(player) + "      ");
            Score line6 = objective.getScore("     ");
            Score line5 = objective.getScore("      ");
            Score line4 = objective.getScore("§c§lRank:");
            Score line3 = objective.getScore("§5§l" + PlayerRank.getPlayerRank(player));
            Score line2 = objective.getScore("             ");
            Score line1 = objective.getScore("§0-===============-");
    
            line1.setScore(1);
            line2.setScore(2);
            line3.setScore(3);
            line4.setScore(4);
            line5.setScore(5);
            line6.setScore(6);
            line7.setScore(7);
            line8.setScore(8);
            line9.setScore(9);
            line10.setScore(10);
            line11.setScore(11);
            line12.setScore(12);
            line13.setScore(13);
            line14.setScore(15);
    
            player.setScoreboard(BOARD);
        }
    }
    
    How I'm calling the code:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            ScoreBoardUtil.createScoreboard(player);
        }
    
    //PS: This isn't how it's going to be called in the future. I'm simply calling this on playerjoin for debugging.
    
     
    Last edited: Jan 16, 2017
  2. Offline

    Zombie_Striker

    @Trap Lul G
    Every time you want to update a scoreboard, before adding the new value, loop through all the scores and find the one you want to replace and remove it.
     
  3. Offline

    Trap Lul G

    How would I replace and remove a score? Would I simply re-define the variable?

    For example:
    line7 = objective.getScore...
     
  4. Offline

    Zombie_Striker

    @Trap Lul G
    1. For loop through all the scores.
    2. If the score is equal to the score you want (7), remove it using the .remove method.
    3. break out of the for loop, and add the new score.
     
  5. Offline

    Trap Lul G

    Edit: fixed code for people having the same problem as me

    Code:
          
                BOARD.resetScores(line7.getEntry());
                line7 = objective.getScore("§5§l" + Level.getParkourLevel(player) + "      ");
            
    Thanks @Zombie_Striker.
     
    Last edited: Jan 16, 2017
  6. Offline

    Zombie_Striker

    @Trap Lul G
    It should be fine. Just remove the if statement, since it is unnecessary and will never be true.
     
    Trap Lul G likes this.
Thread Status:
Not open for further replies.

Share This Page