Hello, How to get all levels even if are not load ? getLevels() return only those who are loaded. Thank you in advance
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.
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
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.
thank's you for your answer ! PHP: if($levelName === "." || $levelName === "..") {} This line is use for why?
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
Incorrect. It only returns loaded levels. https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Server.php#L874 https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Server.php#L948 https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Server.php#L996
getLevels() returns Level objects. What do you expect the Level object to contain if the level isn't loaded?
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.