EntityDamagedByEntity Event no damage done

Discussion in 'Plugin Development' started by guitargun, Jan 29, 2015.

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

    guitargun

    when a player uses a tool the code triggers a EntityDamagedByEntity Event. I did some debugging it is being fired but the target player doesn' recieve any damage. Also I don't recieve any potion effects that should have happened witht his. when I use a real physical hitting object is does work.
    the idea witht his part is that the object that is used is invisible for the players.

    Trigger mechanism:
    Code:
                          
                            List<Player> players = archeck.playertoarena.get(player.getUniqueId()).players.get(player.getUniqueId()).ab.sqloc(player, ab.getSize());
                            for(Player target : players){
                                if(target.getUniqueId() != player.getUniqueId()){
                            Arrow pr = player.getWorld().spawnArrow(player.getLocation(), new Vector(0,0,0), 0, 0);
                            pr.setMetadata("name", new FixedMetadataValue(plugin, ab.name().toLowerCase()));
                            pr.setShooter(player);
    
                            DamageCause cause = DamageCause.ENTITY_ATTACK;
                            EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(pr, target, cause, 2);
                            target.setLastDamageCause(e);
                            Bukkit.getServer().getPluginManager().callEvent(e);
                            }
                            }
    Event listener (beware of long code):
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void ondamage(EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                boolean pass = false;
              
                try{
                    event.setDamage(DamageModifier.ARMOR, 0);
                }catch(UnsupportedOperationException ex){
                  
                }
                Player victim;
                Player attacker;
                if (event.getDamager() instanceof Player) {
                    attacker = (Player) event.getDamager();
                    if (plugin.mov.playertoarena.get(attacker.getUniqueId()) != null) {
                        if (plugin.mov.playertoarena.get(attacker.getUniqueId()).players
                                .get(attacker.getUniqueId()).getProf() == Profession.MAGE) {
                            event.setCancelled(true);
                            return;
                        }
                    }
                    victim = (Player) event.getEntity();
                } else {
                    victim = (Player) event.getEntity();
                    if (event.getDamager() instanceof Projectile) {
                        Projectile pro = (Projectile) event.getDamager();
                        attacker = (Player) pro.getShooter();
                    } else {
                        attacker = null;
                    }
                }
                if (attacker.getInventory().getHeldItemSlot() == 0) {
                    if (plugin.mov.playertoarena.get(attacker.getUniqueId()) != null) {
                        if (plugin.mov.playertoarena.get(attacker.getUniqueId()).players
                                .get(attacker.getUniqueId()).getProf() == Profession.NONE) {
                            plugin.mov.playertoarena.get(attacker.getUniqueId()).players
                                    .get(attacker.getUniqueId()).setProf(
                                            Profession.WARRIOR);
                        }
                    }
                    for (Arena ar : plugin.mov.arenas.values()) {
                        if (ar.players.containsKey(attacker.getUniqueId())) {
                            pass = true;
                            break;
                        }
                    }
                    if (!pass) {
                        return;
                    }
                    // crit is already calculated
                    double damage = 0;
                  
                    if(event.getDamager() instanceof  Projectile){
                        Projectile pr = (Projectile) event.getDamager();
                        if(pr.hasMetadata("name")){
                        Ability ab = Ability.returnability(pr.getMetadata("name").get(0).asString());
                        damage = ab.getDam();
                        if(ab.getPassive() != null){
                            String passive = ab.getPassive();
                            if(passive.contains("slowed")){
                                int timed = 0;
                                for(String in : passive.split(" ")){
                                    try{
                                        timed = Integer.parseInt(in);
                                    }catch(NumberFormatException ex){
                                        continue;
                                    }
                                }
                                victim.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, timed*20, 2));
                            }else if(passive.contains("weakened")){
                          
                                int timed = 0;
                                for(String in : passive.split(" ")){
                                    try{
                                        timed = Integer.parseInt(in);
                                    }catch(NumberFormatException ex){
                                        continue;
                                    }
                                }
                                victim.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, timed*20, 2));
                            }
                        }
                        }
                    }
                    double reduction = 0;
                    double truedam = 0;
                    Damageable dam = attacker;
                    double totallife = 0;
                    for (int i = 4; i < 8; i++) {
                        if (attacker.getInventory().getItem(i) != null) {
                            ItemStack item = attacker.getInventory().getItem(i);
                            /**
                             * passive handler currently crit to add the damage
                             * extra with the amount of stacks
                             */
                            for (String in : item.getItemMeta().getLore()) {
                                if (in.contains("Passive")) {
                                    // missed crit handler
    
                                    // passive stacked damage handler
                                    if (in.contains("damage")
                                            && !in.contains("true")) {
                                        int amout = item.getAmount();
                                        damage += amout * 3;
                                    }
                                    if (in.contains("true")) {
                                        for (String in2 : in.split(" ")) {
                                            if (in2.contains("+")) {
                                                in2 = in2.replace("+", "");
                                                try {
                                                    truedam += Integer
                                                            .parseInt(in2);
                                                } catch (NumberFormatException ex) {
                                                    continue;
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    // pasive lifesteal handler
                                    if (in.contains("lifesteal")) {
                                        if (in.contains("stack")) {
                                            if (dam.getHealth() != dam
                                                    .getMaxHealth()) {
                                                totallife += item.getAmount() / 4;
                                            }
                                        } else {
                                            for (String in2 : in.split(" ")) {
                                                if (in2.contains("%")) {
                                                    in2 = in2.replace("%", "");
                                                    try {
                                                        totallife += Integer
                                                                .parseInt(in2);
                                                        break;
                                                    } catch (NumberFormatException ex) {
                                                        continue;
                                                    }
    
                                                }
                                            }
                                        }
                                    }
                                } else
                                /**
                                 * lifesteal handler non passive
                                 */
                                if (in.contains("lifesteal")) {
    
                                    // getting total lifesteal
                                    if (dam.getHealth() != dam.getMaxHealth()) {
                                        for (String in2 : in.split(" ")) {
                                            if (in2.contains("%")) {
                                                in2 = in2.replace("%", "");
                                                try {
                                                    totallife += Integer
                                                            .parseInt(in2);
                                                    break;
                                                } catch (NumberFormatException ex) {
                                                    continue;
                                                }
                                            }
                                        }
                                    }
    
                                } else
                                /**
                                 * next is for the general power non passive
                                 */
                                if (in.contains("Damage") && !in.contains("Area")
                                        && !in.contains("true")) {
                                    for (String in2 : in.split(" ")) {
                                        if (in2.contains("+")) {
                                            in2 = in2.replace("+", "");
                                            try {
                                                damage += Integer.parseInt(in2);
                                            } catch (NumberFormatException ex) {
                                                continue;
                                            }
                                            break;
                                        }
                                    }
    
                                    /**
                                     * to add area damage?!
                                     */
                                } else if (in.contains("true")) {
                                    for (String in2 : in.split(" ")) {
                                        if (in2.contains("+")) {
                                            in2 = in2.replace("+", "");
                                            try {
                                                truedam += Integer.parseInt(in2);
                                            } catch (NumberFormatException ex) {
                                                continue;
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
    
                    /**
                     * next is for all of the protection things the victim has. this
                     * include making the damage less than it already is. this wil
                     * only mean that the crit, area damage and the normal damage
                     * are being lessed the life still will remain the same however
                     * this is only for the area damage.!!
                     */
                    /**
                     * normal .setdamage is before the armor calculation
                     */
                    for (int i = 4; i < 8; i++) {
                        if (victim.getInventory().getItem(i) != null) {
                            ItemStack item = victim.getInventory().getItem(i);
                            for (String in : item.getItemMeta().getLore()) {
                                if (in.contains("Protect")
                                        || in.contains("protect")) {
                                    for (String in2 : in.split(" ")) {
                                        if (in2.contains("+")) {
                                            for (String in3 : in2.split("")) {
                                                try {
                                                    if (in.contains("Passive")) {
                                                        reduction += item
                                                                .getAmount()
                                                                * Double.parseDouble(in3);
                                                    } else {
                                                        reduction += Double
                                                                .parseDouble(in3);
                                                    }
                                                } catch (NumberFormatException ex) {
                                                    continue;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    reduction = 100 / (100 + (Math.ceil(reduction / 2.0) * 10));
                    if (reduction == 1) {
                        reduction = 0;
                    }
                    reduction = -reduction;
                    /**
                     *
                     * to add true damage true damage has to deal with the armor
                     * reduction. so this is after the protecting things
                     */
    
                    try{
                        event.setDamage(DamageModifier.ARMOR, reduction);
                    }catch(UnsupportedOperationException ex){
                      
                    }
                    if (damage == 0) {
                        damage = 1;
                    }
                    event.setDamage(critcalc(attacker, damage));
    
                    /**
                     * giving the lifesteal
                     */
                    if (totallife != 0 && dam.getHealth() != dam.getMaxHealth()) {
                        if (totallife > 60) {
                            totallife = 60;
                        }
    
                        double healthb = Math.ceil((damage / totallife) / 0.4) * 0.4;
                        if (healthb > 0 && healthb < 20) {
                            attacker.setHealth(dam.getHealth() + healthb);
                        }
                    }
    
                    /**
                     * check if the attack was crit for the missed crit passive
                     */
    
                    for (int i = 4; i < 8; i++) {
                        if (attacker.getInventory().getItem(i) != null) {
                            ItemStack item = attacker.getInventory().getItem(i);
                            for (String in : item.getItemMeta().getLore()) {
                                if (in.contains("Passive")) {
                                    // missed crit handler
                                    if (in.contains("missed crit")) {
                                        int amount = item.getAmount();
                                        if (amount >= 1
                                                && damage < event.getDamage()) {
                                            item.setAmount(1);
                                        } else if (amount < 4) {
                                            item.setAmount(amount + 1);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (truedam != 0) {
                        Damageable v = victim;
                        if (v.getHealth() - truedam >= 0) {
                            victim.setHealth(v.getHealth() - truedam);
                        } else {
                            while (v.getHealth() - truedam < 0) {
                                truedam--;
                            }
                            victim.setHealth(v.getHealth() - truedam);
                        }
                    }
                }
            }
        }
     
  2. Offline

    sirrus86

    I wasn't able to go through the entirety of your event, but one thing I do know is, to my knowledge, simply calling an EntityDamageEvent doesn't actually cause damage. You'd need to use Entity.damage(int amount) or Entity.damage(int amount, Entity source), then catch the event this triggers and work from there.
     
    Konato_K likes this.
  3. Offline

    guitargun

    @sirrus86 I did a quick look at it and it doesn't show any form of getting the source for that is what I also need. I need this metadata to give some information for any potion effects that can happen.

    EDIT:
    I did this now in the trigger
    Code:
                            List<Player> players = archeck.playertoarena.get(player.getUniqueId()).players.get(player.getUniqueId()).ab.sqloc(player, ab.getSize());
                            for(Player target : players){
                                if(target.getUniqueId() != player.getUniqueId()){
                            Arrow pr = player.getWorld().spawnArrow(player.getLocation(), new Vector(0,0,0), 0, 0);
                            pr.setMetadata("name", new FixedMetadataValue(plugin, ab.name().toLowerCase()));
                            pr.setShooter(player);
                          
                            target.damage(2, pr);
                            System.out.println("Should be damaged");
                            }
                            }
    en this as catch since it doesn't trigger the enittydamagedbyentity event
    Code:
        @EventHandler
        public void test(EntityDamageEvent e){
            System.out.println("this is catched");
            System.out.println(e instanceof EntityDamageByEntityEvent);
            System.out.println(e.getEventName());
    //        if(e instanceof EntityDamageByEntityEvent){
    //            ondamage((EntityDamageByEntityEvent) e);
    //        }
        }
    problem is the catch isn't triggered when used with the target.damage but is triggered when taking other damage

    someone any solutions to this? still stuck and the above doesn't work since the EntityDamageEvent doesn't catch

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

    guitargun

  5. Offline

    SuperOriginal

    When you damage the player just handle it there instead of trying to listen for the event?
     
  6. Offline

    guitargun

    so I have to copy my 2 methods that are around 350 lines to another class. I have the damageredcution in it as well and I don't know how bukkit handles the reduction armor with the damage
     
  7. Offline

    SuperOriginal

    @guitargun How can a method invocation be 350 lines?
     
    GrandmaJam likes this.
  8. Offline

    guitargun

    the entitydamagebyentity event is one of the 2 I use. it calculates every damage, reduction etc. (see first post at the event listener part)
     
  9. Offline

    guitargun

  10. Offline

    TGRHavoc

    @guitargun
    It could be because you're setting the damage to 0.

    Code:
    if (event.getEntity() instanceof Player) {
                boolean pass = false;
           
                try{
                    event.setDamage(DamageModifier.ARMOR, 0);
                }catch(UnsupportedOperationException ex){
               
                }
    Edit: Never mind. Just looked at the docs :p
     
  11. Offline

    guitargun

    @TGRHavoc my EDBEE is no problem that all works perfectly. the problem is with damaging a player by calling a event. what @SuperOriginal said is possible but the problem is I dont know how I go from damage - the reduction caused by armor to the actual dealt damage.
     
  12. Offline

    1Rogue

    You don't damage people by manually calling events, you call the methods supplied by the Damageable interface. In your example:

    Code:java
    1. myPlayer.damage(42, yourArrow);


    Typically speaking, you should almost never call bukkit events yourself.
     
  13. Offline

    guitargun

    @1Rogue I tried that as well but nothing happend but the player gets damaged. I tried listening to EntityDamageEvent and the EDBEE but both didn't trigger.
     
  14. Offline

    1Rogue

    Probably because you ignore all damage not directly caused by a player:

    Code:java
    1. @EventHandler
    2. public void ondamage(EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof Player) {
    4. //...
    5. }
    6. }


    You should break up the method into multiple ones for listening to the EntityDamageByEntityEvent to make your code more readable and easier to manage.
     
  15. Offline

    guitargun

    @1Rogue nope cause after that if I have a else for the projectile
    Code:
                if (event.getDamager() instanceof Player) {
                    attacker = (Player) event.getDamager();
                 
                    victim = (Player) event.getEntity();
                } else {
                    victim = (Player) event.getEntity();
                    if (event.getDamager() instanceof Projectile) {
                        Projectile pro = (Projectile) event.getDamager();
                        attacker = (Player) pro.getShooter();
                    } else {
                        attacker = null;
                    }
                }

    Edit:

    I did a System out before the if statement just to be sure it fired but that didn't happen
     
  16. Offline

    1Rogue

    In that case I reiterate my previous point:

    Also to note that a ProjectileSource is not always a Player (per your unchecked cast).
     
  17. Offline

    guitargun

    @1Rogue If I am not mistaken the event.getEntity is the damagee (victim) of the event, so this can't be another entity (this is a arena plugin). I did a system.out above that statement to be sure. Also with the normal enitydamagevent nothing happend so the EDBEE didn't happen as well.

    for the projectilesoure I only have players walking around in and when they shoot anything either they are the shooter by default or I made them the shoot of that projectile. I will put in that else for it in the final release,
     
  18. Offline

    sirrus86

    So just to bring everything up to speed, manually throwing the event does trigger the event but not the damage, while using the Entity.damage() method does damage the entity but doesn't trigger the event at all?
     
  19. Offline

    guitargun

    exactly.
    but calculating all reductions and applying this to bypass this is a problem I don't know how to solve. I don't know how the Damagecause.Armor reduces the normale damage done.
     
  20. Offline

    guitargun

    @1Rogue do you have any other tricks that I don't know about and that I can use?
     
Thread Status:
Not open for further replies.

Share This Page