Creating Explosion When SnowBall hits ground.

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

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

    OppositeGamer

    How to? This is my code. But there is only an explosion when its near a player. How can I make it do it every time?

    Code:
              Entity entity = e.getEntity();
                Projectile proj = (Projectile)entity;
                Entity shooter = proj.getShooter();
               
                if ((entity.getType() == EntityType.SNOWBALL) &&
                          (shooter.getType() == EntityType.PLAYER)) {
                          Player p = (Player)shooter;
                         
                int radius = 4;
                List entitylist = entity.getNearbyEntities(radius, radius, radius);
               
                for (int i = 0; i < entitylist.size(); i++) {
                      Entity realEntity = (Entity)entitylist.get(i);
     
                      if ((realEntity instanceof LivingEntity)) {
                        LivingEntity newEnt = (LivingEntity)realEntity;
                       
                        double posX = proj.getLocation().getX();
                        double posY = proj.getLocation().getY();
                        double posZ = proj.getLocation().getZ();
                       
                          proj.getWorld().createExplosion(posX, posY, posZ, 4.0F, false, false);
                        }
                    }
                }
          }
    }
     
  2. Offline

    ZeusAllMighty11

    Projectile hit event works fine for me with arrows
     
  3. Offline

    suckycomedian

    You want it to explode when nothing is near it?
     
  4. Offline

    crushh87

    It might be the instanceof LivingEntity part, that's my best guess
     
  5. Offline

    OppositeGamer

    Yeah suckycomedian exactly. And crushh I removed that instanceof and still no luck. It still wont explode unless near a player... its odd.
     
  6. Offline

    suckycomedian

    You're telling it to explode if something alive is near it. If nothing is near it then it can't pass that. Just take all that out and put proj.getWorld().createExplosion(proj.getLocation(), 4.0F, false, false);
     
  7. Offline

    OppositeGamer

    kk I will trye​

    Got it to work! This is the final code for anyone else that might need it. :)

    Code:
          @EventHandler
          public void SnowballLightning(ProjectileHitEvent e) {
           
              Entity entity = e.getEntity();
                Projectile proj = (Projectile)entity;
                Entity shooter = proj.getShooter();
               
                if ((entity.getType() == EntityType.SNOWBALL) &&
                          (shooter.getType() == EntityType.PLAYER)) {
                          Player p = (Player)shooter;
                         
                int radius = 4;
                List entitylist = entity.getNearbyEntities(radius, radius, radius);
               
                for (int i = 0; i < entitylist.size(); i++) {
                    realEntity = (Entity)entitylist.get(i);
                }
     
                        LivingEntity newEnt = (LivingEntity)realEntity;
                       
                        double posX = proj.getLocation().getX();
                        double posY = proj.getLocation().getY();
                        double posZ = proj.getLocation().getZ();
                       
                          proj.getWorld().createExplosion(posX, posY, posZ, 4.0F, false, false);
                          newEnt.damage(4);
                }
          }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  8. Offline

    suckycomedian

    Also doing it the way you had it, if you threw it into a crowd of 4 entities, there would've been 4 explosions because it would've gone through that code 4 times
     
  9. Offline

    gomeow

    No it wouldn't. He closed the for loop.

    OppositeGamer

    You don't need to cast to projectile or anything.

    Projectile proj = event.getEntity() //Returns projectile
     
  10. Offline

    suckycomedian

    Code:
    int radius = 4;
                List entitylist = entity.getNearbyEntities(radius, radius, radius);
             
                for (int i = 0; i < entitylist.size(); i++) { //4 entities in list
                      Entity realEntity = (Entity)entitylist.get(i);
                          //get entity i
     
                      if ((realEntity instanceof LivingEntity)) {
                        LivingEntity newEnt = (LivingEntity)realEntity;
                     
                        double posX = proj.getLocation().getX();
                        double posY = proj.getLocation().getY();
                        double posZ = proj.getLocation().getZ();
                     
                          proj.getWorld().createExplosion(posX, posY, posZ, 4.0F, false, false);
                          //explode at entity i(1) location
                          //explode at entity i(2) location
                          //explode at entity i(3) location
                          //explode at entity i(4) location
                        }
                    }
     
Thread Status:
Not open for further replies.

Share This Page