Java Basics: How would you do that ...

Discussion in 'Plugin Development' started by mgbeenieboy, Apr 21, 2014.

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

    mgbeenieboy

    Hey, I want to display the health of someone, but not in a number, that's too boring. I want a health bar with [###___]. Well... I could make a couple of if-clauses or cases, but isn't there another possibility in java?
     
  2. Offline

    Onlineids

    int percent = (p.getHealth() / p.getMaxhealth()) *100
    if(percent >= 90){
    //9 # out of 10 of whatever your using
    }
     
  3. Offline

    mgbeenieboy

    Uhh... why is this better than

    if(health = 20){
    ##########
    }

    or

    case: 20
    ##########

    I think I gonna do that with cases
     
  4. Offline

    Syd

    mgbeenieboy
    It's better, because you can't assue max health is 20, as well as you can't assume it's an integer.

    Code:java
    1. StringBuilder healthBar = new StringBuilder("[");
    2.  
    3. int sharps = 10 * player.getHealth() / player.getMaxHealth(); //amounts of hearts
    4.  
    5. for(int i = 0; i < 10; i++)
    6. {
    7. if (i < sharps)
    8. healthBar.append("#");
    9. else
    10. healthBar.append("_");
    11. }
    12.  
    13. healthBar.append("]");
    14.  
    15. //now you can get the healthbar String with healthBar.toString()


    This would be the complete code to create such a bar.
     
  5. Offline

    mgbeenieboy

    Eclipse says that getHealth is not usable for the type player (but for Damageable). It allows me to use HealthScale and MaxHealth does not work as well :(
     
  6. Offline

    Syd

    mgbeenieboy
    Sounds like you try to compile with CraftBukkit instead of Bukkit.
     
    CraftedShack likes this.
  7. Offline

    mgbeenieboy

    D: and this is wrong? I always use the craftbukkit.jar drom dl.bukkit.org as my library?!
     
  8. Offline

    Konkz

  9. Offline

    thisguy128512

    Mildly derailing this topic here, but what's wrong with compiling with CraftBukkit, and not Bukkit, if it works? What could go wrong? I can't seem to find anything by Googling.
     
  10. Offline

    mgbeenieboy

    Ok. but getHealth for the type player doesn't exist either. If I use HealthScale instead, in game the command always shows me that i have full health, what's not the case!
     
  11. Offline

    Garris0n

    Yeah, because getHealthScale doesn't get the health. getHealth does. Are you using Bukkit now?
     
  12. Offline

    Onlineids

    Nothing, but using craftbukkit alone makes you unable to do a few things such as in this thread getting player health.
     
  13. Offline

    NonameSL

    This code is good, but isntead of player.getHealth() use ((Damageable)player).getHealth(); (You can use the ♥symbol instead of # and ♡ isntead of _)
     
  14. Offline

    mgbeenieboy

    Yes, I'm using bukkit now. I don't know why, maybe I had to restart eclipse, but now it works :D Thank you very much.
     
Thread Status:
Not open for further replies.

Share This Page