4

I have a big issue with removing a score from the scoreboard using Bukkit API. Here is my code:

ScoreBoard board;
Objective obj = board.registerNewObjective("foo", "dummy");
obj.getScore("bar").setScore(5);
// ...

Now, I need to remove that "bar" score from the scoreboard (objective). How do I do that? I did research, and couldn't find a method in the Bukkit API that would remove an existing score entry from the objective.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Victor2748
  • 4,149
  • 13
  • 52
  • 89

3 Answers3

4

You can use Scoreboard.resetScores(String) if the entry for "bar" exists only in the objective and resetting the score will remain feasible (e.g. Not when another Objective contains an entry):

{
    Objective obj;
    // ...
    obj.getScoreboard().resetScores("bar");
}

Otherwise, you can replace the objective, leaving out the item to remove:

{
    Objective obj;
    // ...
    Scoreboard sb = obj.getScoreboard();
    final HashMap<String, Integer> map = new HashMap<>();
    for (String entry : sb.getEntries())
        map.put(entry, obj.getScore(entry).getScore();
    obj.unregister();
    obj = sb.registerNewObjective("foo", "dummy");
    for (final Entry<String, Integer> entry : map.entrySet())
        obj.getScore(entry.getKey()).setScore(entry.getValue());
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
2

Maybe the resetScores() method of the Scoreboard can help (resetScores of Scoreboard)

Alternatively the unregister() method of the Objective can achieve your goal? (unregister of Objective)

I believe the second method does exactly what you want.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
timbru31
  • 365
  • 2
  • 21
2

Take a look at Scoreboard.resetScores(String).

Removes all scores for an entry on this Scoreboard

spongebob
  • 8,370
  • 15
  • 50
  • 83