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

Help with Minigame.

Comments in 'Plugin Development' started by PalkiaDudeYT, Sep 17, 2016.

  1. PalkiaDudeYT
    Offline

    PalkiaDudeYT New Member

    Joined:
    Aug 30, 2015
    Posts:
    33
    Minecraft User:
    PalkiaDude
    How do I choose 1 player out of 6 players to perform a task while the other 5 perform a different task?
  2. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    $player = $players[mt_rand(0, count($players)-1];

    When you use Level::getPlayers() or Server::getOnlinePlayers you cant do that for some strange reason, to bypass it do

    PHP:

    $numb 
    mt_rand(05);
    $c 0;
    foreach(
    $level->getPlayers() as $p){
    if(
    $numb == $c){
    $player $p;
    break;
    }else{
    $c++;

    }}

  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Defining and performing incrementing on $c is redundant in this case as $c is tracking array keys but the foreach loop does this automatically
    PHP:
    foreach($array as $key => $value# Or $c => $p in your case
    Your code can be shortened to
    PHP:
    $numb mt_rand(05);
    foreach(
    $level->getPlayers() as $i => $p) {
       if(
    $numb != $i) continue;
       return 
    $p;
    }
    Or even shorter
    PHP:
    $players $level->getPlayers();
    $p $players[mt_rand(0count($players) - 1)];
    Because I don't see no reason why it should not work. Can you show a way to reproduce it?
    applqpak likes this.
  4. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Level::getPlayers()[0] always triggers the error undefined index.

    Oh sorry, I thought that is is just a one dimensional array
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Why don't you use array_rand?
    PHP:
    $players $this->getServer()->getOnlinePlayers();

    $random $players[array_rand($players)];
    applqpak likes this.
  6. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    array_rand is slower and returns weird results.
    applqpak and Primus like this.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    You're true but if you are not using it constantly then that doesn't matter.
    Legoboy0215 and applqpak like this.

Share This Page

Advertisement