[Tutorial] Scoreboards/Teams with the Bukkit API

Discussion in 'Resources' started by chasechocolate, Apr 6, 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):
    Code:java
    1. ScoreboardManager manager = Bukkit.getScoreboardManager();

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

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

    What you can do with Teams (the methods in the Team class):
    Code:java
    1. //Adding players
    2. team.addPlayer(player);
    3.  
    4. //Removing players
    5. team.removePlayer(player);
    6.  
    7. //Adding prefixes (shows up in player list before the player's name, supports ChatColors)
    8. team.setPrefix("prefix");
    9.  
    10. //Adding suffixes (shows up in player list after the player's name, supports ChatColors)
    11. team.setSuffix("suffix");
    12.  
    13. //Setting the display name
    14. team.setDisplayName("display name");
    15.  
    16. //Making invisible players on the same team have a transparent body
    17. team.setCanSeeFriendlyInvisibles(true);
    18.  
    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):
    Code:java
    1. Objective objective = board.registerNewObjective("test", "dummy");

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

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

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

    Using that will show up like this:
    [​IMG]

    Resetting scores for a player:
    Code:java
    1. board.resetScores(player);
    2.  
    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:
    Code:java
    1. player.setScoreboard(board);

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

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

    Picture:
    [​IMG]
    2. Display a score under a player's name:

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


    Documentation:
    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

    fresser123

    How do i view the scoreboard? :O
    Using this code:
    Code (Text):
    1.  
    2. plugin.board = plugin.manager.getNewScoreboard();
    3. plugin.redTeam = plugin.board.registerNewTeam("Red Team");
    4. plugin.blueTeam = plugin.board.registerNewTeam("Blue Team");
    5.  
    6. plugin.kill.setDisplaySlot(DisplaySlot.PLAYER_LIST);
    7. plugin.kill.setDisplayName("Kills");
    8. for(Player p:Bukkit.getOnlinePlayers()){
    9. p.setScoreboard(plugin.board);
    10. if(plugin.blue.contains(p)){
    11. plugin.blueTeam.addPlayer(p);
    12. }
    13. else if(plugin.red.contains(p)){
    14. plugin.redTeam.addPlayer(p);
    15. }
    16. }
    17.  
     
  10. fresser123 you have to send it to the player:
     
  11. Offline

    MrTwiggy

    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

    xWatermelon

    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.
     
  14. Glad I could help :)
     
    xWatermelon likes this.
  15. Offline

    Stigosaurus

    Great tutorial!
     
  16. Thanks :)
     
    Stigosaurus likes this.
  17. 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();
    player.setScoreboard(emptyBoard)
     
  18. 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.
     
  19. board.clearSlot will stop display to all player.
    With my method it will only stop display to one player.
     
  20. chasechocolate
    For criteria, is it possible to use your own value for each player?

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

    ohtwo

    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()...
     
  23. For me it's one of the best tutorials I've seen :D
     
    chasechocolate likes this.
  24. You don't have. For me it's updating automaticlly.
     
  25. stirante
    Did you set it when a listener is fired to update? Or did you just set it once like in onEnable()?
     
  26. I just created scoreboard, set it to players and changed values. Everything worked greate.
     
  27. stirante
    I created a scoreboard on my plugins enable. I also did this when the plugin enabled:
    Code (Text):
    1.     public void manageScoreboard() {
    2.         Objective objective = board.registerNewObjective("Reputation", "dummy");
    3.         objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    4.         objective.setDisplayName("Reputation");
    5.         for (Player p : Bukkit.getOnlinePlayers()) {
    6.             Score score = objective.getScore(p);
    7.             score.setScore(api.getRep(p));
    8.             p.setScoreboard(board);
    9.         }
    10.     }
    Will it keep updating even for new players that join?
     
  28. It have to be set for every new player i think.
     
  29. AFAIK, no. You will have to listen for PlayerJoinEvent and send them your scoreboard.
     
  30. chasechocolate quick question. Can this be tailored to one map on a server.
     

Share This Page