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

Know player name who kill an other player by Projectile

Discussion in 'Development' started by romainbizet46, Oct 21, 2017.

  1. romainbizet46

    romainbizet46 Witch

    Messages:
    52
    In an PayerInteractEvent player are able to launch snowball with 10 dommage if snowball hit an other player

    But how can i get thé $killer and the $victim?

    PHP:
    public function onDamage(EntityDamageEvent $event){
            
    $victim $event->getEntity();
            
    $cause $event->getCause();
          
            if(
    $cause == EntityDamageEvent::CAUSE_PROJECTILE){
                if(
    $event instanceof Player){
                    
    $event->setDamage(10);
                    
    $killer $event->getDamager();
                    
    $welt $killer->getLevel()->getFolderName();
                    
    $arenaname $this->getConfig()->get("arena");
                          
                    
    $this->Title($victim"§4Death"$killer->getName());
                    
    $this->Title($killer"§2Kill"$victim->getName());
                          
                    
    $this->GunItems($victim);
                    
    $this->GunItems($killer);
                          
                    
    $deathmsg $this->getLanguage()->get("player.death");
                    
    $deathmsg str_replace("{player}"$killer->getName(), $deathmsg);
                          
                    
    $victim->sendMessage($this->prefix $deathmsg);
                          
                    
    $killmsg $this->getLanguage()->get("player.kill");
                    
    $killmsg str_replace("{player}"$victim->getName(), $killmsg);
                    
    $killer->sendMessage($this->prefix $killmsg);
                    return;
                }
            }
        }
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You do realize $event in this case can never be a Player instance, right?

    Back to your question, you can always var_dump($event) and get the required cases.

    In your case, $event->getDamager() should be the projectile and $event->getDamager()->getOwningEntity() should be the entity who fired the projectile (this can also return null at times, so make sure you handle that too. For example when a player fires a projectile and logs out, and the projectile later hits an entity).
     
  3. romainbizet46

    romainbizet46 Witch

    Messages:
    52
    When i add your code
    PHP:
    $event->setDamage(10);

    Doesnt working
     
  4. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    try this code it should work fine.
    PHP:
    public function onDamage(EntityDamageEvent $event){
            
    $victim $event->getEntity();
            
    $cause $event->getCause();
       
            if(
    $cause == EntityDamageEvent::CAUSE_PROJECTILE){
                if(
    $victim instanceof Player){
                    
    $event->setDamage(10);
                    
    $killer $event->getDamager();
                    
    $welt $killer->getLevel()->getFolderName();
                    
    $arenaname $this->getConfig()->get("arena");
                       
                    
    $this->Title($victim"§4Death"$killer->getName());
                    
    $this->Title($killer"§2Kill"$victim->getName());
                       
                    
    $this->GunItems($victim);
                    
    $this->GunItems($killer);
                       
                    
    $deathmsg $this->getLanguage()->get("player.death");
                    
    $deathmsg str_replace("{player}"$killer->getName(), $deathmsg);
                       
                    
    $victim->sendMessage($this->prefix $deathmsg);
                       
                    
    $killmsg $this->getLanguage()->get("player.kill");
                    
    $killmsg str_replace("{player}"$victim->getName(), $killmsg);
                    
    $killer->sendMessage($this->prefix $killmsg);
                    return;
                }
            }
        }
     
    OnTheVerge and romainbizet46 like this.
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    consider checking if damager is player will be a good practice
     
    romainbizet46 likes this.
  6. romainbizet46

    romainbizet46 Witch

    Messages:
    52
    PHP:
    public function onDamage(EntityDamageByEntityEvent $event){
            
    $victim $event->getEntity();
            
    $cause $event->getCause();
           
            if(
    $cause == EntityDamageEvent::CAUSE_PROJECTILE){
                if(
    $victim instanceof Player){
                    
    $event->setDamage(10);
                    
    $killer $event->getDamager();
                    if(
    $killer instanceof Player){
                        
    $deathmsg $this->getLanguage()->get("player.death");
                        
    $deathmsg str_replace("{player}"$killer->getName(), $deathmsg);
                       
                        
    $victim->sendMessage($this->prefix $deathmsg);
                       
                        
    $killmsg $this->getLanguage()->get("player.kill");
                        
    $killmsg str_replace("{player}"$victim->getName(), $killmsg);
                       
                        
    $killer->sendMessage($this->prefix $killmsg);
                        return;
                    }
                }
            }
        }
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That won't work, $event->getDamager() is a projectile
    PHP:
    $projectile $event->getDamager();
    if(
    $projectile instanceof Projectile){//pocketmine/entity/projectile/Projectile
        
    $projectileShooter $projectile->getOwningEntity();//this can be null if the victim was damaged after the shooter logged out.
    }
     
    OnTheVerge and romainbizet46 like this.
  8. romainbizet46

    romainbizet46 Witch

    Messages:
    52
    Maybe i do use last dommage?
    I m very confuse but know wath i want to do
     
  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.