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

All players but not ...

Discussion in 'Development' started by #A6543, Mar 8, 2017.

  1. #A6543

    #A6543 Zombie

    Messages:
    267
    How can I pick a random player from all online players? And the player must have the permission x.perm
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    There is a rule in this section about making posts...
    Try it on your own first and show us your results
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Here ya go:
    • Player::hasPermission("x.perm");
    • Player::getServer()->getOnlinePlayers();
    • mt_rand()
    • array_keys()
     
    corytortoise likes this.
  4. #A6543

    #A6543 Zombie

    Messages:
    267
    Something like this?
    PHP:
    $players $this->getServer()->getOnlinePayers();
    foreach (
    $players as $player) {
    if(!
    $player->hasPermission("x.perm")) {
    unset(
    $players[$player]);
    }
    $playersselect $players[array_rand($players)];
     
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Close, but try something more along the lines of this:
    PHP:
    $players $this->getServer()->getOnlinePlayers();
    $rand mt_rand(0,count($players)-1);
    $selected $players[$rand];
     
    Muqsit likes this.
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    This doesn't include the perm thingy
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    $selected cannot be a player.
    This is a schematic of how $players looks like:
    PHP:
    $players = [
        
    46 => Player::class,
       
    94 => Player::class,
       
    //Player::getRawUniqueId() => Player::class
    ];

    //What you can use is...
    $kplayers array_keys($players);
    $selected $players[$kplayers[mt_rand(0count($kplayers) - 1)]];
    So..
    PHP:
    $players = [];
    foreach (
    Server::getInstance()->getOnlinePlayers() as $pl) {
        if (
    $pl->hasPermission("x.perm")) {
            
    $players[] = $pl;
        }
    }

    $randomplayer $players[mt_rand(0count($players) - 1)];
     
  8. #A6543

    #A6543 Zombie

    Messages:
    267
    I want to make something like VIP join. But it kicks the VIP and the normal player :/
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You need to listen to the PlayerKickEvent for this one.
    PHP:
    public function onKick(PlayerKickEvent $event){
        
    $player $event->getPlayer();
        if (!
    $player->loggedIn and !$player->isBanned() and $player->hasPermission("x.perm")) {
            
    $event->setCancelled(true);
        }
    }
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Note that as of PHP 7.1 (although PocketMine is not using it yet), array_rand() uses the same algorithm as mt_rand().
     
  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.