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

Run command in console

Discussion in 'Development' started by Amon28, May 11, 2017.

Thread Status:
Not open for further replies.
  1. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    Help with plug-in I'm making a plug in that runs a command in the console what I did
    $command = "lagg clear";
    $this->getServer()->dispatchCommand (new ConsoleCommandSender(), $command);

    It says this Call to a member function dispatchCommand() on null
     
  2. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Can you post the entire code on github instead to give us context? The error is telling you that $this->getServer() is null, which means that whatever '$this' happens to be, it doesn't extend a class that has the getServer() method.
     
    HimbeersaftLP and corytortoise like this.
  3. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  5. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    I remove the __construct() from the plug in i tested it and it said this
    Error: "Call to a member function dispatchCommand() on null"
     
  6. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    How can i run a command in console using a plug in
     
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Try this edited version of your plugin:
    PHP:
    <?php
    namespace GolemCrate\Dew\Commands;

    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;

    class 
    crate extends PluginBase // no need to implement CommandExecutor because PluginBase already does
    // removed constructor function
            
    public function onCommand(CommandSender $senderCommand $command$label, array $args){
                
    $cmd strtolower($command->getName());
                switch(
    $cmd){
                    case 
    "gcrate":
                        if(
    $sender instanceof Player){
                            
    $inv $sender->getInventory();

                            
    $it Item::get(131,0,1);
                            
    $it->setCustomName("§bGkey §7don't put in chest or throw");

                            if(
    $inv->contains($it)){
                                
    $level $sender->getLevel();
                                
    $player $sender->getName();

                                
    $inv->removeItem($it);

                                
    $result rand(1,3);
                                switch(
    $result){
                                    case 
    1:
                                        
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "lagg clear"); // moved string from variable to function
                                        
    $sender->sendMessage("§b------------");
                                        
    $sender->sendMessage("§bGcrate > §aYou won 500 Money!");
                                        
    $sender->sendMessage("§b------------");
                                    break;
                                    case 
    2:
                                        
    $i Item::get(19,0,1);
                                        
    $i->setCustomName("500 Money");

                                        
    $e Item::get(19,0,1);
                                        
    $e->setCustomName("500 Money.");

                                        
    $en Enchantment::getEnchantment(1);
                                        
    $en->setLevel(4);

                                        
    $i->addEnchantment($en);
                                        
    $e->addEnchantment($en);

                                        
    $inv->addItem($i);
                                        
    $inv->addItem($e);

                                        
    $sender->sendMessage("§b------------");
                                        
    $sender->sendMessage("§bGcrate > §aYou won 1000 Money!");
                                        
    $sender->sendMessage("§b------------");
                                   break;
                                   case 
    3:
                                       
    $i Item::get(276,0,1);
                                       
    $i->setCustomName("§bDiam Sword");

                                       
    $inv->addItem($i);
                                       
    $sender->sendMessage("§bGcrate > §aYou won Diamond Sword");
                                   break;
                                }
                            }else{
                                
    $sender->sendMessage("§e[Gcrate] §fyou need a Gkey!");
                            }
                        }else{
                            
    $sender->sendMessage("§cRun this command on Game!");
                        }
                    }
                }
        }
     
    Last edited: May 11, 2017
  8. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    Are there any errors show in console while loading your plugin?
     
  9. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
    There's no errors saying in the console while loading the plug in.
    I tested it again and it said the same error
     
  10. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Show more information, like this:
    • Paste your code into pastebin and give the link to it.
    • Show the error, either an image or text.
    • What did you expect to happen?
    • What really happened?
     
  11. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    PHP:
    <?php

    namespace pmmp\Test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;
    class 
    Main extends PluginBase {

        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
            switch(
    $command->getName()){
                case 
    "gcrate":
                    
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "say hi");
                    return 
    true;
            }
        }
    }
    This code just works. Can't make it simpler then this.
     
  12. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    They have provided all of the above information.

    @Amon28 Could you paste a copy of your plugin.yml? I have another idea.
     
    SOFe likes this.
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    If you are calling the constructor in the class extending PluginBase, then..
    PHP:
    public function __construct(){
        
    parent::__construct();
        
    //your code...
    }
    But the constructor in your plugin's class extending PluginBase is useless.
     
    jasonwynn10 likes this.
  14. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
     
  15. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    The code I previously provided should work with the changes I made.
     
  16. Amon28

    Amon28 Slime

    Messages:
    76
    GitHub:
    amon28
  17. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  18. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    It references a class "GolemCrate\Dew\Main" as your main class, that class should extend PluginBase. None of your other classes (ie "GolemCrate\Dew\Commands\crate") should extend PluginBase as they are not plugins, just classes in your plugin.
     
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I think he's not showing full code.
     
  20. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    I assumed so, I think they are having PM load the main class and then trying to call new crate($this) or something and assuming crate will just act as a plugin with a working getServer method. If that is not the case, I have no idea what is going wrong.
     
Thread Status:
Not open for further replies.
  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.