Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Get object of player

Comments in 'Plugin Development' started by xFlare, Feb 8, 2015.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    How can I get the object of the player, If I only have the name? Also how can I check if that player is offline/online? Thanks!
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or getPlayerExact() if you only want an exact match (not like /tell pem would find PEMapModder)
    EvolSoft, Dutok, iksaku and 2 others like this.
  3. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    I have this bug where:
    $isValid = $this->getServer()->getPlayer("Steve");
    var_dump("$isValid");
    returns null even though Steve is online and breathing. How do u fix this? This only happens from time to time.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    var_dump($isValid). With the quotes, you are converting it into string.
    EvolSoft likes this.
  5. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    with or without quotes, the same thing happens. so to avoid crashes, i always do $isValid instanceof Player
  6. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so may i ask again, whats causing this? the var_dump always return null
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You did something wrong. Are you sure Steve is online? And what is the complete code of that part?
  8. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    of course, its pointless if not.
    only this: $isValid = $this->getServer()->getPlayer("Steve"); which returns null. it only happens from time to time.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Run this?
    PHP:
    var_dump(
        
    $this->getServer()->getPlayer("Steve"),
        
    $this->getServer()->getPlayerExact("Steve"),
        
    $this->getServer()->getOnlinePlayers()
    );
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually you can't get the object of a player if he is offline. Unless you try to get an OfflinePlayer object, whose functions are more limited than a Player object because the player is offline (and you can't do things like sendMessage())
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    done. same result on first one.
  12. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so i loaded a new user named as JohnSmith. it didnt return null this time. it happens from time to time which sucks.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    All returned null? Even getOnlinePlayers()?
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    no. only
    $this->getServer()->getPlayer("Steve"),
    $this->getServer()->getPlayerExact("Steve"),

    which the getOnlinePlayers returned Steve and JohnSmith
  15. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    PHP:
    ($this->getServer()->getPlayer("Steve") instanceof Player// pocketmine\Player
  16. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Yes i know about this to avoid crashes if getPlayer returns null. Its just that it gets annoying when it returns null even though you know its not
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Same if you do var_dump($this->getServer()->getPlayer("Steve")) directly?
  18. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Yes
  19. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Im having trouble debug this as it just teleports me to a random player. No matter what player I tp to.
    PHP:
    public function issueCommand($sender$victem){
            
    $user = new Config($this->myuser "users/" strtolower($sender->getName() . ".yml"), Config::YAML);
            
    $mybalance $user->get("coins");
            
    $name $sender->getName();
            
    $name strtolower($name);
            
    $object $this->getServer()->getPlayer($victem);
            if(
    $mybalance 49){
                if(
    $object !== null and $object->isOnline()){
                    
    $sender->sendMessage("[Teleport] Teleporting you to $victem.");
                    
    $sender->sendMessage("-50 coins, teleport command cost.");
                    
    $mybalance $mybalance 50;
                    
    $user->set("coins"$mybalance);
                    
    $user->save();
                    
    $x $object->getY();
                    
    $y $object->getX();
                    
    $z $object->getZ();
                    
    $sender->teleport(new Vector3($x$y$z)); //Not teleporting to the object it randomly tps you.
                    
    return true;
                }
                else{
                    
    $sender->sendMessage("[Teleport] $victem is not online.");
                    return 
    true;


                }
            }
            else{
                
    $sender->sendMessage("[Teleport] You need at least 50 coins to teleport.");
                return 
    true;
            }
        }
    --------------------------
    command code:
    PHP:
    case "tpme":
                    
    $victem $args[0];
                    
    $this->issueCommand($sender$victem);
                    return 
    true;
                    break;

Share This Page

Advertisement