event.getEntity() problems.

Discussion in 'Plugin Development' started by but2002, Mar 14, 2011.

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

    but2002

    Code:
                   if (event.getEntity() != null)
                   {
                       System.out.println( event.getEntity().getClass().getName() );
    
                   }
    That's inside of an onEntityExplode event, and only Creeper explosions are appearing.

    I need to differentiate between Creepers, TNT, and Ghast Fireballs. How could I go about this?
     
  2. Offline

    Edward Hand

    event.getEntity() returns a null entity for TNT and GhastFireballs. It doesn't for creepers.
    That's about the only 'propper' detection you can do as far as I know.
     
  3. Offline

    but2002

    So I've noticed. It shouldn't be doing that as far as I know.

    Is there any other possible way to differentiate between the three?
     
  4. Offline

    Edward Hand

    You could use the ExplosionPrimedEvent.
    It is called when the creeper/TNT/fireball actually explodes, but before block/player damage is done.

    You can then use:
    Code:
    if(event.getEntity() instanceof TNTPrimed)
    
    if(event.getEntity() instanceof Creeper)
    
    if(event.getEntity() instanceof Fireball)
     
  5. Offline

    but2002

    Would cancelling this event also cancel the world damage?
     
  6. Offline

    Edward Hand

    Yes. It prevents the explosion entirely. If you cancel an ExplosionPrimedEvent the EntityExplodeEvent will never be called either.
     
  7. Offline

    but2002

    Thank you! With your help, I managed to whip this together. :)

    Code:
           public void onExplosionPrimed(ExplosionPrimedEvent event)
           {
                   if (event.getEntity() != null)
                   {
                       if (event.getEntity().getClass().getName().contains("CraftCreeper"))
                       {
                           event.getEntity().remove();
                           event.setCancelled(true);
                           return;
                       }
    
                       if (event.getEntity().getClass().getName().contains("CraftFireball"))
                       {
                           event.getEntity().remove();
                           event.setCancelled(true);
                           return;
                       }
    
                   }
                   return;
           }
     
  8. Offline

    eisental

    It would be cleaner (and more efficient) to use instanceof like Edward proposed:
    Code:
      public void onExplosionPrimed(ExplosionPrimedEvent event)
           {
                   if (event.getEntity() != null)
                   {
                       if (event.getEntity() instanceof Creeper)
                       {
                           event.getEntity().remove();
                           event.setCancelled(true);
                           return;
                       }
    
                       if (event.getEntity() instanceof Fireball)
                       {
                           event.getEntity().remove();
                           event.setCancelled(true);
                           return;
                       }
    
                   }
                   return;
           }
     
Thread Status:
Not open for further replies.

Share This Page