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

How to get random online player and save name to config

Comments in 'Plugin Development' started by Kabluinc, Aug 30, 2016.

  1. Kabluinc
    Offline

    Kabluinc New Member

    Joined:
    Mar 2, 2016
    Posts:
    29
    Minecraft User:
    Kabluinc
    How would i get a random online player and save his name to player.yml

    Thanks for your help
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    # Get
    $players $this->getServer()->getOnlinePlayers();
    if((
    $c count($players)) > 0) {
       
    $player $players[mt_rand(0$c 1)];
    }
    I suggest to use \pocketmine\utils\Config
    PHP:
    # Save
    $data = [
       
    "name" => $player->getName(),
       
    "online" => $player->isOnline() ? "true" "false"
    ];
    $playerFile = new Config($this->getDataFolder()."players/".strtolower(trim($player->getName())).".yml"Config::YAML$data);
    $playerFile->save();
    Use mkdir to create 'players' folder. To get random player from array you can also use array_rand
    PocketKiller and Survingo like this.
  3. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Why not array_rand()?
  4. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
  5. Kabluinc
    Offline

    Kabluinc New Member

    Joined:
    Mar 2, 2016
    Posts:
    29
    Minecraft User:
    Kabluinc
    To adapt it to my plugin i used this code:
    PHP:
    $arena $config->get("arenas");
                    
    $levelArena $this->plugin->getServer()->getLevelByName($arena);
                    if(
    $levelArena instanceof Level)
                    {
                        
    $players $levelArena->getPlayers();
    if((
    $c count($players)) > 0) {
       
    $player $players[mt_rand(0$c 1)];
       
    $name $player->getName();
       
    $judge = new Config($this->plugin->getDataFolder() . "/judge.yml"Config::YAML);
       
    $judge->set("judge"$name);
       
    $judge->save();
    It retured an error of something related to array to string conversion when the game started

Share This Page

Advertisement