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

Get one random Player out of a List

Comments in 'Plugin Development' started by CraftYourBukkit, May 2, 2015.

  1. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Hey there.

    How do I get a random Player out of a List? I'm saving Players into a YAML Config when doing a command. If there are 3 or more players in the list, one of them will be picked randomly and the player will get a sword.

    I already done the List and the command, the only thing I need is the random pick.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    /** @var string[] $list */
    $selected = [];
    for(
    $i 0$i 3$i++){
      while(
    in_array($k mt_rand(0count($list) - 1))); // using a while to avoid selecting duplications.
      
    $selected[] = $k;
    }
    $selections array_map(function($k) use($list){
      return 
    $list[$k];
    }, 
    $selected);
    CraftYourBukkit likes this.
  3. sekjun9878
    Offline

    sekjun9878 Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    108
    Plugins:
    1
    Minecraft User:
    sekjun9878
    Or alternatively you could just use array_rand().
  4. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    which sometimes return the same variable in different interval. its not that great. hoping to test @PEMapModder's method.
    PEMapModder likes this.
  5. sekjun9878
    Offline

    sekjun9878 Staff Member PocketMine Team

    Joined:
    Aug 22, 2013
    Posts:
    108
    Plugins:
    1
    Minecraft User:
    sekjun9878
    That's not one of OP's requirements but fair enough :) Also, I would strongly advise against using in_array (O(n)) - use array keys and isset(O(1)) instead :)
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Hey, thanks for your Post but I still don't know how to do it :/
    So, what I wanna do is:
    PHP:
    public function startGame(){
                
    $this->AStarted true// Sets the Game on true/started
               
               // Pick a random player out of $this->inA and teleport him to 0 0 0, teleport the other players to 1 1 1(Example)
            
    }
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then $this->inA is the $list. And for each $selected as $select, call teleport on $list[$select]
  8. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    public function startGame(){
    $this->AStarted true// Sets the Game on true/started $list = $this->inA->getAll(); $selected = [];for($i = 0; $i < 3; $i++){ while(in_array($k = mt_rand(0, count($list) - 1))); // using a while to avoid selecting duplications.$selected[] = $k;}$selections = array_map(function($k) use($list){ return $list[$k];}, $selected);

    foreach($selected as $select){
    $list[$select]->teleport(...);
    }
    }
    Is that correct?
    Sorry but I don't code for that long and don't know much so yea.. But well thanks for your help! :)
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Use my code to define things...

Share This Page

Advertisement