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
Few things wrong with your code: You were comparing a Vector3 against a string You were assuming that the event had a damager You were checking the entities nametag Fixes: You can check the distance (squared) from a vector with Position::distance(Vector) EntityDamageEvent has a number of causes, it isn't always an EntityDamageByEntityEvent 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 }}
Instead of depending on the nametag, I suggest that you identify entities by setting an NBT tag inside.
It can change. I believe he wanted to check if distance <= 8, so distanceSquared <= 64 should be correct.
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.