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.
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());
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With