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

Command don't work

Comments in 'Plugin Development' started by Player7221, Feb 27, 2016.

  1. Player7221
    Offline

    Player7221 New Member

    Joined:
    Feb 15, 2016
    Posts:
    33
    Hi!
    I have a problem, because my command "sms" don't work. My code:
    Commands class:
    Code:
    <?php
    
    namespace Sms;
    
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    
    class Commands implements CommandExecutor
    {
        public function onCommand(CommandSender $sender, Command $command, label $label, array $args)
        {
            if(strtolower($command->getName()) === "sms")
            {
                if($sender instanceof Player)
                {
                    $sender->sendMessage("To jest wiadomosc");
                    return true;            
                }
                else
                {
                    $sender->sendMessage("Musisz byc w grze");
                    return true;
                }
            }
            return false;
        }
    }
    
    If I write command "sms" to console show "Usage: /sms", but why? I added return true;

    My plugin.yml
    Code:
    name: Sms
    main: Sms\Main
    version: 1.0.0
    api: 1.13.1
    
    commands:
    sms:
      description: "This is a sms command"
      usage: "/sms"
      permission: sms.commands.sms
    
    Main class:
    Code:
    <?php
    
    namespace Sms;
    use pocketmine\plugin\PluginBase;
    
    
    class Main extends PluginBase
    {   
        public function onEnable()
        {
            $this->saveDefaultConfig();
        }
    
        public function onDisable()
        {
    
        }
    }
    
  2. amiremad
    Offline

    amiremad Active Member

    Joined:
    Sep 8, 2015
    Posts:
    192
    Minecraft User:
    amir107
    it is a message not sms lol
    Player7221 likes this.
  3. amiremad
    Offline

    amiremad Active Member

    Joined:
    Sep 8, 2015
    Posts:
    192
    Minecraft User:
    amir107
    one second i will check
    Player7221 likes this.
  4. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Add this in onEnable()
    PHP:
    $this->getCommand('sms')->setExecutor(new Commands()); // Register the command
    And this in Commands.php
    PHP:
    use pocketmine\command\CommandExecutor// Use this since your class implements CommandExecutor
    PEMapModder, applqpak and Player7221 like this.
  5. amiremad
    Offline

    amiremad Active Member

    Joined:
    Sep 8, 2015
    Posts:
    192
    Minecraft User:
    amir107
    add this code


    PHP:
    use pocketmine\Player
    Player7221 likes this.
  6. Player7221
    Offline

    Player7221 New Member

    Joined:
    Feb 15, 2016
    Posts:
    33
    If I add
    use pocketmine\command\CommandExecutor;
    to Commands.php error:
    Code:
    Fatal error: Declaration of Sms\Commands::onCommand() must be compatible with pocketmine\command\CommandExecutor::onCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $command, $label, array $args) in /home/data/XXXX/527058.pocketmp/plugins/Sms/src/Sms/Commands.php on line 11
    [11:17:50] [Server thread/EMERGENCY]: An unrecoverable error has occurred and the server has crashed. Creating a crash dump
    [11:17:50] [Server thread/EMERGENCY]: Please upload the "/home/data/XXXX/527058.pocketmp/crashdumps/CrashDump_Sat_Feb_27-11.17.50-CET_2016.log" file to the Crash Archive and submit the link to the Bug Reporting page. Give as much info as you can.
    [11:17:51] [Server thread/INFO]: 关闭中... Sms v1.0.0
    [11:17:51] [Server thread/INFO]: Unloading level "world"
    
  7. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    It should be
    PHP:
    public function onCommand(CommandSender $senderCommand $command$label, array $args)
    PEMapModder, applqpak and Player7221 like this.
  8. Player7221
    Offline

    Player7221 New Member

    Joined:
    Feb 15, 2016
    Posts:
    33
    WOW now it work!
    Vaivez66 thank you very much!!!
  9. Player7221
    Offline

    Player7221 New Member

    Joined:
    Feb 15, 2016
    Posts:
    33
    How I make $this->getConfig in the Commands.php?
    This class don't show $this->getConfig :(.
  10. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Just make
    PHP:
    $config = new Config($this->getDataFolder() . 'config.yml'Config::YAML);
    Don't forget to use
    PHP:
    use pocketmine\utils\Config;
    Player7221 likes this.
  11. Player7221
    Offline

    Player7221 New Member

    Joined:
    Feb 15, 2016
    Posts:
    33
  12. Player7221
    Offline

    Player7221 New Member

    Joined:
    Feb 15, 2016
    Posts:
    33
    Please help me.
  13. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    You should make a function that pointing to your main class. Example
    PHP:
    class Main extends PluginBase{
        
    // Do some stuff here
    }
    And in AnotherClass
    PHP:
    class AnotherClass{

        public function 
    __construct(Main $plugin){
            
    $this->plugin $plugin// $this->plugin will pointing to Main class
        
    }

        public function 
    useStuff(){
           return 
    $this->plugin->getConfig(); // Will return to method getConfig() in PluginBase
        
    }

    }
    Last edited: Feb 27, 2016

Share This Page

Advertisement