How can I pick a random player from all online players? And the player must have the permission x.perm
There is a rule in this section about making posts... Try it on your own first and show us your results
Here ya go: Player::hasPermission("x.perm"); Player::getServer()->getOnlinePlayers(); mt_rand() array_keys()
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)];
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];
$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(0, count($kplayers) - 1)]]; So.. PHP: $players = [];foreach (Server::getInstance()->getOnlinePlayers() as $pl) { if ($pl->hasPermission("x.perm")) { $players[] = $pl; }}$randomplayer = $players[mt_rand(0, count($players) - 1)];
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); }}
Note that as of PHP 7.1 (although PocketMine is not using it yet), array_rand() uses the same algorithm as mt_rand().