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

Prevent on null crash on array

Comments in 'Plugin Development' started by MyNameIsTriXz, Dec 31, 2015.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Is there a way to prevent that the server crashes when there are 0 variables in an array? So I could prevent the crash on count($this->getServer()->getLevelByName("...")->getPlayers()), and turn the result instead of on null to just 0 (as amount)? :/
  2. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    if()
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Server should not crash if you are using it correctly. How are you using it?
    EvolSoft and CraftYourBukkit like this.
  4. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Assuming you're trying to get all players in a specified world:
    getLevelByName() function can return null or an instance of PocketMine Level class. So you can simply check if the function returned a Level instance. See the code below:
    PHP:
    $lvl $this->getServer()->getLevelByName("...");
    if(
    $lvl instanceof Level){
        
    $cnt count($lvl->getPlayers());
    }
    MyNameIsTriXz likes this.

Share This Page

Advertisement