0

FIXED

I am attempting to add a health bar next to my entities name, like:

Level 3 Skeleton ||||

Where bars are how much health, out of 5. I have tried everything it seems, but I cannot figure it out! I think its really simple, but I just can't get it...

@EventHandler
public void entityDamageEntity(EntityDamageEvent event) {
    LivingEntity entity = (LivingEntity) event.getEntity();
    if (entity.getCustomName() != null) {
        entity.setCustomName(entity.getCustomName().replaceAll("|", ""));
        int health = (int) Math.ceil(entity.getHealth() / entity.getMaxHealth() * 5);
        int i = 1;
        String healthbar = " |";
        while(i < health){
            i++;
            healthbar = healthbar + "|";
        }
        entity.setCustomName(entity.getCustomName() + healthbar);
    }
}

I just cannot seem to get it to work! It does weird things, try using it with a named entity. If someone could point out the error, that would be great =D

https://i.stack.imgur.com/RYdcI.png

FIXED CODE:

@EventHandler
public void entityDamageEntity(EntityDamageEvent event) {
    LivingEntity entity = (LivingEntity) event.getEntity();
    if (entity.getCustomName() != null) {
        entity.setCustomName(entity.getCustomName().replaceAll("\\|", ""));
        int health = (int) ((float) entity.getHealth() / entity.getMaxHealth() *5);
        if (health > 0){
            char[] bars = new char[health + 1];
            Arrays.fill(bars, '|');
            entity.setCustomName(entity.getCustomName() + " " + new String(bars));
            entity.setCustomName(entity.getCustomName().replaceAll("  ", " "));
        } else {
            entity.setCustomName(entity.getCustomName()); 
            entity.setCustomName(entity.getCustomName().replaceAll("  ", " "));
        }
    }
}
Paul
  • 21
  • 3

2 Answers2

1

I see an immediate problem here. | is a special character in regex, so you should escape this character.

Try:

entity.setCustomName(entity.getCustomName().replaceAll("\\|", ""));
Lai Xin Chu
  • 2,462
  • 15
  • 29
  • Thanks you! I noticed it wasn't removing the bars, forgot it was a special character. – Paul May 03 '13 at 06:02
1

So without going into the game, you have 1 major problem with the code (you are dividing two integers which is going to give you 0), and then an efficiency issue with appending strings. Fixing the first

int health = (int) ((float) entity.getHealth() / entity.getMaxHealth() *5);

What you want to do now is append 0 through 5 health bars. The following will create an array of 1 through 5 '|'. It is more efficient than a while loop as it just creates the needed array size directly, as opposed to using appending.

if (health > 0){
    char[] bars = new char[health];
    Arrays.fill(bars, '|');
    entity.setCustomName(entity.getCustomName()+" " + new String(bars));
} else {
    entity.setCustomName(entity.getCustomName()); // no bars to add
}
greedybuddha
  • 7,488
  • 3
  • 36
  • 50
  • Thank you for figuring this out. – Paul May 03 '13 at 06:07
  • Yep. I had a problem with the `entity.setCustomName(entity.getCustomName()+" " + new String(bars));` Adding a space inbetween the entities name and the bars, so after that, I added: `entity.setCustomName(entity.getCustomName().replaceAll(" ", " "));` – Paul May 03 '13 at 06:19