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

Get player name in a world

Discussion in 'Development' started by Amon28, Jun 1, 2017.

  1. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    I need to get the player'a name in a world what I did is
    PHP:
    $name Server::getInstance()->getLevelByName("knockback")->getPlayers()->getName();
    Error: "call to a member function getName() on array"
    I've been searching on Google how to fix this but no luck
    PHP:
    public function onDeath(PlayerDeathEvent $ev){
      
    $return Server::getInstance()->getLevelByName("spawn");
      foreach(
    Server::getInstance()->getLevelByName("knockback")->getPlayers() as $all){
     
       if(
    count($all) == 1){
      
        
    $name Server::getInstance()->getLevelByName("knockback")->getPlayers()->getName();
        foreach(
    $this->getServer()->getOnlinePlayers() as $onlineplayers){
                                            
    $onlineplayers->sendMessage("§b[KB]§e$name has won KB!");
                                                                  }
                                
    $all->teleport($return->getSpawnLocation());
                          }
                          }
                          }
                          }
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    The error is self-explanatory. $level->getPlayers() returns an array of Players. How can you get a single name of a group of players? Even if there is only one player in a level, it will still return that name in an array.
     
  3. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    PHP:
    $name Server::getInstance()->getLevelByName("knockback")->getPlayer()->getName();
    Error: Call to undefined method pocketmine/level/level::getPlayer()
    Sorry I'm new to this thing I usually get the sender name
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you need the player NAME to be able to get it's instance
     
  5. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    How do I get it, when the player dies of fall damage or being pricked by a cactus will it find the player who last hit the dead player?
     
    Last edited: Jun 1, 2017
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    corytortoise likes this.
  7. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
  8. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Yes it will, as I mentioned. Look at the EntityDeathEvent where it checks if the death cause it NOT another entity.
     
  9. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    Thanks I'll try this
     
  10. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    PHP:
    public function onDeath(PlayerDeathEvent $ev){
      
    $return Server::getInstance()->getLevelByName("spawn");
      
    $lastPlayerAttacker null;
      
    $entity $ev->getPlayer();
      foreach(
    Server::getInstance()->getLevelByName("knockback")->getPlayers() as $all){
     
       if(
    count($all) == 1) {
        
    $cause $entity->getLastDamageCause();
       if(
    $cause !== EntityDamageEvent::CAUSE_ENTITY_ATTACK) {
        
    $lastPlayerAttacker $this->getServer()->getPlayer($this->getLastPlayerAttacker($entity));
       if(
    $lastPlayerAttacker !== null) {
              
    $name $lastPlayerAttacker->getName();
        foreach(
    $this->getServer()->getOnlinePlayers() as $onlineplayers){
                                            
    $onlineplayers->sendMessage("§b[KB]§e$name has won KB!");
                                                                  }
                                
    $all->teleport($return->getSpawnLocation());
                          }
                          }
                          }
                          }
                          }
    Dunno what I did wrong
    Call to undefined method KnockBack\Main::getLastPlayerAttacker()
     
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    The error says exactly the issue you are having... :facepalm: You shouldn't need someone to tell you.
    You have an undefined function in your class. That is a php issue, not something about PMMP API. Issues such as those should go to StackOverflow.
     
    Sandertv likes this.
  12. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    Yeah I'm noob at this, I'm still learning I just recognize a few errors by having the errors repeatedly
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    While modern computers have been developed with marvelous artificial intelligence, they have not yet been taught to read minds yet. There are many players in a world, and nobody from computers to robots to humans to aliens to :shoghi: can guess what you want to do. You may want to get the player who got hurt in cactus, or the player who pushed another player onto the cactus, or the player who placed the cactus, or the player who granted the permission to place cacti, or many more possibilities.
     
  14. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    How do I get the instance of player death in a level
     
  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.