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

Code error!?

Discussion in 'Help' started by rektpixel, Apr 1, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hello I have this plugin but it's got an error. here is the error:
    Unhandled exception executing command 'pwb MBLobby ke' in pwb: Call to a member function

    [PocketMine] [21:16:01] > Unhandled exception executing command 'pwb MBLobby ke' in pwb: Call to a member function getOnlinePlayers() on null
    [PocketMine] [21:16:01] > Error: "Call to a member function getOnlinePlayers() on null" (EXCEPTION) in "/phar_PerWorldBroadcast-master_WOZU_qlz9U8gXBR.phar/src/pwb/main" at line 18

    Here is the code:

    PHP:
    <?php
    namespace pwb;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    class 
    main extends PluginBase {
        public function 
    onEnable(){
            
    $this->getLogger()->info("loaded!");
        }
        
        public function 
    onCommand(CommandSender $sender,Command $command$label,array $args){
            switch (
    $command->getName()){
                case 
    "pwb":
                    if (
    $sender->hasPermission("pwb.use")){
                    if (isset(
    $args[0]) and isset($args[1])){
                        
    $players $this->getServer()->getLevelByName($args[0])->getPlayers();
                        unset(
    $args[0]);
                        
    $message implode(" "$args);
                        if (!
    $players == null){
                            foreach (
    $players as $player){
                                
    $player->sendMessage($message);
                            }
                        }else{
                            
    $sender->sendMessage("no such world");
                        }
                        
                    }else{
                        
    $sender->sendMessage("Usage: /pwb [world] [message]");
                    }}else{
                        
    $sender->sendMessage("No permission to use this command!");
                    }
                break;
                
            }
        }
        
    }

    The line " $players = $this->getServer()->getLevelByName($args[0])->getPlayers(); " is line 18 btw that's where the error is :/ Could someone help?! I have also attached the zip file

    Just something completely different I wanna add, wtf happened to the forums? It's a bit messed up in my opinion.
     

    Attached Files:

  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    There is no -BUTTONS YOU ARE HURTING MY EYES!- getPlayers function in Level. You might want to write your own
    PHP:
    /* $this must have a getServer function which returns \pocketmine\Server (classes extending pluginBase do */
    function aprilFoolsSuck(\fools\pocketmine\level\Level $level): array{
    $players = [];
    foreach(
    $this->getServer()->getPlayers() as $player){
    if(
    $player->getLevel() == $level){
    $players[] = $player;
    }
    return 
    $players;
    }
    }
    Sorry for no formatting and no phpdoc.
    April fools btw.
     
  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.