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

how can I get last damager

Discussion in 'Development' started by rektpixel, Aug 18, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hey I was doing this custom death thingie, I want the player who killed a player to be sent a message 'You killed {player} + 5 coins' and then with economy be sent 5$ I think Iv'e almost got it but I'm getting a bit stuck :/ here is the console error:
    Code:
    [16:24:33] [Server thread/CRITICAL]: "Could not pass event 'pocketmine\event\entity\EntityDamageByEntityEvent' to 'DeathView v0.0.2': Call to a member function sendMessage() on boolean on TheDiamondYT\DeathView\Loader
    [16:24:33] [Server thread/CRITICAL]: Error: "Call to a member function sendMessage() on boolean" (EXCEPTION) in "/plugins/DeathView-master/src/TheDiamondYT/DeathView/Loader" at line 67
    
    here is the code:
    PHP:
        public function onEntityDamage(EntityDamageEvent $ev) {
            if(
    $ev->isCancelled()) {
                return;
            } 
            
    $entity $ev->getEntity();
          
            if(
    $entity instanceof Player and $entity->getHealth() - $ev->getDamage() <= 0) {
                if(
    $entity->getGamemode() === Player::SPECTATOR){
                    return;
                }
                
    $ev->setCancelled(true);
                
    $entity->setGamemode(Player::SPECTATOR);
                
    $this->getServer()->getScheduler()->scheduleDelayedTask(new SpectateTask($this$entity), $this->config["time"] * 20);
                
    $entity->addTitle("§c§lYOU DIED!!§r""§fRespawning.."151515);
                
    $level $entity->getLevel();
                
    $x $entity->getX();
                
    $y $entity->getY();
                
    $z $entity->getZ();
                
    $pos1 = new Vector3($x$y$z);
                
    $pos = new Vector3($x$y 2$z);
                
    $level->addSound(new AnvilFallSound($pos1));
                
    $level->addParticle(new HeartParticle($pos));
                
    $killer  $entity->getLastDamageCause()->getDamager instanceof EntityDamageByEntityEvent;
                
    $killer->sendMessage("You killed " $entity->getNameTag() . " + 5 coins") instanceof EntityDamageByEntityEvent;
                
    $money $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->addMoney(5)->$player->getName());
                return;
                }
    (line 67 is:
    PHP:
    $killer->sendMessage("You killed " $entity->getNameTag() . " + 5 coins") instanceof EntityDamageByEntityEvent;
    ) could anyone help me with this? thanks :)
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    The error mentions you try to call sendMessage on a boolean, which is exactly the case.
    You assign:
    PHP:
    $killer  $entity->getLastDamageCause()->getDamager instanceof EntityDamageByEntityEvent
    So the killer is: Last damage cause instanceof EntityDamageByEntityEvent? Which there is only a boolean to answer. You could do the following to fix this:
    PHP:
    if($ev instanceof EntityDamageByEntityEvent) {
      
    $killer $ev->getDamager();
    } else {
      return;
    }
    That way you assign $killer correctly, and have the check if it is an EntityDamageByEntityEvent.
     
    Az928, SOFe, Muqsit and 2 others like this.
  3. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    PHP:
      public function onEntityDamage(EntityDamageEvent $ev) {
            if(
    $ev->isCancelled()) {
                return;
            } 
            
    $entity $ev->getEntity();
          
            if(
    $entity instanceof Player and $entity->getHealth() - $ev->getDamage() <= 0) {
                if(
    $entity->getGamemode() === Player::SPECTATOR){
                    return;
                }
                 if (
    $ev instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {
                
    $ev->setCancelled(true);
                
    $entity->setGamemode(Player::SPECTATOR);
                
    $this->getServer()->getScheduler()->scheduleDelayedTask(new SpectateTask($this$entity), $this->config["time"] * 20);
                
    $entity->addTitle("§c§lYOU DIED!!§r""§fRespawning.."151515);
                
    $level $entity->getLevel();
                
    $x $entity->getX();
                
    $y $entity->getY();
                
    $z $entity->getZ();
                
    $pos1 = new Vector3($x$y$z);
                
    $pos = new Vector3($x$y 2$z);
                
    $level->addSound(new AnvilFallSound($pos1));
                
    $level->addParticle(new HeartParticle($pos));
                
    $killer  $ev->getDamager();
                
    $killer->sendMessage("You killed " $entity->getName() ." + 5 coins")
                
    $money $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->addMoney(5$entity);
                }
     
    rektpixel likes this.
  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.