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

It seems I don't understand how to use foreach...

Comments in 'Plugin Development' started by args, Sep 11, 2014.

  1. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    Hey, I'm trying to scatter my players across 12 locations, but they all end at location 12 somehow, I'm guessing I just understood the use of the foreach loop, maybe its different in Java and PHP?

    Here's my code, its pretty obvoious what im trying to accomplish but it does not work the way id like, what should i do to fix it?
    Code:
     $ind = 0;
           foreach(Server::getInstance()->getOnlinePlayers() as $p){
               $ind = $ind +1;
                if($ind = 1){
                $p->teleport($pos1);
                }
               if($ind = 2){
                   $p->teleport($pos2);
               }
               if($ind = 3){
                   $p->teleport($pos3);
               }
               if($ind = 4){
                   $p->teleport($pos4);
               }
               if($ind = 5){
                   $p->teleport($pos5);
               }
               if($ind = 6){
                   $p->teleport($pos6);
               }
               if($ind = 7){
                   $p->teleport($pos7);
               }
               if($ind = 8){
                   $p->teleport($pos8);
               }
               if($ind = 9){
                   $p->teleport($pos9);
               }
               if($ind = 10){
                   $p->teleport($pos10);
               }
               if($ind = 11){
                   $p->teleport($pos11);
               }
               if($ind = 12){
                   $p->teleport($pos12);
               }
            }
    
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Use == or === for comparisons not =. Also a for loop would be better and getInstance() is bad practice.
  3. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    Oh wow, I did know that, i guess i just had a bit of a brain dead moment ahah! thank you.
    But can you please explain to me what I should use instead of Server::getInstance()? and how I could use a for loop for the players?

    thank you :)
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you are in the context of (i.e. "the class that extends" in this case) PluginBase, use $this->getServer().
    If you are in the context of PluginTask, use $this->getOwner()->getServer().
    If you are in the context of something else, pass the Server object to the constructor of the class, or pass the object in the method parameters.

    Also, instead of making
    PHP:
    $pos1 = new Position(blah);
    $pos2 = new Blah();
    $pos3 blah;
    ...
    if(
    $ind === 1){
      
    // teleport to $pos1
    }
    if(
    $ind === 2){
      
    // teleport to $pos2
    }
    ...
    You can try:
    PHP:
    $pos = []; // same as array()
    // either define the positions one by one if necessary
    $pos[0] = new Position(blah);
    $pos[1] = new Blah();
    $pos[2] = blah;
    // or use a formula to calculate the coords, initializing $x as a value and adding another value to $x and use the newest value each time
    $x 10;
    for(
    $i 0$i $number_of_positions$i++){
      
    $x += 3;
      
    $pos[$i] = new Position($x600$level);
    }
    // then to teleport players
    $players $this->getServer()->getOnlinePlayers(); // get an array of all online players
    foreach(array_values($players) as $ind => $player){ // array_values() makes the array elements have keys from 0 to (size of array - 1), allowing direct use of the array keys as $ind instead of counting it somewhere else
      // either use the $pos array
      
    $player->teleport($pos[$ind]); // $ind increases every time since an array key is unique in the array, and it is simplified into purely ascending integers because of array_values()
      // or calculate the position instead of pre-defining the positions
      
    $player->teleport(new Position(10 $ind 3600$level));
    }
    Last edited: Sep 11, 2014
    args likes this.
  5. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    thank you
    Last edited: Sep 11, 2014
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Look at the edited post.

    Edit: You must use array_values() because (I forgot what it is exactly) getOnlinePlayers() does not return an array with clean integral ascending keys.
    args likes this.
  7. args
    Offline

    args New Member

    Joined:
    Jun 14, 2014
    Posts:
    47
    Minecraft User:
    args
    Thanks for such an informative post!
    Funnily enough they taught us about using arrays like that this week in c# at the uni i go to :) Halfway through writing the code I realized i could try and use them but I was almost done X)

    For creating the positions and teleporting to them it will work great, but for entering what the coords are, I couldn't think of a formula to write that would be faster than typing them all manually because of the locations they contain.

    The part about using array_values is very helpful, thanks so much :)
  8. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, we told him that already. :)

Share This Page

Advertisement