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

Solved Player is in radius of a zombie

Discussion in 'Development' started by iCirgio, Feb 10, 2018.

  1. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    I am making a guards plugin but got into the error. How do i get if a player is in the radius of the mob?


    Previous Attempt
    PHP:
    public function onKill(EntityDamageEvent $ev){

               
    $player $ev->getEntity();
               
    $boss $ev->getEntity()->getNameTag() == "§3Guard §8<§a" $entity->getHealth() . "/" $entity->getMaxHealth() . "§8>";

      if(
    $ev instanceof EntityDamageByEntityEvent && $player instanceof Player){

      
    $fighter $ev->getDamager();

       if(
    $player->distanceSquared($boss) < 64 && $fighter->distanceSquared($boss) < 64){

    //Code
     
    Last edited: Feb 10, 2018
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You are comparing Vector3 with string
     
    0x15f likes this.
  3. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    Anyone help
     
  4. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Few things wrong with your code:
    1. You were comparing a Vector3 against a string
    2. You were assuming that the event had a damager
    3. You were checking the entities nametag
    Fixes:
    1. You can check the distance (squared) from a vector with Position::distance(Vector)
    2. EntityDamageEvent has a number of causes, it isn't always an EntityDamageByEntityEvent
    3. I suggest saving the bosses entity ID and checking that against the entities ID. Using the nametag isn't the best choice.
    PHP:
    public function onDamage(EntityDamageEvent $ev) {
        if(!
    $ev instanceof EntityDamageByEntityEvent) {
            return;
        }

        
    $entity $ev->getEntity();
        
    $damager $ev->getDamager();
        if(!
    $damager instanceof Player or $entity->getNameTag() !== "§3Guard §8<§a" $entity->getHealth() . "/" $entity->getMaxHealth() . "§8>") {
            return;
        }

        
    $vector = new Vector3($damager->getX(), $damager->getY(), $damager->getZ());
        if(
    $entity->getPosition()->distance($vector) <= 64) {
            
    //code
        
    }
    }
     
    NickTehUnicorn likes this.
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Instead of depending on the nametag, I suggest that you identify entities by setting an NBT tag inside.
     
    jasonwynn10 likes this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It can change.
    I believe he wanted to check if distance <= 8, so distanceSquared <= 64 should be correct.
     
    jasonwynn10 likes this.
  7. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    To be honest, after I saw him compare a vector and a string I wasn't entirely sure he know what he was doing squaring.
     
    jasonwynn10 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.