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

Server::getPlayer($args[0]) returns null

Discussion in 'Development' started by InspectorGadget, Nov 30, 2016.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi! I've been trying to fix my code to give my players freeze and Confuse but i couldn't make it happen. It seems not working. My ARGS issue has been solved but not this. It would be a great help if anyone could help me.
    Thanks

    My error: Freeze has the same Error
    [Server thread/CRITICAL]: Unhandled exception executing command 'conf InspectorGadget' in conf: Argument 1 passed to freezer\main::confuse() must be an instance of pocketmine\Player, null given, called in /home/gs/gs/data/servers/4016401/plugins/Freezer/src/freezer/main.php on line 70 2016-11-30
    [Server thread/CRITICAL]: TypeError: "Argument 1 passed to freezer\main::confuse() must be an instance of pocketmine\Player, null given, called in /home/gs/gs/data/servers/4016401/plugins/Freezer/src/freezer/main.php on line 70" (EXCEPTION) in "/Freezer/src/freezer/main" at line 52

    My full main.php
    PHP:

    <?php

    namespace freezer;

    use 
    pocketmine\Player;
    use 
    pocketmine\event\Cancellable;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\PluginCommand;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\ConsoleCommandSender;

    class 
    main extends PluginBase implements Listener {

        public 
    $manager;

        public function 
    onEnable() {
            if(!
    is_dir($this->getDataFolder())) {
                @
    mkdir($this->getDataFolder());
            }
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getConfig()->getAll();
            if(
    $this->getConfig()->get("Enable") == true) {
                
    $this->setEnabled(true);
                return;
            
    $this->saveDefaultConfig();
            
    $server Server::getInstance();
            
    $this->manager = new manager($this$activeForWorlds);
            }
        }

        public function 
    isFrozen(Player $player){
            return 
    in_array($player->getName(), $this->frozen);
        }

        public function 
    freeze(Player $player){
            
    $this->frozen[$player->getName()] = $player->getName();
        }

        public function 
    unfreeze(Player $player){
            unset(
    $this->frozen[$player->getName()]);
        }

        public function 
    confuse(Player $player){
            
    $this->confuse[$player->getName()] = $player->getName();
        }

        public function 
    unConfuse(Player $player){
            unset(
    $this->confuse[$player->getName()]);
        }

        public function 
    isConfused(Player $player){
            return 
    in_array($player->getName(), $this->confuse);
        }
            
    // if($this->getPlugin()->isFrozen($target) !== true){
        
    public function onCommand(CommandSender $senderCommand $command$label, array $args){
            
    //if($sender instanceof Player){
                
    switch($command){
                    case 
    "conf": {
                        if (isset(
    $args[0])) {
                            
    $target $this->getServer()->getPlayer($args[0]);
                            
    $this->confuse($target);
                            
    $sender->sendMessage("$target has been confused!");
                            
    $target->sendMessage("You've been Confused!");
                            return 
    true;
                        }
                    }
                    break;
                    case 
    "unfr": {
                        if(isset(
    $args[0])) {
                            
    $player $this->getServer()->getPlayer($args[0]);
                                
    $sender->sendMessage("$player has been unfrozen");
                            return 
    true;
                        }
                        else {
                            
    $sender->sendMessage(TextFormat::RED "[Freezer] Missing arguments");
                        }
                    }
                    break;
                    case 
    "fr": {
                        if(isset(
    $args[0])){
                                
    $player $args[0];
                                
    $sender->sendMessage("Freezing > $player");
                                
    $file = new Config($this->getDataFolder() . "players.txt"Config::ENUM);
                                
    $file->set($player$player);
                                
    $file->save();


                                
    $target $this->getServer()->getPlayer($args[0]);
                                    if(
    $target !== null){
                                            
    $this->freeze($target);
                                            
    $sender->sendMessage($target->getName() . " §ahas been frozen!");
                                            
    $target->sendMessage("§eYou have been §bfrozen§e!");
                                            return 
    true;
                                    }



                                
    $sender->sendMessage("You have perm froze > $player");
                                return 
    true;
                        }
                        else{
                            
    $sender->sendMessage(TextFormat::RED "[Freezer] Missing arguments");
                        }
                    break;
                    }
                }
            
    //}
            //else {
            //    $sender->sendMessage("In game!");
            //}
        
    }
      
        public function 
    onDisable() {
            
    $this->freezer->saveAll();
            
    $this->getConfig()->saveAll();
        }
    }
     
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Any help will be very much appreciated.
     
  3. archie426

    archie426 Baby Zombie

    Messages:
    130
    GitHub:
    archie426
    Please can you change the title to something more helpful?
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Please write a descriptive title. I have edited the thread title for you already, and you are receiving one warning point for it.

    Please attempt to read the error message and fix it yourself before asking.

    Please also read the posting guidelines:
    Your error is saying:
    So you should point out which line line 70 is to save trouble for others.
    I have done this for you, and it is this line:
    PHP:
    $this->confuse($target);
    Your confuse() function accepts a pocketmine\Player instance. According to line 69:
    PHP:
    $target $this->getServer()->getPlayer($args[0]);
    Your $target should be a return value of Server::getPlayer(), which returns a pocketmine\Player instance or null. From your error message, it appears that it is null.
    Server::getPlayer() returns null when the player is not found. It can hence be deduced that $args[0] does not refer to a valid online player.
    You have to fail the command gracefully when $args[0] does not refer to a valid online player. This can be done by sending a message and then exiting the command executor gracefully when the return value ($target) is found to be null:
    PHP:
    if ($target === null) {
        
    $sender->sendMessage("Player not found!");
        return 
    true;
    }
     
    archie426 and HimbeersaftLP like this.
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    That doesnt return error but it doesnt confuse the player when online...
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Of course. Your confuse() function only adds the player to the array. You aren't executing anything that checks if the player is confused. It is your own problem of implementation of confuse, and it has nothing to do with this thread. The question in this thread is already resolved. If you have any other questions, they won't be related to this question, so please start a new thread.
     
    HimbeersaftLP 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.