How do I make a Scoreboard that shows per-player stats?

Discussion in 'Plugin Development' started by gabrielmaennl55, May 18, 2014.

Thread Status:
Not open for further replies.
  1. TGRHavoc
    Ive never really used an EntityDamagedEvent so how do i select the player?
     
  2. Offline

    TGRHavoc

    gabrielmaennl555
    If the entity damaged is an instace of Player, update their scoreboard..
     
    gabrielmaennl555 likes this.
  3. TGRHavoc
    like when i do this i get errors:
    Code:java
    1. @EventHandler
    2. public void scoreboardUpdate(EntityDamageEvent event){
    3.  
    4. Score enemyHealth = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:"));
    5. enemyHealth.setScore((int) player.getHealth());
    6.  
    7.  
    8.  
    9. }


    Ok ill do that

    TGRHavoc
    Im getting an error here at the "obj" part I know i need to get the scoreboard or something how would i do that?
    Code:java
    1. @EventHandler
    2. public void scoreboardUpdate(EntityDamageEvent event){
    3.  
    4. if (event instanceof Player){
    5. Player player = (Player)event;
    6.  
    7. Score enemyHealth = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:"));
    8. enemyHealth.setScore((int) player.getHealth());
    9.  
    10. }
    11.  
    12. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    TGRHavoc

  5. Offline

    TGRHavoc

  6. TGRHavoc
    Ok also 1 more thing with the "obj" error
    do i like need do a Scoreboard scoreboard = or something? or like an objective? like
    in other words can i have an example?
     
  7. Offline

    TGRHavoc

    gabrielmaennl555
    Give me a couple of mins....

    gabrielmaennl555
    Some psuedo code:
    Note: It WILL need to be changed.
    Code:java
    1. HashMap<String, Scoreboad> playerSb = new HashMap<String,Scoreboard>(); // Create hashmap to save player name and scoreboard
    2.  
    3. public void onJoin<PlayerJoinEvent e){
    4. Player player = e.getPlayer(); //Save the player
    5.  
    6. ScoreboardManager manager = Bukkit.getScoreboardManager();
    7. board = manager.getNewScoreboard();
    8. Objective objective = board.registerNewObjective("health", "dummy");
    9. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    10. objective.setDisplayName("Health");
    11. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Health:")); //Get a fake offline player15.
    12. score.setScore((int) player.getHealth());
    13. player.setScoreboard(board);
    14. playeSb.put(player.getName(), board);
    15. }
    16.  
    17. public void damage(EntityDamagedEvent e){
    18. if (e.getEntity() instanceof Player){
    19. Player player = (Player)e.getEntity();
    20. Scoreboard board = playerSb.get(player.getName());
    21. Objective obj = board.getObjective("health");
    22. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Health:")); //Get a fake offline player15.
    23. score.setScore((int) player.getHealth()); //Update score
    24. player.setScoreboard(board); // Update what the player sees
    25. playeSb.put(player.getName(), board);
    26.  
    27. }else{
    28. return;
    29. }
    30. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  8. TGRHavoc
    Thanks so much i really appreciate your help! ;)

    TGRHavoc
    I did this and it dosnt work
    Code:java
    1. public void onJoin(PlayerJoinEvent e){
    2. Player player = e.getPlayer(); //Save the player
    3.  
    4. ScoreboardManager manager = Bukkit.getScoreboardManager();
    5. Scoreboard board = manager.getNewScoreboard();
    6. Objective objective = board.registerNewObjective("health", "dummy");
    7. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    8. objective.setDisplayName("Health");
    9. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Health:")); //Get a fake offline player15.
    10. score.setScore((int) player.getHealth());
    11. player.setScoreboard(board);
    12. playerSb.put(player.getName(), board);
    13. }
    14.  
    15. public void damage(EntityDamageEvent e){
    16. if (e.getEntity() instanceof Player){
    17. Player player = (Player)e.getEntity();
    18. Scoreboard board = playerSb.get(player.getName());
    19. Objective obj = board.getObjective("health");
    20. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Health:")); //Get a fake offline player15.
    21. score.setScore((int) player.getHealth()); //Update score
    22. player.setScoreboard(board); // Update what the player sees
    23. playerSb.put(player.getName(), board);
    24.  
    25. }else{


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  9. Offline

    TGRHavoc

    gabrielmaennl555
    Which bit doesn't work? Is it throwing an error in the console?
     
  10. TGRHavoc
    Theres no errors at all

    No scoreboard pops up & i don't have any other scoreboard plugin on the server

    TGRHavoc
    I printed a logger message when the player joins
    i don't think the actual code is reading because no message came

    TGRHavoc
    wow i feel so dumb... i forgot to put an @EventHandler... it works now :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  11. Offline

    Maurdekye

    gabrielmaennl555 to update the scoreboard on health changes, listen during an EntityDamageEvent, and check if the recipient is a player. If so, then update their score with their new health.
    Do the same with any mobs you have.
     
  12. Maurdekye TGRHavoc
    Thanks you guys XD ;D this is the finishing WORKING Code of Health! Imma try to add EnemyHealth now
    Code:java
    1. //SCOREBOARD HANDLER=======================================================
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent e){
    4. Player player = e.getPlayer(); //Save the player
    5.  
    6. ScoreboardManager manager = Bukkit.getScoreboardManager();
    7. Scoreboard board = manager.getNewScoreboard();
    8. Objective objective = board.registerNewObjective("health", "dummy");
    9. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    10. objective.setDisplayName(ChatColor.GREEN + "SP");
    11. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    12. score.setScore((int) player.getHealth());
    13. player.setScoreboard(board);
    14. playerSb.put(player.getName(), board);
    15. }
    16. @EventHandler
    17. public void damage(EntityDamageEvent e){
    18. if (e.getEntity() instanceof Player){
    19. Player player = (Player)e.getEntity();
    20. Scoreboard board = playerSb.get(player.getName());
    21. Objective obj = board.getObjective("health");
    22. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    23. score.setScore((int) player.getHealth()); //Update score
    24. player.setScoreboard(board); // Update what the player sees
    25. playerSb.put(player.getName(), board);
    26.  
    27. }
    28. }
    29. @EventHandler
    30. public void move(EntityRegainHealthEvent e){
    31. if (e.getEntity() instanceof Player){
    32. Player player = (Player)e.getEntity();
    33. Scoreboard board = playerSb.get(player.getName());
    34. Objective obj = board.getObjective("health");
    35. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    36. score.setScore((int) player.getHealth()); //Update score
    37. player.setScoreboard(board); // Update what the player sees
    38. playerSb.put(player.getName(), board);
    39. }
    40. }


    Maurdekye TGRHavoc
    1 more question how do i make this scoreboard not break the plugin "Health Bars" scoreboard?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  13. Offline

    TGRHavoc

    gabrielmaennl555 likes this.
  14. Offline

    tommyhoogstra

    God this was frustrating to read through.
     
  15. Offline

    gabrielmaennl55

    tommyhoogstra

    XD

    TGRHavoc


    Also one small question if anyone can answer
    How would I make the scoreboard update the health like Evry second?
    The way it is now is a little glitchy if anyone could tell me thanks!

    Also how do I get those symbols like the "Heart" symbol <3 ❤️
    Those

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  16. Offline

    TGRHavoc

    gabrielmaennl555 likes this.
  17. Offline

    NoChanceSD

  18. TGRHavoc
    how would i make an entity damage event where when a player hits a mob or player it will get THEIR health and put it next to "enemy health:"
     
  19. Offline

    TGRHavoc

    gabrielmaennl555
    Check if the "damager" is an instance of a player. If they are, add an offline player called "Enemy Health" to the scoreboard. Update the new players' score to the entity damaged health..
     
    gabrielmaennl555 likes this.
  20. Offline

    mrkirby153

    gabrielmaennl555

    First of all, stop bumping your posts (Replying to them). There's an EDIT button for a reason.

    Secondly, TGRHavoc

    IMO, people shouldn't be spoon-fed code. gabrielmaennl555 Havoc gave you everything you need to know to get it working. You just need to think a little about it.
     
  21. mrkirby153
    Im not bumping my posts
    If your going to talk here it has to be for the topic of "how do i make scoreboard that shows per-player stats"
    You don't just come here and criticise

    TGRHavoc
    I found a bug if anyone knows how to fix it I've trued evrythig
    Like when a player gets damaged it says the Health is 19 but really its 20
    and it sometimes does it with all the other health so its inaccurate
    if someone could help me thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  22. Offline

    mrkirby153

    gabrielmaennl555

    Okay, so maybe I was being a little too critical. Forgive me on that. And what is the current code that you are running?
     
    gabrielmaennl555 likes this.
  23. mrkirby153
    I forgive you! ;D
    my current code:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e){
    3. Player player = e.getPlayer(); //Save the player
    4.  
    5. ScoreboardManager manager = Bukkit.getScoreboardManager();
    6. Scoreboard board = manager.getNewScoreboard();
    7. Objective objective = board.registerNewObjective("health", "dummy");
    8. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    9. objective.setDisplayName(ChatColor.GREEN + "Stats");
    10. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    11. score.setScore((int) player.getHealth());
    12. player.setScoreboard(board);
    13. playerSb.put(player.getName(), board);
    14. }
    15. @EventHandler
    16. public void damage(EntityDamageEvent e){
    17. if (e.getEntity() instanceof Player){
    18. Player player = (Player)e.getEntity();
    19. Scoreboard board = playerSb.get(player.getName());
    20. Objective obj = board.getObjective("health");
    21. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    22. score.setScore((int) player.getHealth()); //Update score
    23. player.setScoreboard(board); // Update what the player sees
    24. playerSb.put(player.getName(), board);
    25.  
    26. }
    27. }
    28. @EventHandler
    29. public void health(EntityRegainHealthEvent e){
    30. if (e.getEntity() instanceof Player){
    31. Player player = (Player)e.getEntity();
    32. Scoreboard board = playerSb.get(player.getName());
    33. Objective obj = board.getObjective("health");
    34. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    35. score.setScore((int) player.getHealth()); //Update score
    36. player.setScoreboard(board); // Update what the player sees
    37. playerSb.put(player.getName(), board);
    38. }
    39. }
    40. @EventHandler
    41. public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent event)
    42. {
    43. ItemStack is = event.getItem();
    44. if(!is.getType().equals(Material.POTION));
    45. Player player = (Player) event.getPlayer();
    46. Scoreboard board = playerSb.get(player.getName());
    47. Objective obj = board.getObjective("health");
    48. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    49. score.setScore((int) player.getHealth()); //Update score
    50. player.setScoreboard(board); // Update what the player sees
    51. playerSb.put(player.getName(), board);
    52. }
    53. @EventHandler
    54. public void onPlayerInteractEvent(PlayerInteractEvent event)
    55. {
    56. ItemStack is = event.getPlayer().getItemInHand();
    57. if(!event.getAction().equals(Action.RIGHT_CLICK_AIR) && !event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    58. return;
    59. if(!is.getType().equals(Material.POTION));
    60. Player player = (Player) event.getPlayer();
    61. Scoreboard board = playerSb.get(player.getName());
    62. Objective obj = board.getObjective("health");
    63. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    64. score.setScore((int) player.getHealth()); //Update score
    65. player.setScoreboard(board); // Update what the player sees
    66. playerSb.put(player.getName(), board);
    67. }
    68. @EventHandler
    69. public void tepeport(PlayerTeleportEvent event)
    70. {
    71. Player player = (Player) event.getPlayer();
    72. Scoreboard board = playerSb.get(player.getName());
    73. Objective obj = board.getObjective("health");
    74. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    75. score.setScore((int) player.getHealth()); //Update score
    76. player.setScoreboard(board); // Update what the player sees
    77. playerSb.put(player.getName(), board);
    78. }
    79. @EventHandler
    80. public void respawn(PlayerRespawnEvent event)
    81. {
    82. Player player = (Player) event.getPlayer();
    83. Scoreboard board = playerSb.get(player.getName());
    84. Objective obj = board.getObjective("health");
    85. Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Health:")); //Get a fake offline player15.
    86. score.setScore(20); //Update score
    87. player.setScoreboard(board); // Update what the player sees
    88. playerSb.put(player.getName(), board);
    89. }
     
  24. Offline

    Gater12

  25. Gater12
    Im making the scoreboard with dummy BECAUSE I'm making a scoreboard that will only show your health and your enemys health and later imma add more to it
    it won't work though with the health criteria because it puts your name there and your health when i want it to be like
    "Stats"
    Health: (health)

    instead of
    "Stats"
    (username) (health)
     
Thread Status:
Not open for further replies.

Share This Page