1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Get all levels

Discussion in 'Development' started by SkySeven, Apr 20, 2017.

Thread Status:
Not open for further replies.
  1. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    Hello,
    How to get all levels even if are not load ?
    getLevels() return only those who are loaded.
    Thank you in advance :)
     
  2. thelucyclub

    thelucyclub Spider Jockey

    Messages:
    28
    GitHub:
    thelucyclub
    I am not quite sure, but you could try loading all levels on startup, so there won't be any lagg when a player try o join.
     
  3. EdwardHamHam

    EdwardHamHam Skeleton

    Messages:
    962
    GitHub:
    edwardhamham
    I think that the ManyWorlds plugin can load worlds after start up. But the easiest thing would be to just load all levels on startup like @thelucyclub suggested. You can only TP/view loaded levels AFAIK
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You could use scandir on the worlds folder and use $server->loadLevel($name) to load it and be able to use it as a level object. Example code:
    PHP:
    $levelNamesArray scandir($server->getDataPath() . "worlds/");
    foreach(
    $levelNamesArray as $levelName) {
      if(
    $levelName === "." || $levelName === "..") {
        continue;
      }
      
    $server->loadLevel($levelName); //Note that this will return false if the world folder is not a valid level, and could not be loaded.
    }
    $levels $server->getLevels(); //Should return all levels in your server.
    Note that it may be a good idea to unLoad the levels that are not used after it again.
     
    HimbeersaftLP and corytortoise like this.
  5. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Dang, I just wanted to day that :eek::p
     
  6. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    thank's you for your answer ! :)
    PHP:
      if($levelName === "." || $levelName === "..") {
    }
    This line is use for why?
     
  7. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Why are you all using super complex methods with scanning the folder? $server->getLevels() returns an array of all levels whether they are loaded or not
     
  9. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    SOFe, corytortoise and Jack Noordhuis like this.
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    getLevels() returns Level objects. What do you expect the Level object to contain if the level isn't loaded?
     
    HimbeersaftLP and Primus like this.
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    what if the level is just generated, but not loaded?
     
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    A Level object is only called from generateLevel() or loadLevel(). The code following the new Level() call in generateLevel() is covers all those in loadLevel(). Especially note that LevelLoadEvent is also called. This shows that generateLevel() implicitly loads the level too. Therefore, a Level object creation always follows a LevelLoadEvent call.
    Now let's look at unloadLevel. On line 948 the level object is removed. Therefore I conclude that the levels array only contains loaded levels.
    Actually, generating a level is just to initialize it and let it generate like the normal infinite world chunk generation. There is not really much difference.
     
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.