java - Bukkit - Health in name -


fixed

i attempting add health bar next entities name, like:

level 3 skeleton ||||

where bars how health, out of 5. have tried seems, cannot figure out! think simple, can't 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 = 1;         string healthbar = " |";         while(i < health){             i++;             healthbar = healthbar + "|";         }         entity.setcustomname(entity.getcustomname() + healthbar);     } } 

i cannot seem work! weird things, try using named entity. if point out error, great =d

http://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("  ", " "));         }     } } 

so without going game, have 1 major problem code (you dividing 2 integers going give 0), , efficiency issue appending strings. fixing first

int health = (int) ((float) entity.gethealth() / entity.getmaxhealth() *5); 

what want append 0 through 5 health bars. following create array of 1 through 5 '|'. more efficient while loop creates needed array size directly, opposed 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 add } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -