Removing an object from scoreboard and replacing it with new objective

Discussion in 'Plugin Development' started by plisov, Oct 21, 2016.

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

    plisov

    I've been trying to make my scoreboard more organized so I decided to put the scores at a standard amount that don't change and put the changing numbers in the objective's title. Every time the number changes however, the objective duplicates. How would I remove the old objective and replace it with the new objective?
    Here is the code:
    Code:
    String levels = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Levels" + ChatColor.DARK_GRAY + "] ";
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
    
            Player player = event.getPlayer();
    
            String name = player.getName();
    
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
    
            Objective objective = board.registerNewObjective("test", "dummy");
    
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
           
            if(player.getName().length() >= 32) {
            objective.setDisplayName(ChatColor.RED + "KitPVP");   
            } else {
            objective.setDisplayName(ChatColor.RED + "KitPVP " + ChatColor.GRAY + "(" + ChatColor.GRAY + player.getName() + ChatColor.GRAY + ")");
            }
           
            if(!getConfig().contains(name)) {
    
                getConfig().set(name + ".kills", 0);
                getConfig().set(name + ".deaths", 0);
    
                getConfig().set(name + ".points", 0);
                getConfig().set(name + ".level", 0);
                saveConfig();
            }
    
            int kills1 = getConfig().getInt(name + ".kills");           
            int deaths1 = getConfig().getInt(name + ".deaths");
    
            int level = getConfig().getInt(name + ".level");
            int points = getConfig().getInt(name + ".points");
            int until = points - 10;
            until = until - until*2;
           
            int kdr = (Math.round(kills1/deaths1));
    
            Score levelsb = objective.getScore(ChatColor.GREEN + "Level: " + level);
            levelsb.setScore(5);
    
            Score line1 = objective.getScore(ChatColor.LIGHT_PURPLE + "=-=-=-=-=-=-=-=-=-=-=-=");
            line1.setScore(3);
           
            Score line2 = objective.getScore(ChatColor.LIGHT_PURPLE + "=-=-=-=-=-=-=-=-=-=-=-=");
            line2.setScore(6);
    
            Score ratio = objective.getScore(ChatColor.GREEN + "K.D. Ratio: " + kdr);
            ratio.setScore(2);
    
            Score kills = objective.getScore(ChatColor.GREEN + "Kills: " + kills1);
            kills.setScore(1);
    
            Score deaths = objective.getScore(ChatColor.RED + "Deaths: " + deaths1);
            deaths.setScore(0);
    
            Score pointsb = objective.getScore(ChatColor.AQUA + "Points Needed to Level-Up: " + until);
            pointsb.setScore(4);
    
            player.setScoreboard(board);
        }
    I have two other classes that are pretty much similar. One is on move and the other is on kill. Expect it's not setting the config number to 0. It's adding 1 in the on death one. The on move just makes the scoreboard update. That's besides the point.
     
  2. Offline

    Zombie_Striker

    @plisov
    1. Get all the entries by using Scoreboard#getEntries
    2. Use a For-each loop for all the entries.
    3. Using Scoreboard#getScore(), find the score for each entry. If that score is equal to the one you want to remove, remove it using ScoreBoard#resetScores() and break out of the for loop.
     
Thread Status:
Not open for further replies.

Share This Page