[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

    Try taking damage, it sometimes need up be updated.
     
  2. Offline

    surtic

    You don't set the Health of the Players....

    Score score = objective.getScore(player);
    score.setScore( player.getHealth() );
     
    ScottGambler likes this.
  3. Offline

    xXSniperzzXx_SD

    chasechocolate
    Do you have any suggestions or ways to fix my problem? Or is this just a bukkit bug.

     
  4. Offline

    ScottGambler

  5. Offline

    chasechocolate

    xXSniperzzXx_SD that's weird because the sidebar works fine for me... It may be a Bukkit bug? What version are you using?
     
  6. Offline

    xXSniperzzXx_SD

    chasechocolate Well i tried the beta version, didnt work so i switched to a dev version 2762

    But i tried using your health example as well and just changed the display, first to the playerlist to make sure it worked, which it did, then i did the sidebar and nothing appeared, i've even force updated my minecraft client to make sure it wasnt something in there



    Edit: Well now its working.... i didn't change anything though so I'm not entirely sure what I did.
     
  7. Offline

    ScottGambler

    If i use the Scoreboards below the name, for show the Health of a player, It won't work if an player has an colored name over the skin. So is there a method that the name is colored and the Scoreboard shows the health , and set the display name colored? :)

    It works, but I needed to delete the colored names of all players..
     
  8. Offline

    uvbeenzaned

    Is there a workaround for score.setScore(42); to be a string instead? I need to be able to display a rank string beside the score? Is there a way to display a suffix for a specific player and not just a team? I would appreciate the help!
     
  9. Offline

    Aqua

    It seems I cant set multiple objects to the board...
    Code:
                 teamA = board.registerNewTeam("TeamA");
                        teamB = board.registerNewTeam("TeamB");
                       
                        teamA.setDisplayName(args[2]);
                        eamB.setDisplayName(args[3]);
                       
                        obj = board.registerNewObjective(teamA.getDisplayName(), "dummy");
                        obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                        objs = board.registerNewObjective("ScoreA", "dummy");
                        objs.setDisplaySlot(DisplaySlot.SIDEBAR);
                       
                        obj2 = board.registerNewObjective(em.teamB.getDisplayName(), "dummy");
                        obj2.setDisplaySlot(DisplaySlot.SIDEBAR);
                        objs2 = board.registerNewObjective("ScoreB", "dummy");
                        objs2.setDisplaySlot(DisplaySlot.SIDEBAR);
     
                        obj.setDisplayName(em.teamA.getDisplayName());
                        scoreA = em.objs.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Kills:"));
                        scoreA.setScore(0);
                       
                        obj2.setDisplayName(em.teamB.getDisplayName());
                        scoreB = em.objs2.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Kills:"));
                        scoreB.setScore(0);
    Variables:
    Code:
        ScoreboardManager manager = null;
        public Scoreboard board = null;
        public Team teamA = null;
        public Team teamB = null;
        public Objective obj = null;
        public Objective objs = null;
        public Objective obj2 = null;
        public Objective objs2 = null;
        public Score scoreA = null;
        public Score scoreB = null;
     
    -----BELOW HERE IS IN ONENABLE()-----
     
        manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
     
    
     
  10. Offline

    chasechocolate

    Aqua no need for creating more than one objective, simply create one objective and set the scores from there.
     
  11. Offline

    bfgbfggf

    chasechocolate
    I have that code but not work... How to fix that? :(
    Code:
                        manager = Bukkit.getScoreboardManager();
                        board = manager.getNewScoreboard();
                        Team team = board.registerNewTeam("Testowe");
                        team.addPlayer(p);
                        team.setDisplayName("display name");
                        Objective objective = board.registerNewObjective("test",
                                "dummy");
                        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                        if (special.isEmpty()) {
                            DisName = players.getString(player + ".NameRank");
                        } else {
                            DisName = players
                                    .getString(player + ".NameSpecialRank");
                        }
                        objective.setDisplayName(ChatColor.DARK_RED + DisName);
                        Score LevelScore = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.GREEN + "Level: "
                                        + ChatColor.BLUE));
                        LevelScore.setScore(lev);
                        Score RankScore = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.GREEN + "RankLevel: "
                                        + ChatColor.BLUE));
                        RankScore.setScore(rank);
                        Score ToRankScore = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.GREEN + "ToNextRank: "
                                        + ChatColor.BLUE));
                        ToRankScore.setScore(levToRank);
                        Score ToLevelScore = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.GREEN + "ToNextLev: "
                                        + ChatColor.BLUE));
                        ToLevelScore.setScore(pointsToLev);
                        Score PointsScore = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.GREEN + "Total: "
                                        + ChatColor.BLUE));
                        PointsScore.setScore(points);
                        Score Nope = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.RED + "========="));
                        Score BonusScore = objective.getScore(Bukkit
                                .getOfflinePlayer(ChatColor.GREEN + "Your Bonus: "
                                        + ChatColor.BLUE + bonus));
     
  12. Offline

    chasechocolate

    bfgbfggf are you ever using player.setScoreboard()?
     
  13. Offline

    bfgbfggf

    chasechocolate
    em.. yes
    At the end of that code. But I test with that, and without. And result is this same.

    EDIT: Ok i find error...
     
  14. Offline

    dmulloy2

    chasechocolate
    In every way I've tried, it sets the display name for every player, not just the individual player... Any way I can do this? I'll provide the code I have if needed.
     
  15. Offline

    chasechocolate

    dmulloy2 make different scoreboard objects for each player.
     
  16. Offline

    bfgbfggf

    chasechocolate

    Can you help my with that code?
    http://pastebin.com/iN6Dr1ZC

    Side bar works fine. Every player has own sidebar.
    But Player list and Below Name don't work ;/

    I want to add on player list some points to every player.
    And below name add This same point + special rank.

    UpdateScore() is for update scoreboard for all players (eg: on reload)
    UpdateScore(String PR) for update only score of one player, PR = nick
    But in my code every player see own stats below every nick.
    And on Player list see only own score. (for others player see 0)

    How to write/fix that?
     
  17. Offline

    dmulloy2

    chasechocolate
    So when updating player health, register a new scoreboard each time? Once I do that do I set that as the scoreboard for all players (<online players>.setScoreboard(<new board>))?
     
  18. Offline

    chasechocolate

    dmulloy2 well there's a built in health criteria so depending on what you want to do, you may not have to ever mess with it. But if not, then just change the score of the objective rather than the whole scoreboard.
     
  19. Offline

    bfgbfggf

    chasechocolate
    Ech I try everything in my code... and still not work.
     
  20. Offline

    dmulloy2

    chasechocolate
    This is what I have so far.. Any glaring mistakes, etc you can see?
    Code:java
    1. public void updateHealth(Player player) throws NoSuchMethodException, IllegalStateException
    2. {
    3. if (plugin.playerhealth == false)
    4. return;
    5.  
    6. if (player.getHealth() > 0)
    7. try
    8. {
    9. ScoreboardManager manager = plugin.getServer().getScoreboardManager();
    10. Scoreboard board = manager.getNewScoreboard();
    11.  
    12. Set<Team> teams = board.getTeams();
    13. for (Team team : teams)
    14. {
    15. if (team.getName().contains("health"))
    16. team.unregister();
    17. }
    18.  
    19. if (board.getObjective(DisplaySlot.BELOW_NAME) != null)
    20. board.getObjective(DisplaySlot.BELOW_NAME).unregister();
    21. if (board.getObjective("showhealth") != null)
    22. board.getObjective("showhealth").unregister();
    23.  
    24. Objective objective = board.registerNewObjective("showhealth", "health");
    25. objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
    26.  
    27. board.registerNewTeam("health0").setDisplayName("");
    28. board.registerNewTeam("health1").setDisplayName("§c❤");
    29. board.registerNewTeam("health2").setDisplayName("§c❤❤");
    30. board.registerNewTeam("health3").setDisplayName("§c❤❤❤");
    31. board.registerNewTeam("health4").setDisplayName("§e❤❤❤❤");
    32. board.registerNewTeam("health5").setDisplayName("§e❤❤❤❤❤");
    33. board.registerNewTeam("health6").setDisplayName("§e❤❤❤❤❤❤");
    34. board.registerNewTeam("health7").setDisplayName("§e❤❤❤❤❤❤❤");
    35. board.registerNewTeam("health8").setDisplayName("§a❤❤❤❤❤❤❤❤");
    36. board.registerNewTeam("health9").setDisplayName("§a❤❤❤❤❤❤❤❤❤");
    37. board.registerNewTeam("health10").setDisplayName("§a❤❤❤❤❤❤❤❤❤❤");
    38.  
    39. final int health = Math.round(player.getHealth() / 2);
    40. Team oldteam = board.getPlayerTeam(player);
    41. if (oldteam != null)
    42. oldteam.removePlayer(player);
    43. board.getTeam("health"+Integer.toString(health)).addPlayer(player);
    44. Score score = objective.getScore(player);
    45. score.setScore(player.getHealth());
    46. objective.setDisplayName(board.getPlayerTeam(player).getDisplayName());
    47. }
    48. catch (Exception e)
    49. {
    50. plugin.outConsole(Level.SEVERE, plugin.getMessage("log_health_error"), e.getMessage());
    51. }
    52. }[syntax][/syntax]
     
  21. Offline

    Deckerz

    Can you make it so that each person has their own personal scoreboard tab?
     
  22. Offline

    Hoolean Retired Staff

    chasechocolate

    Is it persistant? (will scores be saved and reloaded upon a server reload)
     
  23. Offline

    chasechocolate

    Yes. Save it in a HashMap<String, Scoreboard> (String = player name, Scoreboard = scoreboard instance for the player).
    I've heard people saying that the scores are saved into the world folder, but I'm not 100% sure. Might want to test it out :)
     
  24. Offline

    bfgbfggf

  25. Offline

    Vincent1468

    Sorry if this has been asked before but is it possible to keep the items in a specific order? For example I create 3 items, item one should be on top, under item one is item two and under item two is item three. item one has a value of 1, item two a value of 2 and item three a value of 3. Now it puts item three on top, then item two and then item one. Any way to change this?
     
  26. Offline

    Kid

    Hi :)
    I have a problem: when I add a player to a team, the color is added to the player name tag (above the player's head). But when I remove him from the team, the color is still here. Is there a way to fix this?

    EDIT: I made a stupid error, team.removePlayer(p) removes the name tag.

    Thank you!
     
  27. Offline

    CD3

    Any way that this can be used solely to change the color of someone's Over-Head tag?

    or to be more specific, could you just get the name-color changing and skip the score showing?
     
  28. Offline

    Kid

    I don't understand CD3.. But anyway, I made a stupid error, it works now, team.removePlayer(p) removes the color tag.
     
  29. Offline

    Suraski

    Thank's for this tutorial! I actually incorporated this into my minigame network, I like how you don't have to create a new board for each person unlike in the ScoreboardAPI provided on dev. Really a nice interface to work with!
     
  30. Offline

    SnowGears

    Thanks so much man! I was hoping I wouldn't have to use the ScoreBoardAPI plugin as a dependency for my plugin and thanks to you, now I don't have to! I really appreciate it. :)
     
Thread Status:
Not open for further replies.

Share This Page