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

Custom player class

Discussion in 'Development' started by SavionLegendZzz, Apr 4, 2017.

  1. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    Ok so i have a custom player class for my Core plugin and i am trying to get the players UUID but it throws an error from the Raklib saying toString on null. Is there any other way i can get the players UUID ?

    Here is the code:
    PHP:
    <?php
    /**
     * Created by PhpStorm.
     * User: Savion
     * Date: 3/24/2017
     * Time: 6:15 PM
     */

    namespace Auth;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\network\SourceInterface;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\UUID;


    class 
    CustomPlayer extends Player{

        private 
    $loginTrys = [];

        private 
    $lastUuid;

        public function 
    __construct(SourceInterface $interface$clientID$ip$port)
        {
            
    parent::__construct($interface$clientID$ip$port);
        }


        public function 
    getPlugin(){
            return 
    Main::getInstance();
        }

        public function 
    isRegistered(){
            if(
    $this->getPlugin()->database->exists(strtolower($this->getName()))){
                return 
    true;
            }else{
                return 
    false;
            }
        }

        public function 
    isLoggedIn(){
            if(isset(
    $this->getPlugin()->loggedIn[$this->getName()])){
                return 
    true;
            }else{
                return 
    false;
            }
        }

        public function 
    sameUuid(){
            
    //have to fix ;P
            
    return false;
        }

        public function 
    login($type){
            if(
    $type === Main::UUID){
                
    $this->getPlugin()->loggedIn[$this->getName()] = $this->getName();
                
    $this->sendMessage(Main::PREFIX_GOOD.Main::LOGGED_IN_BY_UUID);
                
    $this->setNameTag($this->getName());
                
    date_default_timezone_set('UTC');
                if(isset(
    $this->loginTrys[$this->getName()])){
                    unset(
    $this->loginTrys[$this->getName()]);
                }
            }

            if(
    $type === Main::PASSWORD){
                
    $this->getPlugin()->loggedIn[$this->getName()] = $this->getName();
                
    $this->sendMessage(Main::PREFIX_GOOD.Main::LOGGED_IN_BY_PASSWORD);
                
    $this->setNameTag($this->getName());
                if(isset(
    $this->loginTrys[$this->getName()])){
                    unset(
    $this->loginTrys[$this->getName()]);
                }
            }
            
    $this->sendMessage(Main::PREFIX_GOOD."Today is ".date("D, F d, Y, H:i T")."!");
            
    $this->sendTitle(TextFormat::GOLD."Welcome to AzeronPE!"TextFormat::AQUA."Follow us on Twitter: @AzeronPE"20*320*3);
        }

        public function 
    logout(){
            unset(
    $this->getPlugin()->loggedIn[$this->getName()]);
        }
    }
     
  2. NIIP99

    NIIP99 Silverfish

    Messages:
    22
    GitHub:
    niip99
    In your CustomPlayer class, execute this:
    PHP:
    $this->getUniqueId();
    CustomPlayer extends Player class, getUniqueId() is inherited from parent class, Player.
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Why are you overriding player class? You don't need to do so at all...
     
    Muqsit likes this.
  4. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    I am wanting to add my own stuff to the Player class without modifying the actually software
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I highly recommend not using a custom player class unless you know what you are doing. PlayerCreationEvent can only be made use of in one plugin.

    Back to the question:
    PHP:
    Player::getUniqueId()->toString();
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You may not need to but he is. This thread isn't about discussing wether he should overwrite the player class but was merely a question about retrieving the players UUID.

    I understand that it's not recommended for plugin developers to make use of the ability to extend the player classes but the fact remains that the ability is there.
    Incorrect. It can be used in as many plugins as you like, you just have to make sure every new player class you use extends an existing one (preferably one that hasn't been extended yet so you don't loose part of the functionality) instead of PocketMines.
     
    Alemiz, jasonwynn10 and NIIP99 like this.
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    1. Seriously, the main question was Is there any other way i can get the players UUID? And yes, it is unneeded to override the player class to achieve that.

    2. Of course. Anything is possible in code. You'll need to check which plugins get enabled first and sequence PlayerCreationEvent as per it. But chances are, it'll still be a mess.
    Quote from PEMapModder
    "If more than one plugin does this, it will conflict. Only the plugin with the highest priority wins.

    Unless it's a private plugin, avoid using it. PlayerCreationEvent was originally for things that are really impossible to do without using it. For example, to override the sendTranslation function."

    So what SOF3 said was correct either ways. It's not needed, and if the sole purpose is obtaining UUID, then it's just useless, unneeded, not recommended.
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The point I was trying to make is that if you have to ask what is asked in the OP of this thread, you definitely don't know how to use the things that makes overriding a player class necessary.
    Which is already impossible, unless your plugins have dependency on one another in a hierarchy chain, which is a disaster. In that case, they aren't plugins by definition. (A extends B extends C extends D... So you can't remove any one of them, so it's actually a chain of plugins that can only be cut in the middle to take half of them away... Do you call them "plugins" (plural)?) And you have to make sure your handlers of PlayerCreationEvent are called in the correct order, which is quite difficult in the current event API... And you have to be aware not to accidentally override methods from another plugin...
    So, theoretically it is possible, but practically it is not possible, or at least, unfeasible.
    This does not really reply to his point ;)
     
  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.