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

The correct way to teleport a player to another level

Comments in 'Plugin Development' started by RekkuzaRage, May 17, 2015.

  1. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    What is a good way to teleport player/s to a specific level? Examples appreciated.
  2. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    PHP:
    if(Server::getInstance()->loadLevel($lvl) != false){ //Level already loaded on PocketMine
       
    $player->teleport(Server::getInstance()->getLevelByName($lvl)->getSafeSpawn());
    }else{ 
    //Level not loaded on PocketMine
       
    Server::getInstance()->loadLevel($lvl); //Loading the level now...
       
    $player->teleport(Server::getInstance()->getLevelByName($lvl)->getSafeSpawn());
    }
    You can also use a PocketMine Position instancd to teleport a player to another world with specified coordinates.
    This way prevents crashes if the world hasn't been loaded
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Avoid using Server::getInstance(). it may sometimes break.
    Also, if($blah) is simpler and probably faster than if($blah != false), and does exactly the same thing.
    But wait...
    You are attempting to load the level on the first line. It only returns false if loading failed. So what is the point of attempting to load again after you know that the level cannot be loaded, as of else{}?
  4. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    There is no performance difference between if(!$boolean) and if($boolean != false){
    Then getLevel returns false if the world is not loaded and so you must try to load it before.
    Maybe this code is better:
    PHP:
    if(Server::getInstance()->loadLevel($lvl) != false){ //Level already loaded on PocketMine
       
    $player->teleport(Server::getInstance()->getLevelByName($lvl)->getSafeSpawn());
    }else{ 
    //Level not loaded on PocketMine
       
    if(Server::getInstance()->loadLevel($lvl) != false){ //Loading the level now... (if it still returns false, the world can't be loaded because it's corrupted or it doesn't exist...)
          
    $player->teleport(Server::getInstance()->getLevelByName($lvl)->getSafeSpawn());
       }else{
         
    //Level load error
       
    }
    }
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Last time, no Server::getInstance() please.
    The first line is loadLevel not isLevelLoaded. And you call loadLevel on line 4 again after loadLevel returns false on line 1.
    And I am very sure if($server->loadLevel($lvl)) is faster than if($server->loadLevel($lvl) != false), even if negligible.
  6. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Ok, if you say so :p

Share This Page

Advertisement