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

onCommand error declaration

Discussion in 'Plugin Help' started by gsdgamer, Jan 27, 2019.

  1. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Hi everyone. I keep getting an error when I try creating a command.

    Code:
    Fatal error: Declaration of Plug\Main\controller::onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $cmd, string $Commandlabel, array $args) must be compatible with pocketmine\plugin\PluginBase::onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $command, string $label, array $args): bool in C:\Users\Home\Desktop\mc\plugins\src\Plug\Main\controller.php on line 29
    My plugin.yml code is this:
    Code:
    name: controller
    main: Plug\Main\controller
    version: 1.0.0
    api: [3.5.7]
    commands:
        tele:
            description: "description"
            usage: "/tele"
            permission: test.my
    
    My main file is this:
    Code:
    <?php
    
    namespace Plug\Main;
    
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    class controller extends PluginBase{
        public function onEnable(){
            foreach(Server::getInstance()->getOnlinePlayers() as $player){
                $this->playerList[$player->getName()] = $this->getPlayerData($player);
            }
            $this->getLogger()->info("AIO has been enabled");
        }
       
        public function onDisable(){
            $this->getLogger()->info("AIO has been disabled");
        }
       
        public function onCommand(CommandSender $sender, Command $cmd, string $Commandlabel, array $args){
            if(strtolower($cmd->getName()) === "tele"){
               
                return TRUE;
            }
            return FALSE;
        }
    }
    
    What is annoying me is that when I first created this code it worked. I went back to it 3 minutes after and got this error.
     
  2. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    Code:
    Fatal error: Declaration of Plug\Main\controller::onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $cmd, string $Commandlabel, array $args) must be compatible with pocketmine\plugin\PluginBase::onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $command, string $label, array $args): bool in C:\Users\Home\Desktop\mc\plugins\src\Plug\Main\controller.php on line 29
    Do you see the difference between your function signature and the interface's signature?

    Your declaration:
    Code:
    onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $cmd, string $Commandlabel, array $args)
    The interface:
    Code:
    onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $command, string $label, array $args): bool
    Aside from the variable names (they don't matter as long as the types are the same,) the only difference is in the return value type. While you had the right idea returning either true or false in the onCommand function, returning it is not enough. You also have to add the type to your method signature to fulfill the interface.

    When you run into these kinds of problems, it's helpful to look closely at the error and pick apart exactly what it's telling you ;) It's definitely an acquired skill, but with practice and attention, you will get it.
     
    Last edited: Jan 28, 2019
    gsdgamer likes this.
  3. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Thanks for the help. I really should take more notice.
     
    Last edited: Jan 27, 2019
  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.