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

Teleport the user to a random position

Comments in 'Plugin Development' started by applqpak, Jun 23, 2016.

  1. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Hi! How would I teleport the player to a random *safe* position? I've searched Google and PocketMine Forums with no avail. Thanks in advance!
  2. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Perhaps look for two air blocks above y=60? The X and Z would not matter.
    applqpak likes this.
  3. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Thanks! :D
  4. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    My other suggestion is search for highest solid block below y=80 (Because of those people creating giant dirt tower in every survival server) if that method fails. I did not consider that there could be 5 air blocks below that...
    (Damn, what a spam!)
    applqpak likes this.
  5. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Not save: $player->teleport(new Position(mt_rand(0, 100000), mt_rand(0, 60), mt_rand(0, 100000));
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    mt_rand(0100000)
    Great move.

    Use loop for this
    PHP:
    $try 2// Check twice
    $radius 500;
    $minRadius 50;
    $pos null;
    $level $player->getLevel();
    do {
       
    $x mt_rand($minRadius$radius);
       
    $z mt_rand($minRadius$radius);
       for(
    $y 0$y 128$y++) {
          if(
    $level->getBlockIdAt($x$y$z) === && $level->getBlockIdAt($x$y+1$z) === 0) {
             
    $pos = new Position($x$y$z$level); 
             
    $found true;
          }
       }
    } while (!
    $found and $tries $try);
    if(
    $pos != null$player->teleport($pos);
    else 
    $player->sendMessage("No valid safe spawn was found");
    applqpak likes this.
  7. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Thanks!
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
  9. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    applqpak likes this.
  10. AvivShopenCraft
    Offline

    AvivShopenCraft New Member

    Joined:
    Oct 14, 2015
    Posts:
    73
    Minecraft User:
    AvivShopen
    i think he can do
    PHP:
    $player->teleport($level->getSafeSpawn(new Vector3(mt_rand(01000000), $y(no matter what is the Y), mt_rand(01000000)));
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Just a suggestion... Like the way to check if the spawn is actually safe

Share This Page

Advertisement