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

How do I get who killed a player?

Comments in 'Plugin Development' started by XFuryMCPE, Jan 17, 2016.

  1. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    Okay, so I've been having troubles with this.. I can't get who killed a player :/, this is what I tried:

    PHP:
    public function onDamage(EntityDamageByEntityEvent $e){
     
    $p $e->getEntity();
     
    $d $p->getDamager();
     if(
    $p instanceof Player){
      if(
    $d instanceof Player){
       switch(
    $p->getHealth()){
        case 
    0:
         
    $d->getInventory()->addItem(Item::get(1,0,1));
        break;
       }
      }
     }
    }
    No luck. Any ideas? Tips? Do you know how to do it? Help appreciated. xD :D
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    public function onDamage(EntityDamageEvent $e){
        
    $p $e->getEntity();
        if(
    $p instanceof Player && $e instanceof EntityDamageByEntityEvent){
        
    $d $e->getDamager();
         if(
    $d instanceof Player){
          if(
    $e->getDamage() >= $p->getHealth() && $p->getHealth() > 0){
            
    $d->getInventory()->addItem(Item::get(1,0,1));
         }
        }
    }
    }
    applqpak, Vaivez66 and XFuryMCPE like this.
  3. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    omg i love you ;) no homo xD. Tysm!
    applqpak likes this.
  4. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    No problem ;)
    applqpak and XFuryMCPE like this.
  5. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    kinda didn't do anything. lol..
    applqpak likes this.
  6. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Well, use PlayerDeathEvent instead EntityDamageEvent.
    PHP:
    public function OnDeath(PlayerDeathEvent $event){
    if((
    $entity $event->getEntity()) instanceof Player){
    $cause $entity->getLastDamageCause();
    if(
    $cause instanceof EntityDamageByEntityEvent && ($damager $cause->getDamager()) instanceof Player){
    // Now you have the killer and the entity.
    }
    }
    PEMapModder likes this.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Right, it is more sensible like that.
    If you are using EntityDamageEvent for MUST BE WE KNOW YOU WON'T reasons, make sure to handle at priority MONITOR and enable ignoreCancelled.
    AndrewBit likes this.
  8. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    I did it my way so that players dont need to click the respawn button(this code is not inside what i posted)

Share This Page

Advertisement