Is this good?

Discussion in 'Plugin Development' started by spookyDHD, Feb 5, 2014.

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

    spookyDHD

    I'm making a try/catch.
    But is this good?

    Code:java
    1. @EventHandler
    2.  
    3. public void onPlayerDamage(EntityDamageEvent e) {//, final EntityDamageEvent event) {
    4.  
    5. try{
    6.  
    7. Player p = (Player) e.getEntity();
    8. if (e.getEntity() instanceof Player) {
    9. if(e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_ATTACK)) { //Get the attack cause.
    10. for(Entity ent : p.getNearbyEntities(1.5D, 1.5D, 1.5D)) //Check nearby entities for possible damagecause
    11.  
    12.  
    13.  
    14. {
    15. if((ent instanceof Zombie)) // If it equals to a zombie (almost only close range mob which counts as ENTITY_ATTACK)
    16. {
    17. p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 30, 1)); //Add the PotionEffect
    18. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 10, 1)); //Add the PotionEffect
    19. e.setDamage(3.5);
    20. }
    21. catch(Exception ex){
     
  2. Offline

    stirante

  3. Offline

    felixfritz

    try/catch blocks are only for things that go wrong outside your range. If-statements should be the general error-checking part. (also performance-wise it's better to use if-statements)

    Are you aware of what methods could probably lead to unexpected errors and cause an exception?
     
Thread Status:
Not open for further replies.

Share This Page