1.17.x EntityDamageEvent is called several times.

Discussion in 'Spigot Plugin Development' started by Isoboroo, Jul 14, 2021.

  1. Hello, I come here for the first time. glad to see you.
    I got some curious things to ask for.
    normally, EntityDamageEvent is called when the entity is damaged by something. it is called only once, but this method called several times (If debug like sending a message to the player, it does about 30 times.)
    I couldn't find the reason, but I set a variable, 'isCoolDown'to make sure the method is not executed simultaneously or several times in a short term.

    Of course, this EventHandler is registered to the event list so it does work. I think it does not quite different from other event handlers, only this method called several times. do you know the reason?


    Code (Text):
    @EventHandler
        public void HitByArrow(EntityDamageEvent e)
        {
            //this secetion checks whether the event is about targeted event.
            if(!ps.getJob().contentEquals("숲 사냥꾼")) return;
           
            if(ps.getFirstSkill()) return;
            if(ps.getSecondSkill()) return;
           
            if(!p.getInventory().getItemInMainHand().getType().equals(Material.ENCHANTED_BOOK)) return;
           
            if(isCoolDown) return;
           
            //critical section for not executing simultaneously.
            isCoolDown = true;
            entity = (LivingEntity) e.getEntity();
           
       
            physic.setEnemy(entity);
           
            //case 1 : active skill.
            if(previous == previousSkill.active)
            {
                physic.set_coefficient(1.0);
                physic.setEnemy((LivingEntity)e.getEntity());
                physic.Damage();
               
                //caculate the vector towards enemy.
                Location back = (physic.getEnemy().getLocation().subtract(p.getLocation().toVector()));
               
                //vector for push back a enemy.
                Vector backVector = back.toVector().normalize().multiply(backOffDistance).divide( back.toVector());
                physic.getEnemy().getLocation().add(backVector);
               
                stackCount ++;
               
               
                //change the state of isCoolDown to false so that next event can occur.
                Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("lastOne"), new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                            isCoolDown = false;
                    }
                   
               
                },(long) (3));
            }
           
            //case2 : hyper skill
            else if(previous == previousSkill.hyper)
            {
                physic.set_coefficient(1.0);
                physic.setEnemy((LivingEntity)e.getEntity());
               
                Location distance = physic.getEnemy().getLocation().subtract(p.getLocation());
               
                //calculate the damage depending on the distance between player and target.
                if(distance.length() <= 7)
                {
                   
                }
               
                else if(distance.length() <= 14)
                {
                    physic.set_coefficient((1.5+ ps.getAccuracy()*0.005) + Math.random()*(ps.getAccuracy()*0.05+2));
                }
               
                else if(distance.length() <= 21)
                {
                    if(Math.random() < 0.35)
                    {
                        physic.set_coefficient((1.5+ ps.getAccuracy()*0.005) + Math.random()*(ps.getAccuracy()*0.005+2));
                    }
                   
                }
               
                else if (distance.length() <= 28)
                {
                    if(Math.random() < 0.75)
                    {
                        physic.set_coefficient((1.5+ ps.getAccuracy()*0.0075) + Math.random()*(ps.getAccuracy()*0.0075+1));
                       
                       
                    }
                   
                    if(physic.getEnemy().getType().equals(EntityType.PLAYER))
                    {
                        // if entity type is player, reduce its physical defense.
                        try {
                            PlayerStatus enemyPs = Main.findPlayerStat(((Player)physic.getEnemy()));
                            enemyPs.setPhysicalDefense(enemyPs.getPhysicalDefense() * (1.0-(0.15+ps.getAccuracy()*0.001+ Math.random()*(ps.getAccuracy()*0.001+0.2))));
                       
                        }
                       
                        catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
                    }
                    //for monster, give an additional damage 35%.
                    else {
                        physic.set_coefficient((1.5+ ps.getAccuracy()*0.0075) + Math.random()*(ps.getAccuracy()*0.0075+2) + 0.35);
                       
                       
                    }
                }
               
                else if (distance.length() >= 29)
                {
                    physic.set_coefficient((2.5+ ps.getAccuracy()*0.01) + Math.random()*(ps.getAccuracy()*0.01+1));
                   
                }
               
                if(stackCount >= 2)
                {
                    physic.set_coefficient(physic.get_coefficient() * Math.sqrt(2.5+ ps.getAccuracy() * 0.01225));
                }
               
                //change the state of isCoolDown to false so that next event can occur.
                Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("lastOne"), new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                            isCoolDown = false;
                    }
                   
               
                },(long) (20*3));
               
                //give the damage to enemy.
                physic.Damage();
            }
           
        }
     
  2. The EntityDamageEvent is called when every entity takes damage of any kind once. What you expect is, that it should only trigger when a certain entity was damaged. Check your conditions before calling any methods or use other events like:

    EntityDamageByEntityEvent
     
  3. I am pretty sure that you just registered your listener a dozen times.
     
  4. Okay Thanks I will check for it