Code: public String damageCauseToString(DamageCause cause){ if(cause.toString().equalsIgnoreCase(DamageCause.BLOCK_EXPLOSION.toString())){//if explosion by tnt return "Died from an explosion"; } else if(cause.toString().equalsIgnoreCase(DamageCause.DROWNING.toString())){//drowned in water return "Drowned to death"; } else if(cause.toString().equalsIgnoreCase(DamageCause.ENTITY_ATTACK.toString())){//killed by some entity return "Was brutally attacked"; } else if(cause.toString().equalsIgnoreCase(DamageCause.ENTITY_EXPLOSION.toString())){//killed by creeper return "Got nerfed by a creeper"; } else if(cause.toString().equalsIgnoreCase(DamageCause.FALL.toString())){//killed by fall damage return "Fell to there death"; } else if(cause.toString().equalsIgnoreCase(DamageCause.FIRE.toString())){//killed by inital fire return "Was burnt to death"; } else if(cause.toString().equalsIgnoreCase(DamageCause.FIRE_TICK.toString())){//killed by lingering return "Got smoked"; } else if(cause.toString().equalsIgnoreCase(DamageCause.LAVA.toString())){//killed by lava return "Went swimming in magma"; } else if(cause.toString().equalsIgnoreCase(DamageCause.LIGHTNING.toString())){//killed by lightning strike return "Was electrocuted"; } else if(cause.toString().equalsIgnoreCase(DamageCause.SUFFOCATION.toString())){//killed by suffocation (inside a block) return "Suffocated"; } else if(cause.toString().equalsIgnoreCase(DamageCause.VOID.toString())){//killed by going below the map return "Fell into the void"; } else if(cause.toString().equalsIgnoreCase(DamageCause.CONTACT.toString())){//killed by some type of contact return "Was squished"; } else if(cause.toString().equalsIgnoreCase(DamageCause.CUSTOM.toString())){//killed by some other know reason return "Strangely died"; }else{//if no specific reason, just died, maybe a plugin set health to 0? return "Died"; } }
Could also do (cause == DamageCause.VOID) or just use a switch... Code:java public String damageCauseToString(DamageCause cause) { switch (cause) { case BLOCK_EXPLOSION: //if explosion by tnt return "Died from an explosion"; case DROWNING: //drowned in water return "Drowned to death";*SNIP* }}
Epic necro! To add some content to this post: using '==' for comparison against enums is to be preferred over .equals(), becuase it has better compile-time checking, won't crash if one of the terms is null, and is (slightly) faster.