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

Plugin crashes server when using a command.

Comments in 'Plugin Development' started by RekkuzaRage, Jul 17, 2014.

  1. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    252
    Minecraft User:
    RekkuzaRage
    Hi,

    I have this problem, I made a very simple kit plugin for my server, but when I run command tools it automatically crashes my test server, resulting in a error log.
    Code:
    07:29:31 [NOTICE] A E_RECOVERABLE_ERROR error happened: "Argument 1 passed to pocketmine\Server::dispatchCommand() must implement interface pocketmine\command\CommandSender, string given, called in C:\Boot\PocketMine-MP\plugins\PocketCore\src\PocketCore\PocketCore.php on line 21 and defined" in "phar://C:/Boot/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/Server.php" at line 1629
    07:29:31 [ERROR] A E_WARNING error happened: "Missing argument 2 for pocketmine\Server::dispatchCommand(), called in C:\Boot\PocketMine-MP\plugins\PocketCore\src\PocketCore\PocketCore.php on line 21 and defined" in "phar://C:/Boot/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/Server.php" at line 1629
    07:29:31 [NOTICE] A E_NOTICE error happened: "Undefined variable: commandLine" in "phar://C:/Boot/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/Server.php" at line 1630
    07:29:31 [NOTICE] A E_RECOVERABLE_ERROR error happened: "Argument 1 passed to pocketmine\command\SimpleCommandMap::dispatch() must implement interface pocketmine\command\CommandSender, string given, called in phar://C:/Boot/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/Server.php on line 1630 and defined" in "phar://C:/Boot/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/command/SimpleCommandMap.php" at line 166
    
    Fatal error: Call to a member function sendMessage() on a non-object in phar://C:/Boot/PocketMine-MP/PocketMine-MP.phar/src/pocketmine/Server.php on line 1637
    
    I am not really sure what the problem is. Below is the source code of the simple plugin.
    PHP:
    <?php

    namespace PocketCore;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;

    class 
    PocketCore extends PluginBase{

        public function 
    onEnable(){
            
    $this->getLogger()->info("PocketCore has loaded!");

        }

        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
            switch(
    $command->getName()){
                case 
    "tools":
                    if (
    $sender instanceof Player) {
                    
    $this->getServer()->dispatchCommand("give " $sender->getName() . " 364 64");
                    }
                    else {
                        
    $sender->sendMessage("Please run command in game.");
                        return 
    true;
                    }
                    break;
                case 
    "food":
                    if (
    $sender instanceof Player) {
                  
                    }
                    else {
                        
    $sender->sendMessage("Please run command in game.");
                        return 
    true;
                    }
                    break;
                default:
                    return 
    false;
            }
        }

        public function 
    onDisable(){
            
    $this->getLogger()->info("PocketCore has been disabled.");
        }

    }
    64FF00 likes this.
  2. MinecrafterJPN
    Offline

    MinecrafterJPN Active Member Plugin Developer

    Joined:
    Aug 26, 2013
    Posts:
    154
    Plugins:
    9
    Minecraft User:
    MinecrafterJPN
    See dispatchCommand()'s document!

    /**
    * Executes a command from a CommandSender
    *
    * @param CommandSender $sender
    * @param string $commandLine
    *
    * @return bool
    */

    For example,
    Code:
    $this->getServer()->dispatchCommand($sender, "give " . $sender->getName() . " 364 64");
    
    64FF00 likes this.
  3. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    you need
    use pocketmine\Server::dispatchCommand();
    use pocketmine\command\SimpleCommandMap::dispatch();

    please look at the error before submitting an error
  4. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    252
    Minecraft User:
    RekkuzaRage
    Thanks a lot, it worked perfectly!
    It isn't required.
    64FF00 likes this.
  5. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    by the way , i think you should use addItem() instead of dispatchCommand()
  6. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    252
    Minecraft User:
    RekkuzaRage
    Hmm...that is better.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That is definitely not the way.
    Falk and 64FF00 like this.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    new ConsoleCommandSender is better (if I hadn't missed any constructor parameters)
    64FF00 likes this.

Share This Page

Advertisement