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

Solved getLevelByName returning null

Discussion in 'Development' started by UnknownOre, Feb 20, 2019.

  1. UnknownOre

    UnknownOre Silverfish

    Messages:
    21
    GitHub:
    UnknownOre
    what's wrong with my code?
    the level with the name Gorzen located on
    PockeMine/worlds/world and Gorzen is the only world i have
    PHP:
    public function onCommand(CommandSender $senderCommand $commandstring $label, array $args): bool {
            if(
    strtolower($command->getName()) == "world"){

            
    $world $sender->getPlayer()->getLevel()->getName();//Gorzen
            
    var_dump($world);//Gorzen
            
    var_dump($this->getServer()->getLevelByName($world));//NULL
            
    var_dump($this->getServer()->getLevelByName('world'));//instanceof pocketmine\level\Level
            
    return true;
        }
      }
    i made this post because my issue on github got closed https://github.com/pmmp/PocketMine-MP/issues/2778
     
    Last edited: Feb 20, 2019
  2. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    Try this:
    PHP:
    $world $sender->getLevel()->getFolderName();
    Let me emphasize that levels are identified by their folder name and not their "display" name.
    This is also why Server->getLevelByName("world") returns the desired Level object in your case. It's the correct name.
     
  3. UnknownOre

    UnknownOre Silverfish

    Messages:
    21
    GitHub:
    UnknownOre
    then shouldn't it be getLevelByFolderName();
    PHP:
    $world $sender->getLevel()->getFolderName();
    var_dump($this->getServer()->getLevelByName($world);
    returned
    Code:
    string(6) "Gorzen"
    object(pocketmine\level\Level)#10763 (0) {
    }
    
     
  4. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    You're absolutely right to point out this discrepancy, however, I personally would prefer to keep the shorter method name.
    But in the upcoming backwards-incompatible version 4.0 your suggestion is (kind of) addressed in the following commit:
    https://github.com/pmmp/PocketMine-MP/commit/e341f3dce233c60a7e5715bc0ec6f857675d4c73
     
    UnknownOre likes this.
  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.