Method: DamageCause to string

Discussion in 'Resources' started by Tster, Aug 3, 2011.

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

    Tster

    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";
            }
        }
     
  2. cant you do cause.equals(DamageCause.VOID) or something, thats faster
     
  3. Offline

    Coryf88

    Could also do (cause == DamageCause.VOID) or just use a switch...
    Code:java
    1. public String damageCauseToString(DamageCause cause) {
    2. switch (cause) {
    3. case BLOCK_EXPLOSION: //if explosion by tnt
    4. return "Died from an explosion";
    5. case DROWNING: //drowned in water
    6. return "Drowned to death";
    7. *SNIP*
    8. }
    9. }
    10.  
     
  4. Offline

    desht

    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.
     
    TigerHix likes this.
Thread Status:
Not open for further replies.

Share This Page