Launching all entities struck by lightning into the air

Discussion in 'Plugin Development' started by ocomobock, May 20, 2013.

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

    ocomobock

    Yeah, this is pretty specific, but I really wanna figure it out.

    So like it says, I want any entities that get struck by lightning to get launched into the air. Here's my code:

    Code:java
    1. @EventHandler
    2. public void onStruckByLightning(LightningStrikeEvent e)
    3. {
    4. double x = e.getLightning().getLocation().getX();
    5. double y = e.getLightning().getLocation().getY();
    6. double z = e.getLightning().getLocation().getZ();
    7. List<Entity> entity = e.getLightning().getNearbyEntities(x, y, z);
    8. Entity[] ent = entity.toArray(new Entity[entity.size()]);
    9. for (int i = 0; i < ent.length; i++)
    10. {
    11. Entity currentEnt = ent[i];
    12. currentEnt.setVelocity(currentEnt.getVelocity().add(new Vector(0, 1.25, 0)));
    13. }
    14. }}[/i]


    This doesn't do anything though. I think my problem is that I can't figure out how to get each individual entity hit by lightning; I wasn't really sure what I was doing with the list and array things. So if this is possible and someone knows how to do this, please tell me how.

    Thank you in advance


    I don't know why I didn't think of this but this worked:

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event)
    3. {
    4. if (!(event.getEntity() instanceof Player))
    5. {
    6. if (doLaunch)
    7. {
    8. if (event.getCause() == EntityDamageEvent.DamageCause.LIGHTNING && event.getCause() != null)
    9. {
    10. event.getEntity().setVelocity(event.getEntity().getVelocity().add(new Vector(0, 1.25, 0)));
    11. doLaunch = false;
    12. }
    13. }
    14. }


    Neeevermind ._.

    Actually that still doesn't work. It only affects the nearest mob to the lightning bolt.

    If anyone feels like looking into this go ahead, but I don't feel like getting this to work anymore.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  2. Offline

    LucasEmanuel

    So, you mean something like this?
    Code:
    LightningStrikeEvent:
      for(Entity e : event.getLightning().getNearbyEntities(3, 3, 3)) {
        e.setVelocity(new Vector(0, 2, 0));
      }
     
    ocomobock likes this.
  3. Offline

    ocomobock

    Yeah, that works. Thank you. I haven't used those types of loops before, so that never really occurred to me.
     
Thread Status:
Not open for further replies.

Share This Page