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

Server::getInstance()->unloadLevel();

Comments in 'Plugin Development' started by Samueljh1, Jan 31, 2015.

  1. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    Guys, how do you do
    PHP:
    Server::getInstance()->unloadLevel(); ?
    I used this for my skywars plugin:
    PHP:
    Server::getInstance()->unloadLevel('Skywars");
    This works fine for loadLevel(), but when i do unloadLevel(), i get:

    Code:
    31.01 09:56:36 [Server] CRITICAL Could not pass event pocketmine\event\player\PlayerMoveEvent to Teleport v1: Argument 1 passed to pocketmine\Server::unloadLevel() must be an instance of pocketmine\level\Level, string given, called in /plugins/SpawnTeleport/src/Teleport/main.php on line 1035 and defined on Teleport\main
  2. Exxarion
    Offline

    Exxarion Active Member Plugin Developer

    Joined:
    Jan 5, 2014
    Posts:
    242
    Plugins:
    1
    Minecraft User:
    Notch
    Server::getInstance is bad practice, thus you should not use it.
  3. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    How can i reload a level then?
  4. Exxarion
    Offline

    Exxarion Active Member Plugin Developer

    Joined:
    Jan 5, 2014
    Posts:
    242
    Plugins:
    1
    Minecraft User:
    Notch
    You need to Add your server instance and pass it as a parameter.
  5. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    example plz? :3
  6. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    There are many ways, but I will only list the most common 3:
    • Server::getInstance() This is considered as a bad practice because includes the usage of a static property, I don't know why it's included in the API of PocketMine, but don't use it anyways xD
    • $this->getServer() This is allowed in many places, you can access this Safe property inside any class that extends the PluginBase one.
    • $player->getserver() The same as above, but accessed within a player object, it's also Safe.
  7. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    The actual issue is that you need to do Server::getInstance()->unloadLevel(Server::getInstance()->getLevelByName("SkyWars"));

    Although statically accessing the Server is bad practice, this won't cause an issue.
  8. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    lel no, i mean get a level instance! :3 p.s what is the difference between Server::getInstance() and the others?
  9. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    ok ty :)
  10. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Because PocketMine uses internally and there are some legitimant uses for it.
    iksaku likes this.
  11. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    That the one that you're using is considered as a BAD PRACTICE xD it's the same way to access the API, but with a different start point xD
  12. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    OK! :)

    P.s just a random question,

    In the:

    PHP:
    public function __construct()
    What would you put here to get the Level instance so you can store it as a var?

    PHP:
    public function __construct(<Is it Level Here?> $level){

      $lev = $level;

    }
  13. Sergey_D
    Offline

    Sergey_D Active Member

    Joined:
    Jan 19, 2014
    Posts:
    75
    Minecraft User:
    CLILLZ
    You give string in function unloadLevel,but will be give pocketmine\level\Level,use this
    Code:
    $lvl = Server::getInstance()->getLevelByName("LevelName");
    Server::getInstance()->unloadLevel($lvl);
    hoyinm14mc, iksaku and Falk like this.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What class is that constructor for?

Share This Page

Advertisement