Setting thunder to not harm specified player

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

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

    DarknessXIII

    Hi, it's me again.

    I made a plugin that strikes a thunder at your enemy with 0.3% chance.
    However it also damages the attacker?
    And there's my question: is it able to set thunder to not harm the attacker?

    Here's my code:

    Code:
    private static double szansaNaPiorun= 0.3;
        private static double szansaNaPoison= 1.4;
        private static double szansaNaDisarm= 1.7;
        public static ItemStack is;
       
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onAttack(EntityDamageByEntityEvent p)
        {
         
            Entity attacker = p.getDamager();
            Entity defender = p.getEntity();
         
            if(attacker instanceof Player){
                double rollPiorun = new Random().nextInt(100);
                double rollPoison = new Random().nextInt(100);
                double rollDisarm = new Random().nextInt(100);
                boolean czyStrzeliPiorun = rollPiorun <= szansaNaPiorun;
                boolean czyZatruje = rollPoison <= szansaNaPoison;
                boolean czyUdaneRozbrojenie = rollDisarm <= szansaNaDisarm;
                if(czyStrzeliPiorun){
                 
                    World world = defender.getWorld();
                    Location location = defender.getLocation();
                    ((Player)attacker).sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Strike" + ChatColor.DARK_RED + "Effects" + ChatColor.BLACK + "]" + ChatColor.BLUE + " Piorunujacy cios!");
                    world.strikeLightningEffect(location);
                }
                if(czyZatruje){
                    ((Player)attacker).sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Strike" + ChatColor.DARK_RED + "Effects" + ChatColor.BLACK + "]" + ChatColor.DARK_GREEN + " Trujacy cios!");
                    ((LivingEntity)defender).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 90, 2));
                    World world = defender.getWorld();
                    Location loc = defender.getLocation();
                    world.playEffect(loc, Effect.MOBSPAWNER_FLAMES, null);
                }
                if(czyUdaneRozbrojenie){
                    if(defender instanceof Player){
                        if(((Player) defender).getItemInHand().getTypeId() == 268 || ((Player) defender).getItemInHand().getTypeId() == 272 || ((Player) defender).getItemInHand().getTypeId() == 267 || ((Player) defender).getItemInHand().getTypeId() == 283 || ((Player) defender).getItemInHand().getTypeId() == 276){
                            ((Player)attacker).sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Strike" + ChatColor.DARK_RED + "Effects" + ChatColor.BLACK + "]" + ChatColor.GOLD + " Rozbrajajacy cios!");
                            is = ((Player) defender).getItemInHand();
                            ((Player) defender).getInventory().removeItem(is);
                            Location loc = defender.getLocation();
                            ((Player) defender).getWorld().dropItemNaturally(loc, is);
                        }
                    }
                }
              }
        }
     
  2. Offline

    Hoolean

    1) Make a hashmap
    2) Before the code that makes the lightning strike, put the attacker in the hashmap, then after the code that makes the lightning strike, take them out
    3) Make a listener listening for the EntityDamageEvent
    4) Make it so when the listener has heard the event, it checks if cause of the damage was lightning (
    Code:
    if(event.getCause().toString()=="LIGHTNING") {
        //do stuff
    }
    )
    5) Make it check if the entity damage was a player
    6) Make it check if the player is contained in the hashmap
    7) If the player is cancel the event
    Done! Any questions?
     
  3. Offline

    keensta

    Isn't there two types of ways to do a lighting strike.

    player.getWorld().strikeLightning(player.getLocation()); (Damage one)
    player.getWorld().strikeLightningEffect(player.getLocation()); (Non damage on)

    Well anyway I user player.getWorld().strikeLightningEffect(player.getLocation()); (Non damage on) on a login event for a rank. It damages no one (near) when this player joins.
     
  4. Offline

    DarknessXIII

    MrBluebear3

    Nope, it doesn't work :F The attacker still recieve damage.
    Code
    Code:
    package DarknessXIII.StrikeEffects;
    
    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.event.entity.EntityDamageEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.World;
    import java.util.*;
    public class ListenerWalki implements Listener {
        
        private static double szansaNaPiorun= 50;
        private static double szansaNaPoison= 1.4;
        private static double szansaNaDisarm= 1.7;
        public static ItemStack is;
        static int itemId;
        
        public static Map<Player, Boolean> excPlayers = new HashMap<Player, Boolean>();
    
        
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onAttack(EntityDamageByEntityEvent p)
        {
            
            Entity attacker = p.getDamager();
            Entity defender = p.getEntity();
            
            if(attacker instanceof Player){
                double rollPiorun = new Random().nextInt(100);
                double rollPoison = new Random().nextInt(100);
                double rollDisarm = new Random().nextInt(100);
                boolean czyStrzeliPiorun = rollPiorun <= szansaNaPiorun;
                boolean czyZatruje = rollPoison <= szansaNaPoison;
                boolean czyUdaneRozbrojenie = rollDisarm <= szansaNaDisarm;
                if(czyStrzeliPiorun){
                    
                    World world = defender.getWorld();
                    Location location = defender.getLocation();
                    ((Player)attacker).sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Strike" + ChatColor.DARK_RED + "Effects" + ChatColor.BLACK + "]" + ChatColor.BLUE + " Piorunujacy cios!");
                    excPlayers.put((Player)attacker, true);
                     world.strikeLightning(location);
                     excPlayers.put((Player)attacker, false);
                }
                if(czyZatruje){
                    ((Player)attacker).sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Strike" + ChatColor.DARK_RED + "Effects" + ChatColor.BLACK + "]" + ChatColor.DARK_GREEN + " Trujacy cios!");
                    ((LivingEntity)defender).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 90, 2));
                    World world = defender.getWorld();
                    Location loc = defender.getLocation();
                    world.playEffect(loc, Effect.MOBSPAWNER_FLAMES, null);
                }
                if(czyUdaneRozbrojenie){
                    if(defender instanceof Player){
                        if(((Player) defender).getItemInHand().getTypeId() == 268 || ((Player) defender).getItemInHand().getTypeId() == 272 || ((Player) defender).getItemInHand().getTypeId() == 267 || ((Player) defender).getItemInHand().getTypeId() == 283 || ((Player) defender).getItemInHand().getTypeId() == 276){
                            ((Player)attacker).sendMessage(ChatColor.BLACK + "[" + ChatColor.GOLD + "Strike" + ChatColor.DARK_RED + "Effects" + ChatColor.BLACK + "]" + ChatColor.GOLD + " Rozbrajajacy cios!");
                            is = ((Player) defender).getItemInHand();
                            ((Player) defender).getInventory().removeItem(is);
                            Location loc = defender.getLocation();
                            ((Player) defender).getWorld().dropItemNaturally(loc, is);
                        }
                    }
                }
               }
        }
        
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onStruckByLightning(EntityDamageEvent event)
        {
            Entity damaged = event.getEntity();
            if(event.getCause().toString()=="LIGHTNING") {
                    if(damaged instanceof Player){
                        if(excPlayers.containsKey(true)){
                        event.setCancelled(true);
                        }
                    }
                }
            }
        }
    
    Maybe I did something wrong? :/
     
  5. Offline

    XbannisherX

    make him god for 3 sec during the thunder?
     
  6. Offline

    Hoolean

    Yep you did! This bit:
    Code:
    if(excPlayers.containsKey(true))
    should be like this:
    Code:
    if(excPlayers.get(event.getEntity())==true)
    I'm pretty sure that's it!

    That's pretty much what the code I said does!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  7. Offline

    DarknessXIII

    Stil doesn't work :F
    It makes the attacker immune from lightning damage for~1 second, and after 1 second he recieve another damage from lightning :/

    Maybe increasing the immunity time will work? But how to do this? :/
     
  8. Offline

    CXdur

    Hmm, this might work? He will get striked, but won't take any damage.
    Code:
        @EventHandler
        public void onEntityDamageEvent(final EntityDamageEvent e) {
            if (!(e.getEntity() instanceof Player)) {
                return;
            }
            if (e.getCause() == DamageCause.LIGHTNING) {
                // check if player is in the hashmap or something :P
                e.setCancelled(true);
                e.setDamage(0);
            }
        }
    
    EDIT: Understood the post after posting, and I see that this isn't the problem. Sorry.
     
Thread Status:
Not open for further replies.

Share This Page