[Tutorial] Scoreboards/Teams with the Bukkit API

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

Thread Status:
Not open for further replies.
  1. Offline

    chasechocolate

    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. Offline

    Ne0nx3r0

    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!
     
  3. Offline

    chasechocolate

    Why thank you ;) I thought I'd share what I got out of 2 hours of Googling, coding and testing!
     
  4. Offline

    molenzwiebel

    Great tutorial! Now let the rewriting of my plugin begin!
     
    Wruczek, snake4212 and Sir_Mr_Bman like this.
  5. Offline

    chasechocolate

    Thanks :)
     
  6. Offline

    molenzwiebel

    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. Offline

    DarkBladee12

    Well done, just waited for this to use it in my plugins ;)
     
  8. Offline

    chasechocolate

    Added ;)
     
  9. Offline

    fresser123

    How do i view the scoreboard? :O
    Using this code:
    Code:
    plugin.board = plugin.manager.getNewScoreboard();
    plugin.redTeam = plugin.board.registerNewTeam("Red Team");
    plugin.blueTeam = plugin.board.registerNewTeam("Blue Team");
     
    plugin.kill.setDisplaySlot(DisplaySlot.PLAYER_LIST);
    plugin.kill.setDisplayName("Kills");
    for(Player p:Bukkit.getOnlinePlayers()){
    p.setScoreboard(plugin.board);
    if(plugin.blue.contains(p)){
    plugin.blueTeam.addPlayer(p);
    }
    else if(plugin.red.contains(p)){
    plugin.redTeam.addPlayer(p);
    }
    }
    
     
  10. Offline

    chasechocolate

    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. Offline

    chasechocolate

    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

    Stigosaurus

    Great tutorial!
     
  15. Offline

    chasechocolate

    Thanks :)
     
    Stigosaurus likes this.
  16. Offline

    Spikes

    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)
     
  17. Offline

    chasechocolate

    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. Offline

    Spikes

    board.clearSlot will stop display to all player.
    With my method it will only stop display to one player.
     
  19. Offline

    iWareWolf

    chasechocolate
    For criteria, is it possible to use your own value for each player?

    Edit: Figured it out.
     
  20. Offline

    chasechocolate

    So you can customize it? Yes, use "dummy".
     
    Regablith likes this.
  21. 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()...
     
  22. Offline

    stirante

    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. Offline

    iWareWolf

    stirante
    Did you set it when a listener is fired to update? Or did you just set it once like in onEnable()?
     
  24. Offline

    stirante

    I just created scoreboard, set it to players and changed values. Everything worked greate.
     
  25. Offline

    iWareWolf

    stirante
    I created a scoreboard on my plugins enable. I also did this when the plugin enabled:
    Code:
        public void manageScoreboard() {
            Objective objective = board.registerNewObjective("Reputation", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("Reputation");
            for (Player p : Bukkit.getOnlinePlayers()) {
                Score score = objective.getScore(p);
                score.setScore(api.getRep(p));
                p.setScoreboard(board);
            }
        }
    Will it keep updating even for new players that join?
     
  26. Offline

    stirante

    It have to be set for every new player i think.
     
  27. Offline

    chasechocolate

    AFAIK, no. You will have to listen for PlayerJoinEvent and send them your scoreboard.
     
  28. Offline

    The_Coder

    chasechocolate quick question. Can this be tailored to one map on a server.
     
  29. Offline

    Tirelessly

    FYI, Health below name doesn't display if nametag color/name is changed. Probably messes up everything else too.
     
  30. Offline

    Aza24

    Great tutorial but I want to display doubles :(
     
Thread Status:
Not open for further replies.

Share This Page