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
    so ur method works? can u share a bit of it? thnx
    LDX likes this.
  2. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    It doesn't cover everything, such as being punched off of a cliff, but it's a lot better than nothing.

    PHP:
      public function onFight(EntityDamageEvent $event) {
        if(
    $event->getEntity() instanceof Player) {
          
    $p $event->getEntity();
          if(
    $event instanceof EntityDamageByEntityEvent && $event->getDamager() instanceof Player) {
            
    $d $event->getDamager();
            
    $this->last[strtolower($p->getName())] = $d;
          } else {
            unset(
    $this->last[strtolower($p->getName())]);
          }
        }
      }
      public function 
    onDeath(PlayerDeathEvent $event) {
        
    $p $event->getEntity();
        if(isset(
    $this->last[strtolower($p->getName())])) {
          
    $k $this->last[strtolower($p->getName())];
          
    $this->addKill($k);
          
    $p->sendMessage("You have been killed by " $k->getName() . ".");
          
    $k->sendMessage("You have killed " $p->getName() . ".");
          
    $this->getServer()->getLogger()->info("§b[LDXStats] §e" $p->getName() . " has been killed by " $k->getName() . ".");
          unset(
    $this->last[strtolower($p->getName())]);
        } else {
          
    $p->sendMessage("You have been killed.");
          
    $this->getServer()->getLogger()->info("§b[LDXStats] §e" $p->getName() . " has been killed."); 
        }
        
    $this->addDeath($p);
        
    $event->setDeathMessage("");
      }
    iJoshuaHD and Tuff like this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The last damage cause may be an integer ID or an EntityDamageEvent, and the EntityDamageEvent may be an EntityDamageByEntityEvent. And the dead entity may be a Player object or otherwise. Same to the entity in EntityDamageEvent. And also there may be a damager or not, and the damager may be a Player or not. It is very variable. You have to expect all cases.
    64FF00 likes this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Well, we need to fix StatsCore ;)
  5. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    where "$this->last" came from
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    An empty array.
    64FF00 and LDX like this.
  7. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    I see no point in defining it earlier. If $this->last[$name] is set, $this->last is set. :p

    Also, $this->addKill($p); and $this->addDeath($p); are functions that record specific player's kill/death statistics and sends them to my website to be processed and put on this page.
    Last edited: Sep 30, 2014
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You have to initialize the array.
  9. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    I do. Just not using $this->last = array(); I specify a key right from the start instead.
  10. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so that means $this->last is this:
    public $last = []; ?
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes. This is just some basic sessioning.
    64FF00 and iJoshuaHD like this.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    This is not necessary. Why don't you just check if $event->getEntity()->getLastDamageCause() instanceof EntityDamageByEntityEvent?
  13. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    finally, i got something correct xD
    64FF00 likes this.
  14. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
  15. Sergey_D
    Offline

    Sergey_D Active Member

    Joined:
    Jan 19, 2014
    Posts:
    75
    Minecraft User:
    CLILLZ
    PHP:
    $t $event->getLastDamageCase()->getDamager()->getName(); //You will get name damager
    $p $this->getServer()->getPlayer($t);
    if(
    $p instanceof Player){
    $p->sendMessage(F::RED .<MESSAGE>); //use pocketmine\utils\TextFormat as F;
    }
    Tuff likes this.
  16. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    duh
    LDX likes this.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    @LDX $event->getLastDamageCause() works just like how your sessioning works.
    64FF00 and LDX like this.
  18. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    i got my problem solved! thanks to @LDX and @PEMapModder!!

    requesting Mods/available staff to close this thread.
    LDX and Tuff like this.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why close? This thread may get useful for future reference and usage that should be inside this thread.
    LDX and Tuff like this.
  20. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    oh ok

Share This Page

Advertisement