Getting entities around ProjectileHitEvent

Discussion in 'Plugin Development' started by OppositeGamer, Jan 3, 2013.

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

    OppositeGamer

    I want to create an explosion on when the snowball hits the ground. But I doesnt damage any players around it. I know you have to get a radius around the event where the SnowBall lands and then get the entities within that location and damage them. However I dont know how to get the radius nor the entities. Help would be much appreciated :)

    Code:
    Code:
       
          @EventHandler
          public void SnowballLightning(ProjectileHitEvent event) {
           
              if(event.getEntity() instanceof Snowball) {
                 
              double posX = event.getEntity().getLocation().getX();
              double posY = event.getEntity().getLocation().getY();
              double posZ = event.getEntity().getLocation().getZ();
             
                event.getEntity().getWorld().createExplosion (posX, posY, posZ, 20.0F, false, false);
              }
            }
    }
    
     
  2. Offline

    CubixCoders

    Code:java
    1.  
    2. @EventHandler
    3. public void SnowballLightning(ProjectileHitEvent event) {
    4.  
    5. if(event.getEntity() instanceof Snowball) {
    6. Snowball sb = (Snowball) event.getEntity();
    7. double posX = event.getEntity().getLocation().getX();
    8. double posY = event.getEntity().getLocation().getY();
    9. double posZ = event.getEntity().getLocation().getZ();
    10.  
    11. event.getEntity().getWorld().createExplosion (posX, posY, posZ, 20.0F, false, false);
    12. for(Entity nearby: snowball.getNearbyEntities(5, 5, 5)){
    13. if(!(nearby instanceof Player)){ // You can't damage entities
    14. return;
    15. }
    16. Player nearbyp = (Player) nearby;
    17. nearbyp.damage(DAMAGEAMOUNT); // Damages player by DamageAmount
    18. }
    19. }
    20. }
    21.  
     
  3. Offline

    fireblast709

    You might want to use continue instead of return
     
  4. Offline

    CubixCoders

    No, i chose return because you can't damage entities, so it checks if the entity nearby is not a player, if it isn't it doesnt run the rest of the code, but the for loop itself is ran multiple times, which is why it works.
     
  5. Offline

    fireblast709

    There can be multiple players nearby. If there is also a pig there, no1 will get hurt
     
    McMhz likes this.
Thread Status:
Not open for further replies.

Share This Page