Why i got this error when my code already working? Code: [00:30:49] [Server thread/CRITICAL]: Error: "Call to undefined method pocketmine \entity\object\ExperienceOrb::getName()" (EXCEPTION) in "plugins/Source-RPGMobs/ src/NORA/RPGMobs/EventListener" at line 32 code: PHP: public function onEntitySpawn(EntitySpawnEvent $e) { $mobs = $e->getEntity(); /*line 32 */ if($mobs->getName() == "Zombie"){ foreach($this->main->getServer()->getOnlinePlayers() as $p) { $name = $mobs->getName(); $level = $this->main->getLevel($p); if($mobs->distance($p) <= 50) { $mobs->setNameTag(TF::GRAY."[".$level."] ".$name); $mobs->setNameTagVisible(true); $mobs->setNameTagAlwaysVisible(true); } } } }
I have tried that way but dont working with no error, when i changed to ->getName() and my code working and has the error @@
EntitySpawnEvent is called when any entity is spawned, not just entities you can get the name of, that method won't work. You might be able to try getting the ID of the entity and checking it.
PHP: public function onEntitySpawn(EntitySpawnEvent $e) { $mobs = $e->getEntity();if(!$mobs instanceof Zombie) return; /*line 32 */ if($mobs->getNameTag() == "Zombie"){ foreach($this->main->getServer()->getOnlinePlayers() as $p) { $name = $mobs->getName(); $level = $this->main->getLevel($p); if($mobs->distance($p) <= 50) { $mobs->setNameTag(TF::GRAY."[".$level."] ".$name); $mobs->setNameTagVisible(true); $mobs->setNameTagAlwaysVisible(true); } } } }
Why are you trying to use Entity::setNameTag() multiple times with the foreach statement? Entity::setNameTag() will set the name tag of an entity for all players.
Only Living entities have the getName() method. PHP: $entity = $event->getEntity();if(!($entity instanceof Living)){ return;} But that would limit your code to Living entities only. Maybe just replace getName() with getNameTag(). Or something with reflections.. PHP: if($mobs instanceof Living){ $name = $mobs->getName();}else{ $name = (new \ReflectionClass($mobs))->getShortName();}