Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset score from an objective?

Tags:

java

bukkit

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.

like image 279
Victor2748 Avatar asked Feb 11 '23 21:02

Victor2748


2 Answers

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());
}
like image 186
Unihedron Avatar answered Feb 23 '23 00:02

Unihedron


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.

like image 29
timbru31 Avatar answered Feb 23 '23 00:02

timbru31