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

how to get player from args

Discussion in 'General discussion' started by Thelastnobody, May 25, 2021.

  1. Thelastnobody

    Thelastnobody Creeper

    Messages:
    1
    GitHub:
    thelastnobody00
    i am trying to get player from args .how i can do that like @a,@e,@r@p or PlayerName
     
  2. Axon

    Axon Zombie

    Messages:
    276
    If your command is something like this `/fly Erik1206`
    You can get the player Instance like this `$this->getServer()->getPlayer($args[1])`
    Selectors aren't supported in PMMP.
    You can add `@a` like I did in VoteParty like this:

    PHP:
    if($args[1] == "@a"){
        foreach(
    $this->getServer()->getOnlinePlayers() as &$OnPlayer){
            
    // Do something with player, this will repeat for all the players           
        
    }
    }
     
    Agent and minijaham like this.
  3. ComorDev

    ComorDev Spider

    Messages:
    11
    GitHub:
    comordev
    Sorry to answer late
    PHP:
    public function execute(CommandSender $senderstring $commandLabel, array $args){
            if(
    $sender instanceof Player) {
                if (isset(
    $args[0])) {
                    if (
    $args[0] === "@a") {
                        foreach (
    Server::getInstance()->getOnlinePlayers() as $player) {
                            
    //todo
                        
    }
                    } elseif (
    $args[0] === "@e") {
                        foreach (
    Server::getInstance()->getLevels() as $level) {
                            foreach (
    $level->getEntities() as $entity) {
                                
    // todo
                            
    }
                        }
                    } elseif(
    $args[0] === "@r"){
                        
    $player array_rand(Server::getInstance()->getOnlinePlayers(), 1);
                        
    $player->sendMessage("lol");
                    } else {
                        
    $searchplayer Server::getInstance()->getPlayer(strtolower($args[0]));
                        if(
    $searchplayer instanceof Player){
                            
    $searchplayer->sendMessage('xD');
                        } else {
                            return 
    false;
                        }
                    }
                }
            }
        }
     
    Last edited: May 28, 2021
  4. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Can't you just array_rand all the player online?
    I'm sorry if I sounded rude, but I was just wondering if there is any difference between your method and my method
     
  5. ComorDev

    ComorDev Spider

    Messages:
    11
    GitHub:
    comordev
    Hello, I'm sorry, I didn't know about this method
     
    minijaham likes this.
  6. ComorDev

    ComorDev Spider

    Messages:
    11
    GitHub:
    comordev
    There is no difference in these methods.
     
    minijaham likes this.
  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.