1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved EntityDamageByEntityEvent adds more damage than there is.

Discussion in 'Development' started by Eduardo, Aug 8, 2019.

  1. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    So I'm not really sure what to do about this one. I have a boss that counts the damage of each player on EntityDamageByEntityEvent and adds it up in an array like this (method inside my boss entity):


    PHP:
    /**
             * @param Player $player
             * @param int    $damage
             */
            
    public function setDamage(Player $playerint $damage){
                    if(!isset(
    $this->damagers[spl_object_hash($player)])){
                            if((
    $this->getHealth() - $damage) <= 0){
                                    
    $this->damagers[spl_object_hash($player)] = [$player$this->getHealth()];
                            }else{
                                    
    $this->damagers[spl_object_hash($player)] = [$player$damage];
                            }
                    }else{
                            if((
    $this->getHealth() - $damage) <= 0){
                                    
    $this->damagers[spl_object_hash($player)][1] += $this->getHealth();
                            }else{
                                    
    $this->damagers[spl_object_hash($player)][1] += $damage;
                            }
                    }
            }
    And this is how I set the damage:
    PHP:
    /**
             * @param EntityDamageByEntityEvent $event
             */
            
    public function entityDamage(EntityDamageByEntityEvent $event){
                    
    $entity $event->getEntity();
                    
    $killer $event->getDamager();

                    if(
    $entity instanceof BossEntity && $killer instanceof Player){
                            
    $entity->setDamage($killer$event->getFinalDamage());
                    }
            }

    Yeah I know why so many ifs, but I figured this is the only way to fix the issue but even then I still run into it. You can see in the screenshot I dealt 274% damage when the max should be 100%. I can't work my head around this, any suggested solutions?
     

    Attached Files:

  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Damage and health aren't always equal. There are effects, armor protection etc which can decrease the damage taken. Another thing to note is that the damage can very well be more than the health of the victim. The event could be cancelled by other plugins as well (set the priority to MONITOR).
    One way to deal with this would be overriding attack method of the BossEntity.
    PHP:
    $health $this->getHealth();
    parent::attack($source);
    if(!
    $source->isCancelled()){
        
    $healthLost $health $this->getHealth();
        
    // $event->getDamager()...
    }
     
  3. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Thank you bruh, you're the G! That worked out great. I had a different solution but yours is better.
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.