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

Teleport 8 connected players. Each player must be different...

Comments in 'Plugin Development' started by LCraftPE, Sep 2, 2016.

  1. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    Hello,
    I come back because I think my last thread was not precise enough...
    First, I 'll explain what I want to do and then I'll show you my code (which obviously doesn't work...).

    I would like teleport 8 connected players through a command. Each player must be different ... (this is what I can not do).

    My code :
    PHP:
        public function getPlayers(){
          
    $Online $this->getServer()->getOnlinePlayers();

          
    $rand1 array_rand($Online);
          
    $rand2 array_rand($Online);
          
    $rand3 array_rand($Online);
          
    $rand4 array_rand($Online);
          
    $rand5 array_rand($Online);
          
    $rand6 array_rand($Online);
          
    $rand7 array_rand($Online);
          
    $rand8 array_rand($Online);

          list(
    $rand1$rand2$rand3$rand4$rand5$rand6$rand7$rand8) = $this->verifyRand($rand1$rand2$rand3$rand4$rand5$rand6$rand7$rand8);

          if(isset(
    $Online[$rand1]) && isset($Online[$rand2]) && isset($Online[$rand3]) && isset($Online[$rand4]) && isset($Online[$rand5]) && isset($Online[$rand6]) && isset($Online[$rand7]) && isset($Online[$rand8])){
            
    $player1 $Online[$rand1];
            
    $player2 $Online[$rand2];
            
    $player3 $Online[$rand3];
            
    $player4 $Online[$rand4];
            
    $player5 $Online[$rand5];
            
    $player6 $Online[$rand6];
            
    $player7 $Online[$rand7];
            
    $player8 $Online[$rand8];

            
    $this->launchTournament($player1"Arene1");
            
    $this->launchTournament($player2"Arene1");
            
    $this->launchTournament($player3"Arene2");
            
    $this->launchTournament($player4"Arene2");
            
    $this->launchTournament($player5"Arene3");
            
    $this->launchTournament($player6"Arene3");
            
    $this->launchTournament($player7"Arene4");
            
    $this->launchTournament($player8"Arene4");
          }
        }

        public function 
    verifyRand($r1$r2$r3$r4$r5$r6$r7$r8){
          
    $rand = [$r1 => true$r2 => true$r3 => true$r4 => true$r5 => true$r6 => true$r7 => true$r8 => true];
          if(
    count($rand) <= 7){
            if(
    count($this->getServer()->getOnlinePlayers()) > 7){
              
    $r1 array_rand($this->getServer()->getOnlinePlayers());
              
    $r2 array_rand($this->getServer()->getOnlinePlayers());
              
    $r3 array_rand($this->getServer()->getOnlinePlayers());
              
    $r4 array_rand($this->getServer()->getOnlinePlayers());
              
    $r5 array_rand($this->getServer()->getOnlinePlayers());
              
    $r6 array_rand($this->getServer()->getOnlinePlayers());
              
    $r7 array_rand($this->getServer()->getOnlinePlayers());
              
    $r8 array_rand($this->getServer()->getOnlinePlayers());
              return 
    $this->verifyRand($r1$r2$r3$r4$r5$r6$r7$r8);
            }
          }else{
            return 
    $rand;
          }
        }
    Sorry for my bad english, I am French...
  2. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    I delete the other thread...
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    public function getTeam($size 8) : array {
       
    $team array_chunk($this->getServer()->getOnlinePlayers(), $size)[0];
       
    shuffle($team);
       return 
    $team;
    }

    $team $this->getTeam();

    foreach(
    $team as $i => $p$this->launchTournament($p"Arene" . ($i 1) );

    Code:
    class Server {
       public function getOnlinePlayers() {
         return ["Steve", "Bob", "Alex", "Jake", "Micheal", "Jorge", "Olivia", "Andreas", "Jim", "Tom", "Bill"];
       }
    }
    
    class Test {
    
       public function getServer() {
         return new Server;
       }   
       
       public function getTeam($size = 8) {
        $team = array_chunk($this->getServer()->getOnlinePlayers(), $size)[0];
        shuffle($team);
        return $team;
       }
    
       public function entry() {
         $team = $this->getTeam();
         
         foreach($team as $i => $p) $this->launchTournament($p, "Arene" . ($i + 1) );
       }
    
       public function launchTournament($player, $arena) {
         print "$player entering $arena... </br>";
       }
       
    }
    
    $t = new Test();
    $t->entry();
    
    Last edited: Sep 2, 2016
    wolfdale, applqpak and Survingo like this.
  4. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    I try this alone and it works ! Thanks you ! I'll soon try for 8 players...
    I 'll let you know.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Just a reminder, array_rand returns a KEY, not the value. This means that if you want to get a random value from the array, this is the proper (but not only) way.

    PHP:
    $array[array_rand($array)];
    For more info read the documentation on array_rand @ php.net
  6. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    That's how they used it.
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    What I am implying is that there is no need to define so many variables :).
  8. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    Hello,
    I tested with 8 players and it teleports players on eight arenas ... But it must have 2 players per arena on the first 4 arenas .
    Player 1 and 2 => Arene1
    Player 3 and 4 =>Arene2
    Player 5 and 6 => Arene3
    Player 7 and 8 => Arene4
  9. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    foreach($team as $i => $p$this->launchTournament($p"Arene" . (floor($i/2) + 1) );
    Primus likes this.
  10. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Implausible to tell that from your code and content of this thread. As your slaves we will help you to change few bytes of code for you, I guess it's extremely hard for you. *cough* *cough* :)
  11. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    Hello,
    Hello,
    I was helped by Ad5001 ... I try to make very simple so that you understand my messages.
    I did not want to be nasty in my last post , sorry...
    I am a novice in php... You 've never been ?

    Thank you for your answer , you are very nice to help me...
  12. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    Thank you, you use functions that I had never learned... We always learn a little more :)
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    You never start writing before learning to read, it's really important that you learn php at first. Appreciate the resources you can find on web for free.
  14. LCraftPE
    Offline

    LCraftPE New Member

    Joined:
    Nov 16, 2014
    Posts:
    30
    Minecraft User:
    LCraftPE
    I learned the basics of PHP on Codacademy , you know a site to deepen my knowledge ?
  15. archie426
    Offline

    archie426 Active Member

    Joined:
    Apr 26, 2015
    Posts:
    193
    Minecraft User:
    archie426
    Maybe somone should introduce a plugin enabling this, like BetterTeleport or something like that (I just made the name up)
    At last I have learnt something. Probably wont again in a month

Share This Page

Advertisement