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

$entity->getName() problem .

Discussion in 'Development' started by Nora1903, Jul 1, 2018.

  1. Nora1903

    Nora1903 Slime

    Messages:
    82
    GitHub:
    cuongvnz
    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);
                    }
                }
            }
        } 
     
  2. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    replace your condition to if($mobs instanceof Zombie) {
     
    Nora1903 likes this.
  3. Nora1903

    Nora1903 Slime

    Messages:
    82
    GitHub:
    cuongvnz
    I have tried that way but dont working with no error, when i changed to ->getName() and my code working and has the error @@
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  5. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    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);
                    }
                }
            }
        }
     
  6. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    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.
     
  7. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    Why the check of nametag AFTER the check for the entity type?
     
  8. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    If entity name is Zombie oh xd it’s already zombie :facepalm:
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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();
    }
     
  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.