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

get entity name

Discussion in 'Help' started by CupidonSauce173, Aug 30, 2018.

  1. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Hi, I have no idea what I am doing wrong

    PHP:
        public function onPlayerDeath(PlayerDeathEvent $event){
            
            
    $player $event->getPlayer();
            
                
            
    $victime $event->getEntity()->getName();
            
            
    $killer $event->getEntity()->getLastDamageCause()->getDamager();

            
            
    $messages = [" Got REKT by "" Died From Da Hand Of "" Got His Ass Fixed By "" Cried To Parents Because Of "" Got Bullied By "];
            
    $randommessage =  $messages[array_rand($messages)];
                
            if(
    $killer instanceof Player){
                    
                 
    $this->getServer()->broadcastMessage(TextFormat::GREEN $victime TextFormat::RED $randommessage TextFormat::GREEN $killer);
            }
            
            
            
            if(
    $event->getEntity() instanceof Player){
                
    $event->setDrops([]);
                
    $this->addStrike($playertrue);
            }
        }
    It doesn't get the Entity name of the $killer but the ID of the entity....$victime is working fine
     
  2. RyanShaw

    RyanShaw Witch

    Messages:
    69
    Its not returning the killers name because you're only asking for the getDamager()
    PHP:
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
    You basically just forgot to add the final ->getName() at the end my friend.
    PHP:
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
    $name $killer->getName();
     
  3. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    I already tried, when I add ->getName(), nothing happen.
     
  4. RyanShaw

    RyanShaw Witch

    Messages:
    69
    What do you mean by nothing happens? In my plugin this works fine for me
    PHP:
                $killer $event->getPlayer()->getLastDamageCause()->getDamager();
                
    $killer_name $killer->getName();    
     
  5. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    mhm weird
     
  6. RyanShaw

    RyanShaw Witch

    Messages:
    69
    I think this is a better way of doing it, in terms of checks too. Idk if there's anything wrong with it as I wrote it just there.
    PHP:
    public function onPlayerDeath(PlayerDeathEvent $event){

    $cause $event->getPlayer()->getLastDamageCause();

    if (
    $cause instanceof EntityDamageByEntityEvent && $cause->getDamager() instanceof Player) {

    $victim $event->getPlayer();
    $victim_name $victim->getName();
    $killer $victim->getLastDamageCause()->getDamager();
    $killer_name $killer->getName();

    $messages = [" Got REKT by "" Died From Da Hand Of "" Got His Ass Fixed By "" Cried To Parents Because Of "" Got Bullied By "];
    $randommessage =  array_rand($messages);//you were doing this before in a weird way idk why
                
    $this->getServer()->broadcastMessage(TextFormat::GREEN $victim_name TextFormat::RED $randommessage TextFormat::GREEN $killer_name);
     
    $event->setDrops([]);
    $this->addStrike($playertrue);
    }
    }
        
     
  7. CupidonSauce173

    CupidonSauce173 Zombie

    Messages:
    298
    GitHub:
    cupidonsauce173
    Idk now it works but thank you.
     
  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.