Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Playing with EntityDeathEvent

Comments in 'Plugin Development' started by iJoshuaHD, Sep 28, 2014.

  1. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Here is a snippet code of what im trying to make:
    Code:
    public function onPlayerDeath(EntityDeathEvent $event){
       $target = $event->getEntity();
       $targeter = $event->getEntity()->getLastDamageCause()->getDamager();
           if($targeter instanceof Player){
               $targetter->sendMessage("You killed " . $target);
          }elseif($target instanceof Player){
              $target->sendMessage("You were killed by ".$targetter);
          }
    }
    
    Im not sure if $target and $targetter is right but it doesnt work for me. Any help is appreciated. Thanks.
    Last edited: Sep 28, 2014
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Look at the documentation on the return types.
  3. WillotheBlessed
    Offline

    WillotheBlessed Active Member

    Joined:
    Sep 17, 2014
    Posts:
    69
    Minecraft User:
    will
    You'd want $targetter->getName() and $target->getName() for the "Send Message" part.

    I'm curious if this code would work, because it's better than what I was going to do in my code. :D
    Tuff likes this.
  4. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    PHP:
    // Wouldn't you want to stop at getLastDamageCause() ?

    $targeter $target->getLastDamageCause()->getName()
  5. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    Actually you would probably want to do the "getName()" after the "if" statement that establishes whether or not it is an instanceof Player.
    EvolSoft and Dutok like this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No you don't.
    Check the function's return type. It can be an EntityDamageEvent instance or an integer.
    Tuff likes this.
  7. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    ive looked many times on EntityDeathEvent and i cant see how to get the player name who died.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. I mean the get last damage cause function.
  9. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    whats the resemblance ?
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    ?
    If this is what you want, look at Entity->getLastDamageCause() and Living->getLastDamageCause().
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so ill just do :
    $target = $event->getEntity()->getLastDamageCause()->getName();
    to get the victim?
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. $entity->getLastDamageCause() can return an int or an instance of EntityDamageEvent. You have to check that yourself.
  13. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    PHP:
    public function onPlayerDeath(EntityDeathEvent $event){
       
    $target $event->getEntity();
       
    $targeter $target->getLastDamageCause();
           if(
    $targeter instanceof Player){
               
    $targetter->sendMessage("You killed " $target);
          }elseif(
    $target instanceof Player){
              
    $target->sendMessage("You were killed by ".$targetter);
          }
    }
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    public function onDeath(EntityDeathEvent $event){
        
    $victim $event->getEntity();
        
    $cause $victim->getLastDamageCause();
        if(
    $cause instanceof EntityDamageEvent){
             
    $this->send("$victim killed by " $cause->getEntity() . "!");
        }
        else{
            
    $this->send("$victim died because of cause ID: $cause"); // $cause is an integer
            
    $this->send("Text for $cause: " $this->getCauseDescription($cause));
        }
    }
    private function 
    getCauseDescription($in){
        foreach((new \
    ReflectionClass("pocketmine\\event\\entity\\EntityDamageEvent"))->getConstants() as $name => $id){
            if(
    $id === $in){
                return 
    $name;
            }
        }
        return 
    "unknown";
    }
    Note that the __toString() method for a Player now is just Player (eid), which does not display the player name. Use $player->getName() instead, but this function doesn't exist if it is not a Player object.
  15. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    Ok, wouldn't line 5 be $cause->getEntitiy()->getName().
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Correct. However, if entity AI is implemented, it could be a zombie which doesn't have a name.

    Edit: I meant, both are wrong, but at least mine won't crash.
  17. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    Haha, okay. Well if you want to get the name of the killer you could always use the damage event, and get the name of the player, and save the most recent damagers name.
  18. WillotheBlessed
    Offline

    WillotheBlessed Active Member

    Joined:
    Sep 17, 2014
    Posts:
    69
    Minecraft User:
    will
    This is the strategy I was originally going to take as well. We are working on the exact same problem! Someone should write a plugin for this!

    I was thinking the "EntityDamageByEntityEvent" would be useful...
  19. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    Nah, I'll submit my CTF plugin soon, (probably) And it should clear things up a bit.
  20. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    $target->getLastDamageCause() works?? Why did I spend all morning writing a function that records when someone gets hurt and who by, for my plugin??
    EvolSoft likes this.

Share This Page

Advertisement