Solved EntityDamageByEntityEvent does not fire on Snowball hit

Discussion in 'Spigot Plugin Development' started by mlgcraftnetwork, Jun 26, 2016.

  1. I'm creating a gadgets system but for some reason the EntityDamageByEntityEvent is not being fired when an snowball hits a player, I find this really weird because a snowball is an entity (right?). Does anyone know why the event isn't being fired? Thanks!

    Code:
    Code (Text):
        @EventHandler
        public void entityDamageByEntity(EntityDamageByEntityEvent e) {
            Entity et = e.getEntity();
            org.bukkit.World w = Bukkit.getServer().getWorld("World");
            if(e.getEntity().getWorld() == w){
                Bukkit.getServer().broadcastMessage("test0");
                if(et instanceof Player){
                    Player p = (Player) et;
                    Bukkit.getServer().broadcastMessage("test1");
                    if(e.getDamager() instanceof Player){
                        Player damager = (Player) e.getDamager();
                        if(damager.getInventory().getItemInMainHand().getType() == Material.STICK){
                            if(Main.gadgets.g2.get(damager.getUniqueId()) >= 1){
                                e.setCancelled(true);
                                Vector velocity = p.getLocation().toVector().subtract(damager.getPlayer().getLocation().toVector()).normalize().add(new Vector(0, .1, 0));
                                p.setVelocity(p.getVelocity().add(velocity));
                                p.getLocation().getWorld().playSound(p.getLocation(), Sound.ENTITY_PLAYER_HURT, 1, 1);
                                p.playEffect(EntityEffect.HURT);
                                Main.gadgets.g2.put(damager.getUniqueId(), Main.gadgets.g2.get(damager.getUniqueId()) - 1);
                                ItemStack sticki = new ItemStack(Material.STICK, 1);
                                ItemMeta meta = sticki.getItemMeta();
                                meta.addEnchant(Enchantment.DURABILITY, 1, true);
                                meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS);
                                meta.setDisplayName("§eWacky Stick §8: §9"+Main.gadgets.g2.get(damager.getUniqueId()));
                                Main.tools.sendActionBar(damager, "§eYou have §9"+Main.gadgets.g2.get(damager.getUniqueId())+" §6Wacky Stick §eusses left!");
                                sticki.setItemMeta(meta);
                                damager.getInventory().setItem(1, sticki);
                                if(Main.gadgets.g2.get(damager.getUniqueId()) <= 0){damager.getInventory().setItem(1, new ItemStack(Material.AIR, 1));}
                                for(Player online : Bukkit.getOnlinePlayers()){
                                    online.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_HURT, 1.0F, 1.0F);
                                    PacketPlayOutWorldParticles particle = new PacketPlayOutWorldParticles(EnumParticle.VILLAGER_ANGRY, false, (float) ((float) p.getLocation().getX()), (float) p.getLocation().getY(), (float) p.getLocation().getZ(), (float) 0.2, (float) 0.2, (float) 0.2, 0, 6, null);
                                    ((CraftPlayer) online).getHandle().playerConnection.sendPacket(particle);
                                }
                            }
                        }
                    } else if(e.getDamager().getType().equals(org.bukkit.entity.EntityType.SNOWBALL)){
                        Bukkit.getServer().broadcastMessage("test2");
                        Entity sb = (Snowball) e.getDamager();
                        e.setCancelled(true);
                        Vector velocity = p.getLocation().toVector().subtract(sb.getLocation().toVector()).normalize().add(new Vector(0, .1, 0));
                        p.setVelocity(p.getVelocity().add(velocity));
                        p.getLocation().getWorld().playSound(p.getLocation(), Sound.ENTITY_PLAYER_HURT, 1, 1);
                        p.playEffect(EntityEffect.HURT);
                        for(Player online : Bukkit.getOnlinePlayers()){
                            online.playSound(p.getLocation(), Sound.ENTITY_WITCH_HURT, 1.0F, 1.0F);
                            PacketPlayOutWorldParticles particle = new PacketPlayOutWorldParticles(EnumParticle.SPELL_WITCH, false, (float) ((float) p.getLocation().getX()), (float) p.getLocation().getY(), (float) p.getLocation().getZ(), (float) 0.2, (float) 0.2, (float) 0.2, 0, 6, null);
                            ((CraftPlayer) online).getHandle().playerConnection.sendPacket(particle);
                        }
                    }
                }
                if(et instanceof ArmorStand){
                    if(e.getDamager() instanceof Player){
                        Player p = (Player) e.getDamager();
                        if(!(Main.rankmanager.stafflvl.get(p.getUniqueId()) >= 3)){
                            e.setCancelled(true);
                        }
                    }
                }
            }
        }
    I tested the code and it works when you hit a player, just not with snowballs.
     
    • Winner Winner x 1
  2. getEntity().getLocation()
    Loop through entities close to that.
    The closest under 2 blocks wins.
     
  3. Okay, thanks!
     
    • Friendly Friendly x 1
  4. How would I check if the snowball hit a player? Because the event is also being fired when hitting mobs, blocks, etc.

    EDIT:
    Fixed it! :)
    Code (Text):
    if(hit != e.getEntity().getShooter()){
     
    #6 mlgcraftnetwork, Jun 26, 2016
    Last edited: Jun 26, 2016