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

Solved Array $player

Comments in 'Plugin Development' started by WitherTube, May 21, 2016.

  1. WitherTube
    Offline

    WitherTube New Member

    Joined:
    Dec 21, 2015
    Posts:
    44
    Minecraft User:
    Wither_Tube
    Hello
    i have a question. I want to get all players in the Variable players because i want to teleport each player to
    a spawnpoint. How i can make it ? Please with a Code thanks!
  2. TheBison_
    Offline

    TheBison_ New Member

    Joined:
    Mar 31, 2014
    Posts:
    46
    Minecraft User:
    TheBisonXD_
    Code:
    $players = $this->getServer()->getOnlinePlayers();
    foreach($players as $player)
    {
         $player->teleport($this->getServer()->getDefaultLevel()->getSafeSpawn());
    }
    ^^
    applqpak, Primus and WitherTube like this.
  3. WitherTube
    Offline

    WitherTube New Member

    Joined:
    Dec 21, 2015
    Posts:
    44
    Minecraft User:
    Wither_Tube
    Thanks man that help !
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    I would load the chunk before teleporting the first player, so they won't bug around.
    applqpak, Primus and Jazzwhistle like this.
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    and again...
    PHP:
    $c 0;
    foreach(
    $players as $p){
    $c++; 
    if(
    $c == 1){
    // 1. spawnpunkt
    }

    if(
    $c == 2){
    // 2. spawnpunkt
    }

    // or create an array like this: [1 => new Position()..., 2 => new Pos()];
    // and do $p->teleport($pos[$c]);
    }
  6. Jazzwhistle
    Offline

    Jazzwhistle Notable Member

    Joined:
    Dec 27, 2014
    Posts:
    364
    Minecraft User:
    Awzaw
    Hi, is this needed, and does it really fix tp glitches? In Player.php on line 84 it says for teleportImmediate():

    * This method may not be reliable. Clients don't like to be moved into unloaded chunks.
    * Use teleport() for a delayed teleport after chunks have been sent
    applqpak and Primus like this.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    o_O
    jojoe77777, applqpak and Extreme_Heat like this.
  8. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    what?
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    So, you're going to manually handle those spawn points? What if they are more than 100?
    applqpak likes this.
  10. WitherTube
    Offline

    WitherTube New Member

    Joined:
    Dec 21, 2015
    Posts:
    44
    Minecraft User:
    Wither_Tube
    There you have right but i want this method only for 5 players xD . If you have a method could you post it here please ? ^^
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Something like @MyNameIsTriXz said in the comment
    PHP:
    # Init positions
    $spoints = [
         
    => [
              
    "free" => true,
              
    "pos" => new Position(000);
         ]
         
    => [
              
    "free" => true,
              
    "pos" => new Position(000);
         ]
         
    => [
              
    "free" => true,
              
    "pos" => new Position(000);
         ]
         
    => [
              
    "free" => true,
              
    "pos" => new Position(000);
         ]
         
    => [
              
    "free" => true,
              
    "pos" => new Position(000);
         ]
    ];

    # Teleport
    $tped false;
    foreach(
    $spoints as $i => $sp){ // You could also use '&$sp' here
         
    if($sp["free"] === false) continue;
         
    $player->teleport($sp["pos"];
         
    $tped true;
         
    $spoints[$i]["free"] = false;
    }
    if(!
    $tped){
         throw new \
    RuntimeException("No free spawn points left."); // Of course you don't have to 'throw' anything :D
    }
    applqpak and WitherTube like this.
  12. WitherTube
    Offline

    WitherTube New Member

    Joined:
    Dec 21, 2015
    Posts:
    44
    Minecraft User:
    Wither_Tube
    Thanks for it ! Can i do it like this
    PHP:

    public function onMet()
    {
    $players $this->getServer()->getOnlinePlayers();
    foreach(
    $players as $player)
    {
    $player[0]->teleport(new position($this->x1 $this->y1 $this->z1) ;
    $player[1]->teleport(new position($this->x2 $this->y2 $this->z2) ;
    $player[2]->teleport(new position($this->x3 $this->y3 $this->z3) ;
    $player[3]->teleport(new position($this->x4 $this->y4 $this->z4) ;
    $player[4]->teleport(new position($this->x5 $this->y5 $this->z5) ;
    }
    }
    }
  13. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    you need to understand your code
    applqpak likes this.
  14. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Why don't you just use for()? Also, please learn what foreach() does.
    applqpak likes this.
  15. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    for is slower than foreach
  16. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    I don't think a bigger foreach() part is faster than a tiny for() part in a plugin.
    applqpak likes this.
  17. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    foreach returns a function for each element in an array, so it executes it for each player.
  18. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    PHP:
    $pos = [new Position(), new Positon(), new Position()];
    $c 0;
    foreach(
    $this->getServer()->getOnlinePlayers() as $p){
    $p->teleport($pos[$c]);
    $c++
    }
    WitherTube likes this.
  19. WitherTube
    Offline

    WitherTube New Member

    Joined:
    Dec 21, 2015
    Posts:
    44
    Minecraft User:
    Wither_Tube
    So i can make it like this :
    PHP:
    $pos = [new Position($this->x1 $this->y1 $this->z1), new Positon($this->x2 $this->y2 $this->z2), new Position($this->x3 $this->y3 $this->z3)];
    $c 0;
    foreach(
    $this->getServer()->getOnlinePlayers() as $p){
    $p->teleport($pos[$c]);
    $c++
    }
  20. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Yes
    WitherTube likes this.

Share This Page

Advertisement