Tutorial Plugin Command v2

Discussion in 'Resources' started by Provsnoobgaming, Mar 25, 2020.

  1. Provsnoobgaming

    Provsnoobgaming Baby Zombie

    Messages:
    130
    GitHub:
    provsalt
    This guide will explain how to split the Command from PluginBase, the reason behind this is to make the plugin cleaner and more readable.I am sick of people making cores with switch ($command->getName())

    Note: If you literally have no idea about how PHP OOP works, don't follow this guide and keep developing like trash because you're not going to understand the methods explained in this post.

    Firstly first you would want to create your pluginbase class
    PHP:
    <?php
    namespace author\pluginname;

    use 
    pocketmine\plugin\PluginBase;

    class 
    MainClass extends PluginBase{
      
    }
    then I will make the $map variable and register the command class
    PHP:
    $map $this->getServer()->getCommandMap();
    $map->register($this->getName(), new CommandClass("test"$this));
    Call this onEnable
    don't forget to import the command class later.
    if you want to register more then 1 command you can use the registerAll function.

    So I am going to create my command folder in the current directory and make a file called CommandClass

    Now you would want to extend Command and make a constructor with something like this

    PHP:
    class Commandclass extends Command implements PluginIdentifiableCommand{

        
    /**
         * @var Plugin
         */
        
    private $plugin;

        public function 
    __construct(string $namePlugin $pluginstring $description "", ?string $usageMessage null, array $aliases = []){
            
    parent::__construct($name$description$usageMessage$aliases);
            
    $this->plugin $plugin;
            
    $this->setDescription("Tests");
        }
    }
    The constructer is telling the server that the command name is $name which is what you are passing in when you call it

    Lets set a description called Tests

    Now you would want to inherit docs
    PHP:
    public function execute(CommandSender $senderstring $commandLabel, array $args){
         
    $sender->sendMessage("Tada");
    }
    What I am doing here is to send a message called tada when you run the command

    PHP:
    public function getPlugin(): Plugin{
            return 
    $this->plugin;
    }
    now if you ever want to access something from your main class you can use the getPlugin function we have created
    Sorry if my explanation was very crappy since most of the time I let phpstorm inherit docs for me so feel free to ask questions relevant to the post.
     
    Last edited: May 6, 2020
  2. anirudh246

    anirudh246 Silverfish

    Messages:
    17
    GitHub:
    anirudh246
    Thank you for this guide! It definitely does look cleaner and more professional than the normal
    switch($cmd->getName();

    For the ones who have a little more trouble understanding, do you mind if I make a repository on GitHub to show a better example of this method of creating commands?
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,979
    GitHub:
    sof3
    Making stuff more complmicated does not exactly mean more professional.
     
  4. mmm545

    mmm545 Witch

    Messages:
    70
    GitHub:
    mmm545
    how to use registerAll ?
    Edit: nvm i figured it out
     
    Last edited: May 24, 2020 at 1:14 AM

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.