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

How can i send a Message to a Player when he kills a Player?

Discussion in 'Development' started by SkyZone, Dec 6, 2016.

  1. SkyZone

    SkyZone Slime

    Messages:
    95
    I cant find any event like this :(
    Is it somehow possible in pocketmine?
     
  2. SkyZone

    SkyZone Slime

    Messages:
    95
    How can i do that?
    PHP:
    public function onDeath(PlayerDeathEvent $event){

    }
    This is what i already coded, but i cant find getDamager()
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    $damager = $event->getDamager();
    $damager->sendMessage("Blablabla");
     
    [deleted] and HimbeersaftLP like this.
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You mentioned yourself :O
     
    Muqsit, Dog2puppy and Sandertv like this.
  5. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    If the player isn't killed by another entity this won't work and will throw an error. You have to make sure that the player was last damaged by an Entity:
    PHP:
    public function onDeath(pocketmine\event\player\PlayerDeathEvent $event) {
        if(
    $event->getPlayer()->getLastDamageCause() instanceof pocketmine\event\entity\EntityDamageByEntityEvent) {
            if(
    $event->getPlayer()->getLastDamageCause()->getDamager() instanceof pocketmine\Player) {
                
    $event->getPlayer()->getLastDamageCause()->getDamager()->sendMessage("You killed " $event->getPlayer()->getName() . "!");
            }
        }
    }
     
    artulloss, OnTheVerge, Muqsit and 3 others like this.
  6. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    If you have other questions like this, try to look through the source code to find your answer. The server has a default death message so you could have tried to look for that and see how they get the two player names. Im not saying dont post on here, just make sure you do research first and dont always rely on the forums
     
    SkyZone likes this.
  7. Tester_master

    Tester_master Silverfish

    Messages:
    19
    many things from bukkit are still possible in pocketmine however not everything
     
    SkyZone likes this.
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The death event doesn't have a damager. The death event only has an entity. And that entity may have a reason of death, or no. And the reason of death may be related to another entity, or no. And the other entity may be a player, or no. And the player may still be online, or no.
     
    Muqsit and SkyZone like this.
  9. SkyZone

    SkyZone Slime

    Messages:
    95
    Where can i find the Code of the DeathMessage? I cant find it :/
     
  10. SkyZone

    SkyZone Slime

    Messages:
    95
    Found a Solution :D
    PHP:
    public function onPlayerDeath(PlayerDeathEvent $event){
            
    $player $event->getPlayer();
            
    $cid $player->getLastDamageCause()->getCause();
            
    $killedby $player->getLastDamageCause()->getEntity()->getNameTag();
                switch(
    $cid){
                    case 
    EntityDamageEvent::CAUSE_ENTITY_ATTACK:
                        
    $this->getServer()->broadcastMessage("Du wurdest von $killedby getötet");
                        break;
                }
        }
    Thanks dudes :)
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I already reminded you so many checks you have to make...
    Also, please post in English on this website. What you post aren't for one person only - if they are, please use conversations.
     
  12. SkyZone

    SkyZone Slime

    Messages:
    95
    Okay, ill post in English next time.
    Ive noticed that i only have the Name of the Damager, but not the Damager itself, like $event->getPlayer is. I need to change the items in the inventory of the killer, but i dont know how, because there isnt getDamager(). Sorry for my bad English, im from Switzerland. :)
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The last damage cause's entity is the player is the $player itself!
    Check if getLastDamageCause() is null! If the player suicides using /kill or a plugin uses kill() directly, there would be no damage cause.
    Check if getLastDamageCause() is an EntityDamageByEntityEvent!
    Check if the EntityDamageByEntityEvent->getDamager() is a Player object or just another entity!
    Check if the player isOnline(). (I'm confirming if this is needed; this may be a vulnerability to intentional malicious memory leaks)
     
    Primus and Sandertv like this.
  14. SkyZone

    SkyZone Slime

    Messages:
    95
    Isnt $player the player whos dead?
     
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Yep.
    getLastDamageCause() returns an EntityDamageEvent
    The entity in the EntityDamageEvent is obviously the entity that is damaged.
    There only exists a damager if this EntityDamageEvent is an EntityDamageByEntityEvent.
     
    Jack Noordhuis likes this.
  16. [deleted]

    [deleted] Guest

    Yeah.
     
  17. SkyZone

    SkyZone Slime

    Messages:
    95
    I recoded the last code into the EntityDamageByEntityEvent, but getDamager() is an Entity, i need a Player :/
     
  18. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    A player is an entity. Just use a check
    (
    PHP:
    if($damager instanceof Player) {
    )
    to see if it is a player.
     
    LilCrispy2o9 likes this.
  19. SkyZone

    SkyZone Slime

    Messages:
    95
    But i cant acces the Inventory of the Player, but i need this to code a gungame plugin :/
     
  20. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    If you did it correctly you can access the inventory. Just use $damager->getInventory() after you performed the check if it is a player.
     
  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.