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

fixing 'friend' plugin need help with the use of 'else'

Discussion in 'Development' started by rektpixel, Jan 8, 2018.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hello,
    I have this code:
    PHP:
        public function onCommand(CommandSender $senderCommand $command$label, array $args) : bool{
            switch(
    $command->getName()){
                case 
    "friend":
                if (
    $sender instanceof Player){
                    if (isset(
    $args[0])){
                        switch (
    $args[0]){
                            case 
    "add":
                            if (isset(
    $args[1])){
                                
    $player $this->getServer()->getPlayer($args[1]);
                                 if(
    $args[1] = $sender);
                                     
    $sender->sendMessage("§cYou can't become friends with yourself.");
                                    }else{
                                        if(!
    $player == null){
                                            
    $this->addRequest($player$sender);
                                        }
                                    }                           
                                 }else{
                                     
    $sender->sendMessage("§c" $args[1] . " §cis not online");
                                 }
                             }else{
                                 
    $sender->sendMessage("§eUsage: /friend add <name>");
                             }
                        break;
                        case 
    "remove":
                            if (isset(
    $args[1])){
                                if (
    $this->removeFriend($sender$args[1])){
                                    
    $sender->sendMessage("§c" $args[1] . " §cremoved as your friend");
                                }else{
                                    
    $sender->sendMessage("§cFriend not found do /friend list to list your friends");
                                }
                            }else{
                                
    $sender->sendMessage("§eUsage: /friend remove <name>");
                            }
                        break;
                        case 
    "list":
                            
    $config = new Config($this->getDataFolder()."players/"strtolower($sender->getName()).".yml"Config::YAML);
                            
    $array $config->get("friends", []);
                            
    $sender->sendMessage(TextFormat::GOLD.TextFormat::BOLD."Friends:");
                            foreach (
    $array as $friendname){
                                
    $sender->sendMessage(TextFormat::GREEN."* ".$friendname);
                            }
                        }
                    }else{
                        
    $sender->sendMessage("§e/friend add <name> §7become friends with a player");
                        
    $sender->sendMessage("§e/friend remove <name> §7remove a player as your friend");
                        
    $sender->sendMessage("§e/friend remove <name> §7remove a player as your friend");
                        
    $sender->sendMessage("§e/accept §7accept a friend request");
                        
                    }
                }else{
            
    $sender->sendMessage("Must use command in-game");
        }
                break;
                case 
    "accept":
                    
    //echo var_dump($this->request);
                    
    if (in_array($sender->getName(), $this->request)){
                        
    //echo "added";
                        
    foreach ($this->request as $target => $requestp){
                            
    $target $this->getServer()->getPlayer($target);
                            
    $requestp $this->getServer()->getPlayer($requestp);
                            echo 
    $target->getName().$requestp->getName();
                            if (
    $requestp->getName() === $sender->getName()){
                                
    //echo "yes";
                                
    $this->addFriend($target$requestp);
                                
    $this->addFriend($requestp$target);
                            }else{
                                
    $sender->sendMessage("§cNo pending friend requests");
                            }
                        }
                    }else{
                        
    $sender->sendMessage(TextFormat::RED."§cNo pending friend requests");
                    }
            }
            return 
    true;
    I keep getting this error in the console when the plugin starts:
    Code:
    [23:08:51] [Server thread/INFO]: Loading source plugin Friends v0.3
    [23:08:51] [Server thread/CRITICAL]: ParseError: "syntax error, unexpected 'else' (T_ELSE)" (EXCEPTION) in "Friends-master/src/friends/main" at line 49
    [23:08:51] [Server thread/DEBUG]: #0 (): BaseClassLoader->loadClass(string friends\main)
    [23:08:51] [Server thread/DEBUG]: #1 (): spl_autoload_call(string friends\main)
    [23:08:51] [Server thread/DEBUG]: #2 DevTools (1).phar/src/FolderPluginLoader/FolderPluginLoader(66): class_exists(string friends\main, boolean 1)
    [23:08:51] [Server thread/DEBUG]: #3 src/pocketmine/plugin/PluginManager(161): FolderPluginLoader\FolderPluginLoader->loadPlugin(string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\plugins\Friends-master)
    [23:08:51] [Server thread/DEBUG]: #4 src/pocketmine/plugin/PluginManager(305): pocketmine\plugin\PluginManager->loadPlugin(string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\plugins\Friends-master, array Array())
    [23:08:51] [Server thread/DEBUG]: #5 DevTools (1).phar/src/DevTools/DevTools(47): pocketmine\plugin\PluginManager->loadPlugins(string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\plugins\, array Array())
    [23:08:51] [Server thread/DEBUG]: #6 src/pocketmine/plugin/PluginBase(90): DevTools\DevTools->onEnable()
    [23:08:51] [Server thread/DEBUG]: #7 src/pocketmine/plugin/PharPluginLoader(122): pocketmine\plugin\PluginBase->setEnabled(boolean 1)
    [23:08:51] [Server thread/DEBUG]: #8 src/pocketmine/plugin/PluginManager(610): pocketmine\plugin\PharPluginLoader->enablePlugin(DevTools\DevTools object)
    [23:08:51] [Server thread/DEBUG]: #9 src/pocketmine/Server(1955): pocketmine\plugin\PluginManager->enablePlugin(DevTools\DevTools object)
    [23:08:51] [Server thread/DEBUG]: #10 src/pocketmine/Server(1941): pocketmine\Server->enablePlugin(DevTools\DevTools object)
    [23:08:51] [Server thread/DEBUG]: #11 src/pocketmine/Server(1655): pocketmine\Server->enablePlugins(integer 0)
    [23:08:51] [Server thread/DEBUG]: #12 src/pocketmine/PocketMine(554): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\, string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\plugins\)
    [23:08:51] [Server thread/DEBUG]: #13 C:/Users/Admin/Creative Cloud Files/Desktop/Lobby - DEV/PocketMine-MP.phar(1): require(string phar://C:/Users/Admin/Creative Cloud Files/Desktop/Lobby - DEV/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
    [23:08:51] [Server thread/CRITICAL]: Could not load plugin 'Friends'
    
    this is line 47 - 50:
    PHP:
                                        }
                                    }                           
                                 }else{
                                     
    $sender->sendMessage("§c" $args[1] . " §cis not online");
    I don't understand whats wrong with '}else{' here. could someone pls help me with the formatting of this :/
     
  2. MioTaku

    MioTaku Witch

    Messages:
    69
    GitHub:
    uselesswaifu
    There's an else right after an else which doesn't make a lot of sense.. maybe try removing the } right before the else
     
    rektpixel likes this.
  3. Flavius12

    Flavius12 Spider Jockey

    Messages:
    32
    GitHub:
    flavius12
    You put a wrong semicolon on the if block on line 10
    You wrote
    PHP:
    if($args[1] = $sender);
    instead of
    PHP:
    if($args[1] = $sender){
    (you should also replace = with ==)
     
    rektpixel likes this.
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Sender is the object. You want to compare the name case-insensitively.
     
    rektpixel and Flavius12 like this.
  5. Flavius12

    Flavius12 Spider Jockey

    Messages:
    32
    GitHub:
    flavius12
    Yes, I didn't notice it. This should be the correct code:
    PHP:
    if($args[1] == $sender->getName()){
    Or better:
    PHP:
    if(strcasecmp($args[1], $sender->getName()) == 0){
    (^ You will do the case-insensitive string comparison by using strcasecmp function)
     
    HyGlobalHD and rektpixel like 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.