Set custom display name as Health?

Discussion in 'Plugin Development' started by shohouku, May 6, 2014.

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

    shohouku

    How would I set a custom display name as health and update it everytime the entity/player gets damaged?

    I'v tried this:

    Code:java
    1. pig.setCustomName("Health: " + pig.getHealth());


    I knew it wouldn't work because I needed to take a -1 everytime it got hit.


    Any ideas?
     
  2. shohouku Inside the EntityDamageEvent, check if the entity is one of the entities you want to display health for. Then, you can call the setCustomName() in there. You might need to do getHealth() - event.getDamage()
     
  3. Offline

    shohouku


    This is my full code:

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent e) {
    3. e.getEntityType();
    4. final Pig pig = (Pig)e.getEntity();
    5. if(e.getEntity() instanceof Pig) {
    6. if(pig.getCustomName().equalsIgnoreCase("Health: " + pig.getHealth())) {
    7. pig.setCustomName("Stupid Pig [Lvl ??]");
    8. if(pig.getCustomName().equalsIgnoreCase("Stupid Pig [Lvl ??]")) {
    9.  
    10.  
    11. pig.setCustomName("Health: " + pig.getHealth());
    12. }
    13. }
    14.  
    15.  
    16.  
    17. }
    18.  
    19. }
     
  4. Offline

    minoneer

    Well, it looks like your code got a bit mixed up.

    Line 3 of your code does nothing.
    In line 4 you cast to a pig without checking if it actually is one beofre
    In line 5 you check if it is a Pig - that should be before the cast
    The check in line 6 will most likely be false, since the health is now lower than when you set the display name - use .startsWith or similar instead
    line 8 will always be true, since you set the exact name you check for in the line before. I assume you are missing some closing brackets there.

    Try cleaning and organising that code - this way it will never work.
     
  5. Offline

    shohouku


    Okay, I updated my code:

    .setCustomName doesn't get updated. Events are all registered and I have listeners.

    Code:java
    1. @EventHandler
    2. public void damaged(EntityDamageEvent devent){
    3. Damageable ddent = (Damageable)devent.getEntity();
    4. LivingEntity dlent = (LivingEntity)devent.getEntity();
    5. if(ddent.getHealth() < ddent.getMaxHealth()){
    6. String name = dlent.getCustomName();
    7. int percent = (int) (dlent.getMaxHealth()/100 * dlent.getHealth());
    8. if(percent >=99){
    9. if(dlent.hasMetadata(name)){
    10. String named = dlent.getCustomName();
    11. dlent.setCustomName(named);
    12. }
    13. }
    14. if(percent >= 90 && percent < 99){
    15. dlent.setCustomName(ChatColor.GREEN + "|||||||||" + ChatColor.GRAY + "|");
    16. dlent.setCustomNameVisible(true);
    17. }
    18. if(percent >= 80 && percent < 90){
    19. dlent.setCustomName(ChatColor.GREEN + "||||||||" + ChatColor.GRAY + "||");
    20. dlent.setCustomNameVisible(true);
    21. }
    22. if(percent >= 70 && percent < 80){
    23. dlent.setCustomName(ChatColor.GREEN + "|||||||" + ChatColor.GRAY + "|||");
    24. dlent.setCustomNameVisible(true);
    25. }
    26. if(percent >= 60 && percent < 70){
    27. dlent.setCustomName(ChatColor.YELLOW + "||||||" + ChatColor.GRAY + "||||");
    28. dlent.setCustomNameVisible(true);
    29. }
    30. if(percent >= 50 && percent < 60){
    31. dlent.setCustomName(ChatColor.YELLOW + "|||||" + ChatColor.GRAY + "|||||");
    32. dlent.setCustomNameVisible(true);
    33. }
    34. if(percent >= 40 && percent < 50){
    35. dlent.setCustomName(ChatColor.YELLOW + "||||" + ChatColor.GRAY + "||||||");
    36. dlent.setCustomNameVisible(true);
    37. }
    38. if(percent >= 30 && percent < 40){
    39. dlent.setCustomName(ChatColor.RED + "|||" + ChatColor.GRAY + "|||||||");
    40. dlent.setCustomNameVisible(true);
    41. }
    42. if(percent >= 20 && percent < 30){
    43. dlent.setCustomName(ChatColor.RED + "||" + ChatColor.GRAY + "||||||||");
    44. dlent.setCustomNameVisible(true);
    45. }
    46. if(percent >= 10 && percent < 20){
    47. dlent.setCustomName(ChatColor.RED + "|" + ChatColor.GRAY + "||||||||");
    48. dlent.setCustomNameVisible(true);
    49. }
    50.  
    51. }
    52. }
    53.  
     
  6. Offline

    minoneer

    First Thing I noticed is your percentage calculation is wrong:





    • int percent = (int) (100/dlent.getMaxHealth() * dlent.getHealth());



     
  7. Offline

    shohouku

    Thought so I was getting errors on it...

    Any other way how to use another calculation that will actual work? o.o
     
  8. Offline

    minoneer

    I already fixed it... compare yours with the one I postet.
     
  9. Offline

    shohouku


    oh oops didn't see haha,

    anyways I don't get an error anymore but the custom names aren't showing up.

    Could it still be the percent?
     
  10. Offline

    minoneer

    I did 2 or 3 small changes. Now I think the code should run, although I haven't testet it myself.

    If it still doesn't work - do you register your listener propperly? is the plugin enabled?

    Code:java
    1. public class Main
    2. {
    3. @EventHandler
    4. public void damaged(EntityDamageEvent event)
    5. {
    6. if (event.getEntity() instanceof LivingEntity)
    7. {
    8. LivingEntity ent = (LivingEntity) event.getEntity();
    9.  
    10. if (ent.getHealth() < ent.getMaxHealth())
    11. {
    12. String name = ent.getCustomName();
    13. int percent = (int) (100 / ent.getMaxHealth() * ent.getHealth());
    14.  
    15. if (percent >= 99)
    16. {
    17. //Not sure what this is supposed to do - just get the name and set it again?
    18. //Then you can just remove it.
    19. if (ent.hasMetadata(name))
    20. {
    21. String named = ent.getCustomName();
    22. ent.setCustomName(named);
    23. }
    24. }
    25. if (percent >= 90 && percent < 99)
    26. {
    27. ent.setCustomName(ChatColor.GREEN + "|||||||||" + ChatColor.GRAY + "|");
    28. ent.setCustomNameVisible(true);
    29. }
    30. if (percent >= 80 && percent < 90)
    31. {
    32. ent.setCustomName(ChatColor.GREEN + "||||||||" + ChatColor.GRAY + "||");
    33. ent.setCustomNameVisible(true);
    34. }
    35. if (percent >= 70 && percent < 80)
    36. {
    37. ent.setCustomName(ChatColor.GREEN + "|||||||" + ChatColor.GRAY + "|||");
    38. ent.setCustomNameVisible(true);
    39. }
    40. if (percent >= 60 && percent < 70)
    41. {
    42. ent.setCustomName(ChatColor.YELLOW + "||||||" + ChatColor.GRAY + "||||");
    43. ent.setCustomNameVisible(true);
    44. }
    45. if (percent >= 50 && percent < 60)
    46. {
    47. ent.setCustomName(ChatColor.YELLOW + "|||||" + ChatColor.GRAY + "|||||");
    48. ent.setCustomNameVisible(true);
    49. }
    50. if (percent >= 40 && percent < 50)
    51. {
    52. ent.setCustomName(ChatColor.YELLOW + "||||" + ChatColor.GRAY + "||||||");
    53. ent.setCustomNameVisible(true);
    54. }
    55. if (percent >= 30 && percent < 40)
    56. {
    57. ent.setCustomName(ChatColor.RED + "|||" + ChatColor.GRAY + "|||||||");
    58. ent.setCustomNameVisible(true);
    59. }
    60. if (percent >= 20 && percent < 30)
    61. {
    62. ent.setCustomName(ChatColor.RED + "||" + ChatColor.GRAY + "||||||||");
    63. ent.setCustomNameVisible(true);
    64. }
    65. if (percent >= 10 && percent < 20)
    66. {
    67. ent.setCustomName(ChatColor.RED + "|" + ChatColor.GRAY + "||||||||");
    68. ent.setCustomNameVisible(true);
    69. }
    70. }
    71. }
    72. }
     
  11. Offline

    shohouku



    It works but the name doesn't get set when I hit the entity more then 5+ times?w
     
  12. Offline

    minoneer

    What's the last "name" that shows on your entity?
     
  13. Offline

    shohouku


    Nvm i was setting an entities health in my other class to 5000D

    Ill check if it works now, ill notify you if it doesn't work :)

    @minoneer

    Okay, this code isn't working very well..

    Do I have to use a scheduler for this?
    Code:java
    1. if (ent.hasMetadata(name))
    2. {
    3. String named = ent.getCustomName();
    4. ent.setCustomName(named);
    5. }


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

    minoneer

    No, you don't need to use the scheduler. What are you trying to do with that code? Right now it isn't doing anything.
     
  15. Offline

    Garris0n

    Code:java
    1. StringBuilder builder = new StringBuilder(); //create a string builder
    2. for(int i = 0; i < pig.geatHealth(); i++) //loop for each health point
    3. builder.append(" |"); //append a pipe symbol
    4.  
    5. String name = builder.toString().trim(); //trim to get rid of possible white space from the first iteration of the loop


    If statement spam is never healthy ;)
     
    shohouku likes this.
Thread Status:
Not open for further replies.

Share This Page