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

Why doesn't this work?

Comments in 'Plugin Development' started by RekkuzaRage, Jul 9, 2015.

  1. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    I was testing out my code to see if killing a player outputs a message to killer and the code isn't working. I registered the PlayerDeathEvent and etc. I am not sure why it isn't working... No errors popup or crashes happen as well.

    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $entity $event->getEntity();
            
    $cause $entity->getLastDamageCause();
        
            if(
    $cause instanceof EntityByDamageEntityEvent) {
                
    $player $cause->getDamager()->getPlayer();
                
    $player->sendMessage("You killed....");
            }
        }
    }
    I also tried this, it checks the killer is a instance of player, but the code doesn't work either.
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $entity $event->getEntity();
            
    $cause $entity->getLastDamageCause();
        
            if(
    $cause instanceof EntityByDamageEntityEvent) {
                
    $player $cause->getDamager()->getPlayer();
                if(
    $player instanceof) {
                    
    $player->sendMessage("You killed....");
                }
            }
    }
  2. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    Add 'Player' after Insteancedof

    PHP:
    public function onPlayerDeathEvent(PlayerDeathEvent $event)
            {
                
    $player $event->getEntity();
                
    $name strtolower($player->getName());
          
                if (
    $player instanceof Player)
                {
                    
    $cause $player->getLastDamageCause();
          
  3. Anton Magsino
    Offline

    Anton Magsino Active Member

    Joined:
    Jan 17, 2014
    Posts:
    58
    Minecraft User:
    MinecrafterPH
    Try this.
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
        
    $entity $event->getEntity();
        if(
    $entity instanceof Player){
           
    $cause $entity->getLastDamageCause();
           if(
    $cause instanceof EntityDamageByEntityEvent){
              
    $player $cause->getDamager();
              if(
    $player instanceof Player){
                 
    $player->sendMessage("You killed....");
              }
           }
        }
    }
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why $name?
    It is EntityDamageByEntityEvent.
    PHP:
    public funciton onDeath(PlayerDeathEvent $event){
      
    $entity $event->getEntity();
      
    $cause $entity->getLastDamageCause();
      if(
    $cause instanceof EntityDamageByEntityEvent){
        
    $player $cause->getDamager(); // getPlayer() is not needed! Where did you learn this? This is just wrong!
        
    if($player instanceof Player){
          
    $player->sendMessage("You killed " $entity->getName());
        }
      }
    }
  5. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    Is an example :)
  6. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    I forgot that, but it is fixed in the plugin itself.
    I saw it in another plugin. Also thanks a lot.

Share This Page

Advertisement