scoreboard tricky-ness?

Discussion in 'Plugin Development' started by soulofw0lf, May 14, 2013.

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

    soulofw0lf

    I'm trying to get a score board under players names to show their actual health instead of their hearts, and have their total health as the objective display name. what i ended up with is only the most recent person on the server having their current health shown, and everyone sees their own max health on everyone else's heads. I would love some input as to what i might be doing wrong here? or if this is even possible?
    Code:java
    1.  
    2. @EventHandler
    3. public void onDamage(EntityDamageEvent event){
    4. if(event.getEntity() instanceof Player){
    5. Player p = (Player)event.getEntity();
    6. setScore(p);
    7.  
    8.  
    9. }
    10. }
    11. public static void setScore(Player p){
    12. Team team = null;
    13. for (Player online : Bukkit.getOnlinePlayers()){
    14. String name1 = online.getName();
    15. ScoreboardManager manager = Bukkit.getScoreboardManager();
    16. Scoreboard board = manager.getNewScoreboard();
    17. if (board.getTeam(name1) == null){
    18. team = board.registerNewTeam(name1);
    19. team.addPlayer(online);
    20. }
    21. if (board.getObjective(name1) == null){
    22. board.registerNewObjective(name1, "dummy");
    23. }
    24. board.getObjective(name1).setDisplaySlot(DisplaySlot.BELOW_NAME);
    25. String display = Integer.toString(online.getMaxHealth());
    26. board.getObjective(name1).setDisplayName("ยง2" + display);
    27. Score score = board.getObjective(name1).getScore(online);
    28. score.setScore(online.getHealth());
    29.  
    30. online.setScoreboard(board);
    31. }
    32. }
    33.  

    i call getScore on playerlogin, on level up, and on entity damage event, but only the most recent player to log on has their health update at all. Any pointers?
     
  2. Offline

    BajanAmerican

  3. Offline

    soulofw0lf

    @BajanAmerican unfortunately that tutorial doesn't cover what i'm trying to do at all, and the only thing remotely relevant was a suggestion to set individual goals for different players. I posted on there asking for help but realized that the resources forum was the wrong place to do so, so i posted here :)
     
  4. Offline

    chasechocolate

    soulofw0lf you can create a different scoreboard for each player (saved in a HashMap<String, Scoreboard>) and change data from there.
     
  5. Offline

    soulofw0lf

    @chasechocolate thanks i'll try it out :)

    @chasechocolate well i've been trying for a few hours to get it to work with a hash map and am having the exact same problems, anyway i got about setting a unique scoreboard for a player just makes him see his max health on everyone else.

    example player1 has a max health of 450 and is currently at 45 player 2 has a max health of 20 and is currently at 20 player 3 has a mac health of 180 and is currently at 90

    player one sees player 2 as 20/450 and sees player 3 as 90/450
    player 2 sees player 1 as 45/20 and player 3 as 90 / 20
    player 3 sees player 1 as 45 /180 and player 2 as 20/180

    how it should be
    player 2 and player 3 see player 1 as 45 / 450
    player 1 and player 3 see player 2 as 20 / 20
    player 2 and player 1 see player 3 as 90 / 180

    after a few hours of not getting it to show up right i even switched over to using a code from on here

    Code:java
    1.  
    Code:java
    1.  
    Code:java
    1.  
    2.  
    3. /* Holds the scoreboard for each player. The key is the
    4. * players name and the value is their associated scoreboard. */
    5. private Map<String, Scoreboard> playerScoreboards = new HashMap<String, Scoreboard>();
    6.  
    7. /* constructs new Scoreboard for a player and puts it
    8. * in the HashMap.*/
    9. public void register(Player player) {
    10. Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    11. Objective side = scoreboard.registerNewObjective("side", "dummy");
    12. // Add your items to the sidebar here
    13. side.setDisplaySlot(DisplaySlot.SIDEBAR);
    14.  
    15. Objective list = scoreboard.registerNewObjective("list", "dummy");
    16. // Add all the items for the player list here
    17. list.setDisplaySlot(DisplaySlot.PLAYER_LIST);
    18.  
    19. Objective name = scoreboard.registerNewObjective("name", "dummy");
    20. // Add all the items for below player names
    21. name.setDisplaySlot(DisplaySlot.BELOW_NAME);
    22. String mhealth = Integer.toString(player.getMaxHealth());
    23. name.setDisplayName(mhealth);
    24.  
    25. playerScoreboards.put(player.getName(), scoreboard);
    26. player.setScoreboard(scoreboard);
    27. }
    28.  
    29. /* Call this from the player quit event. If you don't it will
    30. * cause nasty memory leaks! */
    31. public void unregister(String player) {
    32. playerScoreboards.remove(player);
    33. }
    34.  
    35. /* This method will change a value for all players registered. */
    36. public void changeAll(DisplaySlot slot, OfflinePlayer scoreName, int value ) {
    37. for(Scoreboard current : playerScoreboards.values()) {
    38. current.getObjective(slot).getScore(scoreName).setScore(value);
    39. }
    40. }
    41.  
    42. /* This will change a score for a specific player. */
    43. public void changeSingle(String player, DisplaySlot slot, OfflinePlayer scoreName, int value ) {
    44. if(playerScoreboards.containsKey(player)) {
    45. Scoreboard scoreboard = playerScoreboards.get(player);
    46. scoreboard.getObjective(slot).getScore(scoreName).setScore(value);
    47. }
    48.  
    49. }
    50. [SIZE=13px][FONT=Trebuchet MS][/FONT][/SIZE]

    same problem with this one as well as the hash maps i tried myself. Is this actually possible? or am i wasting my time beating my head against walls?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page