[Tutorial] Scoreboards/Teams with the Bukkit API

Discussion in 'Resources' started by chasechocolate, Apr 5, 2013.

  1. So you've probably seen this tutorial and are wondering why I posted this. The problem with that is it uses NMS code (specifically packets) and is version dependent so it will break every update. Bukkit recently added it's own Scoreboard API into the latest development builds, and I wrote this to explain how to use it (all the links to the JavaDocs/other documentation are near the end). Although this seems fairly long, please bear with me because I tried to keep it as simple as I could. Hope I can help you!

    Getting the ScoreboardManager (required for creating Scoreboards):
    1. ScoreboardManager manager = Bukkit.getScoreboardManager();

    Creating a Scoreboard with the ScoreboardManager:
    1. Scoreboard board = manager.getNewScoreboard();

    Registering a new Team:
    1. Team team = board.registerNewTeam("teamname");

    What you can do with Teams (the methods in the Team class):
    1. //Adding players
    2. team.addPlayer(player);
    4. //Removing players
    5. team.removePlayer(player);
    7. //Adding prefixes (shows up in player list before the player's name, supports ChatColors)
    8. team.setPrefix("prefix");
    10. //Adding suffixes (shows up in player list after the player's name, supports ChatColors)
    11. team.setSuffix("suffix");
    13. //Setting the display name
    14. team.setDisplayName("display name");
    16. //Making invisible players on the same team have a transparent body
    17. team.setCanSeeFriendlyInvisibles(true);
    19. //Making it so players can't hurt others on the same team
    20. team.setAllowFriendlyFire(false)

    Registering a new Objective (list of all the criteria here, replace "dummy" with the criteria name):
    1. Objective objective = board.registerNewObjective("test", "dummy");

    What you can do with Objectives (the methods in the Objective class):
    1. //Setting where to display the scoreboard/objective (either SIDEBAR, PLAYER_LIST or BELOW_NAME)
    2. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    4. //Setting the display name of the scoreboard/objective
    5. objective.setDisplayName("Display Name");

    Setting Scores for players:
    1. Score score = objective.getScore(player);
    2. score.setScore(42); //Integer only!

    Custom names for scores:
    1. Score score = objective.getScore(ChatColor.GREEN + "Kills:"); //Get a fake offline player
    2. score.setScore(1);

    Using that will show up like this:

    Resetting scores for a player:
    1. board.resetScores(player);
    3. //If you are using custom player names (as shown above), then you have to do this:
    4. board.resetScores(ChatColor.GREEN + "Kills:"); //Same as what I used above, case/color sensitive

    Setting the Scoreboard for a player:
    1. player.setScoreboard(board);

    Removing a player's scoreboard:
    1. player.setScoreboard(manager.getNewScoreboard()); //manager.getNewScoreboard() will return a blank scoreboard

    1. Display a player's health under their name:
    1. ScoreboardManager manager = Bukkit.getScoreboardManager();
    2. Scoreboard board = manager.getNewScoreboard();
    4. Objective objective = board.registerNewObjective("showhealth", "health");
    5. objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
    6. objective.setDisplayName("/ 20");
    8. for(Player online : Bukkit.getOnlinePlayers()){
    9. online.setScoreboard(board);
    10. online.setHealth(online.getHealth()); //Update their health
    11. }

    2. Display a score under a player's name:

    1. ScoreboardManager manager = Bukkit.getScoreboardManager();
    2. Scoreboard board = manager.getNewScoreboard();
    4. Objective objective = board.registerNewObjective("lives", "dummy");
    5. objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
    6. objective.setDisplayName("lives");
    8. for(Player online : Bukkit.getOnlinePlayers()){
    9. Score score = objective.getScore(online);
    10. score.setScore(5); //Example
    11. }
    13. for(Player online : Bukkit.getOnlinePlayers()){
    14. online.setScoreboard(board);
    15. }

    Help! It's not working!
    Most likely you are using a CraftBukkit build that does not support Scoreboards/Teams. If your IDE says that it could not locate any of the classes that I used, then you're not using a build that supports Scoreboards/Teams. Find one here. Also be sure that you are importing the correct classes. Make sure that your imports begin with "org.bukkit.scoreboard" so you will be using the Bukkit classes instead of the NMS classes. If you are positive that the version that you are using is correct and supports these newly implemented features, please reply with the error that you receive and the code you are using to make Scoreboards/Teams and I, along with my fellow Bukkiteers, will do the best to help you.
  2. Nice job on this.

    For Mojang's part this seems kind of half-baked (as compared to a display that is more mod-friendly) but you've explained it very well!
    GrandmaJam and Wizardo367 like this.
  3. Why thank you ;) I thought I'd share what I got out of 2 hours of Googling, coding and testing!
  4. Great tutorial! Now let the rewriting of my plugin begin!
    Wruczek, snake4212 and Sir_Mr_Bman like this.
  5. Thanks :)
  6. One question, how would you remove a player from the scoreboard (after he leaves)?

    EDIT: Figured it out, just scoreboard.resetScores(player)
    Phasesaber likes this.
  7. Well done, just waited for this to use it in my plugins ;)
  8. Added ;)
  9. Offline


    How do i view the scoreboard? :O
    Using this code:
    plugin.board = plugin.manager.getNewScoreboard();
    plugin.redTeam = plugin.board.registerNewTeam("Red Team");
    plugin.blueTeam = plugin.board.registerNewTeam("Blue Team");
    for(Player p:Bukkit.getOnlinePlayers()){
    else if(plugin.red.contains(p)){
  10. fresser123 you have to send it to the player:
  11. Offline


    How does the team transparency work on client side? Does it only make teammates transparent if the players have an invisibility potion on?
  12. Offline


    Thanks for the tutorial, going to use it now!
  13. Yes. For example, player 1 and player 2 are on the same team. When player 2 receives an invisibility potion, player 1 (and all the other players on their team) will see player 2 with a transparent body and a visible name-tag. But if any players that aren't on their team see player 2, it will seem to them like player 2 has a normal invisibility potion.

    Glad I could help :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Oct 30, 2015
  14. Offline


    Great tutorial!
  15. Thanks :)
    Stigosaurus likes this.
  16. Is there a way to hide again the scoreboard?
    player.setScoreboard(null); dont work :3

    Edit: You can hide the scoreboard again if you create a empty scoreboard and set the empty scoreborad to the player.
    Kinda like this:
    Scoreboard emptyBoard = manager.getNewScoreboard();
  17. Spikes try using board.clearSlot(DisplaySlot.<SLOT>). I think that if you use board.resetScores(player), it will show up again when it gets updated.
  18. board.clearSlot will stop display to all player.
    With my method it will only stop display to one player.
  19. chasechocolate
    For criteria, is it possible to use your own value for each player?

    Edit: Figured it out.
  20. So you can customize it? Yes, use "dummy".
    Regablith likes this.
  21. Offline


    I'm sorry for not reading the code thoroughly, nor the JavaDoc, but from what I've skimmed through, I can't tell if the scoreboard updates itself for each player, or you need to constantly need to do player.SetScoreboard()...
  22. For me it's one of the best tutorials I've seen :D

    You don't have. For me it's updating automaticlly.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Oct 30, 2015
    chasechocolate likes this.
  23. stirante
    Did you set it when a listener is fired to update? Or did you just set it once like in onEnable()?
  24. I just created scoreboard, set it to players and changed values. Everything worked greate.
  25. stirante
    I created a scoreboard on my plugins enable. I also did this when the plugin enabled:
        public void manageScoreboard() {
            Objective objective = board.registerNewObjective("Reputation", "dummy");
            for (Player p : Bukkit.getOnlinePlayers()) {
                Score score = objective.getScore(p);
    Will it keep updating even for new players that join?
  26. It have to be set for every new player i think.
  27. AFAIK, no. You will have to listen for PlayerJoinEvent and send them your scoreboard.
  28. chasechocolate quick question. Can this be tailored to one map on a server.
  29. FYI, Health below name doesn't display if nametag color/name is changed. Probably messes up everything else too.
  30. Offline


    Great tutorial but I want to display doubles :(

Share This Page