Solved Multiple command file in one plugin

Discussion in 'Plugin Help' started by PratyayPrasun, Aug 5, 2021.

  1. PratyayPrasun

    PratyayPrasun Creeper

    Messages:
    4
    GitHub:
    PratyayGaming
    I have just started learning a little bit of pocketmine coding with the help of internet and I came across a issue that I don't know how to make different file for different commmand like I am creating a core so it have a few commmand of different categories and I don't want to make my main file a mess. I have seen the bedwars plugin but can't able to understand because there commmand are little bit confusing that make overall file confusing for me. Thanks in advance
     
  2. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    127
    It goes something like this, you just need a class extending Command.php and to register use the Server.php function getCommandMap()->registerCommand($prefix, $class);
    PHP:
    class MyCommand extends Command{

    ///Implement __construct to define Command name, aliases....

    public function execute(CommandSender $senderstring $label, array $args) : bool{

    }
    }
    class 
    Main extends PluginBase{

    public function 
    onEnable(){
    $this->getServer()->getCommandMap()->registerCommand("test", new MyCommand("test"));
    }
    }
     
  3. PratyayPrasun

    PratyayPrasun Creeper

    Messages:
    4
    GitHub:
    PratyayGaming
    Thanks for it do I have to do anything on main file or anywhere else
     
  4. PratyayPrasun

    PratyayPrasun Creeper

    Messages:
    4
    GitHub:
    PratyayGaming
    And can I make multiple file with this format
     
  5. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    127
    yes you have to register with commands in the main file
     
  6. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    127
    yes you have to register with commands in the main file Yes, as many Command files as you want
     
  7. PratyayPrasun

    PratyayPrasun Creeper

    Messages:
    4
    GitHub:
    PratyayGaming
    Thanks alot you help me alot I figured it out
     
  8. Pinesolz

    Pinesolz Spider

    Messages:
    8
    GitHub:
    atlzz
    You can create commands within multiple files. I do it with construct.
    ex.
    PHP:
    public function __construct(){
        
    parent::__construct("command_name""description""usage", ['aliasses']);
        
    $this->setPermission("permission.name"); # (If permission is needed)
    }

    /**
     * @param CommandSender $sender
     * @param string $commandLabel
     * @param array $args
     */
    public function execute(CommandSender $senderstring $commandLabel, array $args){
        
    # Your code here || (What will the command execute?)
        #ex.
        
    $sender->sendMessage(TextFormat::GREEN "Sup man ");
    }
    Then we need to register our commands via command map in the loader/main class.
    PHP:
    public function onEnable(): void {
         
    $this->getServer()->getPluginManager()->registerEvents($this$this);
                
    $this->getServer()->getCommandMap()->registerAll("CoreCommands", [
                    new 
    MyNameOfCommandFileOne(),
                    new 
    MyNameOfCommandFileTwo(),
                    new 
    MyNameOfCommandFIleThree() # No comma on the last one || (This will error)
                    # Not the literal command name^, the file name works for me.
                
    ]);
    Then your command should work.
     
    Last edited: Sep 14, 2021

Share This Page

  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.