This can be done by using the event EntityTargetEvent
Entities won't target the player if you cancel the event:
@EventHandler
public void onEntityTarget(EntityTargetEvent e) {
Entity target = e.getEntity();
if(target instanceof Player) {
Player playerTargetted = (Player) target;
if (shouldBeInvisible(playerTargetted) {
e.setCancelled(true); //Cancel the target event
}
}
}