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

Checking if target and killer are instanceof Player not working

Discussion in 'Development' started by xBeastMode, Mar 15, 2017.

  1. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    There's a bug with both PlayerDeathEvent and EntityDeathEvent. When one entity kills another and I check if both are instanceof player, the code does not execute but without checking it does.

    Here's my code:

    PHP:
       /**
        * @param EntityDeathEvent|PlayerDeathEvent $event
        */
        
    public function onDeath(EntityDeathEvent $event){
                
    $target $event->getEntity();
                
    $cause $event->getEntity()->getLastDamageCause();

                if(
    $cause instanceof EntityDamageEvent){
                        if(
    $target instanceof Player){
                                
    $this->core->PocketLegionLevels()->setDeaths($target$this->core->PocketLegionLevels()->getDeaths($target) + 1);
                        }
                }

                if(
    $cause instanceof EntityDamageByEntityEvent){
                        
    $killer $cause->getDamager();

                        if(
    $killer instanceof Player and $target instanceof Player){
                                
    $this->core->PocketLegionLevels()->setKills($killer$this->core->PocketLegionLevels()->getKills($killer) + 1);
                                
    $this->core->PocketLegionLevels()->setDeaths($target$this->core->PocketLegionLevels()->getDeaths($target) + 1);

                                if(
    $this->core->PocketLegionLevels()->getCrate($killer)){
                                        
    $level $this->core->PocketLegionLevels()->getLevel($killer);
                                        
    Server::getInstance()->broadcastMessage(RandomUtils::colorMessage(str_replace(['@player''@level'], [$killer->getName(), $level],
                                            
    $this->core->getMessage('levels''receive_crate'))));
                                }

                                if(
    $this->core->PocketLegionLevels()->playerLevelUp($killer)){
                                        
    $level $this->core->PocketLegionLevels()->getLevel($killer);
                                        
    Server::getInstance()->broadcastMessage(RandomUtils::colorMessage(str_replace(['@player''@level'], [$killer->getName(), $level],
                                            
    $this->core->getMessage('levels''level_up'))));
                                }
                        }

                        return;
                }
        }
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Have you tried var_dump to see what $killer and $target are?
     
  3. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    What else could they be besides Players? I already tested it by killing another Player and it isn't doing anything.
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    If everything you stated is true, then a check isn't passing somewhere. There is absolutely 0 harm in debugging every variable.
     
  5. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Ok, sure. I'll try it but I don't have another player to test it. Is there a way I can spawn a testing NPC?
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You did use pocketmine/Player right?
     
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    does every time the event fires before your instance of player check
    echo '$killer class: '.getclassname($killer);
    echo '$target class: '.getclassname($target);
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Would anyone test this with me or how can I spawn an NPC?
     
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    try specter plugin it came in handy most times, if it even work for latest ver
     
  10. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    I forgot about that one. Does it still work for api 3.0.0-ALPHA4?
     
  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.