[Solved]How to strike lightning at player's position?

Discussion in 'Plugin Development' started by DarknessXIII, Aug 28, 2012.

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

    DarknessXIII

    Hi, I'm new in Plugin Development.

    So, I'm trying to make a plugin, that nakes player strike lightning at his enemy. (i think about: you hit the player with sword -> it strikes lightning art him)

    I tried this:
    Code:
        @EventHandler
        public void onAttack(PlayerInteractEvent e, EntityDamageByEntityEvent p){
            Entity defender = p.getEntity();
            Player sender = null;
            final Player player = (Player) sender;
     
            World world = player.getWorld();
         
            Location location = defender.getLocation();
            world.strikeLightning(location);
     
        }
    
    But it doesn't work :/

    All the code:
    StrikeEffects.java:
    Code:
    package DarknessXIII.StrikeEffects;
     
    import DarknessXIII.StrikeEffects.ListenerWalki;
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class StrikeEffects extends JavaPlugin {
       
        public void onDisable() {
            Bukkit.getScheduler().cancelAllTasks();
        }
     
        public void onEnable() {
     
            getServer().getPluginManager().registerEvents(new ListenerWalki(), this);
        }
     
    }
    
    ListenerWalki.java
    Code:
    package DarknessXIII.StrikeEffects;
     
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class ListenerWalki implements Listener {
       
        @EventHandler
        public void onAttack(PlayerInteractEvent e, EntityDamageByEntityEvent p){
            Entity defender = p.getEntity();
            Player sender = null;
            final Player player = (Player) sender;
     
            World world = player.getWorld();
         
            Location location = defender.getLocation();
            world.strikeLightning(location);
     
        }
    }
    
    Sorry for my bad grammar, I'm not English :/
     
  2. Offline

    dark navi

    I don't think you can have an event listen to two events at a time.
     
  3. Offline

    x86cam

    All you need to do is:
    (targetPlayer is the target player, and you need to get the targetPlayer)
    world.strikeLightning(targetPlayer.getLocation());
     
  4. Offline

    DarknessXIII

    Ok, I got it working by changing my listener to this:
    Code:
    @EventHandler
        public void onAttack(EntityDamageByEntityEvent p){
            Entity defender = p.getEntity();
            Player sender = null;
            final Player player = (Player) sender;
     
            World world = defender.getWorld();
         
            Location location = defender.getLocation();
            world.strikeLightning(location);
     
        }
    
    But when I hit a player, it spreads thousands and thousands of thunders and its isn't stopping :/

    EDIT
    @Up
    Ok, I'll try.
     
  5. Offline

    Chrono7

    You shouldn't need a sender or the PlayerInteractEvent from the looks of it, you can get the world from the defender
     
  6. Offline

    MucTweezer

    I think that when the lightning strike is damaging the player, EntityDamageByEntityEvent is being called again, thus producing another lightning strike (and so on and so forth), which calls the event and produces another lightning strike, creating an endless streak of lightning strikes.
     
    DarknessXIII likes this.
  7. Offline

    Giant

    That would be because you are listening to the damage event, which I believe getting struck by lighting also triggers, therefore getting struck by lighting directly triggers the next lightning bolt, which triggers the next, which triggers another, and you get the point... :p
     
    DarknessXIII likes this.
  8. Offline

    DarknessXIII

    @Up
    So it can be stopped if the plugin checks if the attacked is player?
    Now it's
    Code:
        @EventHandler
        public void onAttack(EntityDamageByEntityEvent p){
            Entity attacker = p.getDamager();
            Entity defender = p.getEntity();
    
            if(attacker == Player ){
            World world = defender.getWorld();
           
            Location location = defender.getLocation());
            world.strikeLightning(location);
               }
        }
    
    But I don't know what should I put instead of "Player" :/
     
  9. Offline

    Giant

    you would want to check for if(attacker instanceof Player) :)
     
    DarknessXIII likes this.
  10. Offline

    DarknessXIII

    @Up
    Yeah, it's working ^.^

    Thank you all guys for the help :]
     
Thread Status:
Not open for further replies.

Share This Page