Tutorial Plugin Command v2

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

  1. Provsnoobgaming

    Provsnoobgaming Baby Zombie

    Messages:
    134
    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
    Primus, mmm545, HimbeersaftLP and 4 others like this.
  2. anirudh246

    anirudh246 Spider Jockey

    Messages:
    31
    GitHub:
    ImDaRealAni
    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,968
    GitHub:
    sof3
    Making stuff more complmicated does not exactly mean more professional.
     
    LyricalLyfes, Primus, Levi and 3 others like this.
  4. NZSigourney

    NZSigourney Spider

    Messages:
    12
    GitHub:
    BlackPMFury
    If I want call A Function at Main like "Public function onForm($player)"
    I will write "$this->plugin->onForm()"?
     
  5. minijaham

    minijaham Zombie Pigman

    Messages:
    693
    GitHub:
    minijaham
    Yes.
     
  6. NZSigourney

    NZSigourney Spider

    Messages:
    12
    GitHub:
    BlackPMFury
    Okay, Thanks bro
     
  7. Primus

    Primus Zombie Pigman

    Messages:
    693
    You lack the knowledge to act as an expert on a given topic. You by no means are restricted from writing such guides, but next time without concrete evidence, don't bring up your humble opinions.
     
  8. minijaham

    minijaham Zombie Pigman

    Messages:
    693
    GitHub:
    minijaham
    Is that...to me? :(
     
  9. Primus

    Primus Zombie Pigman

    Messages:
    693
    Not at all buddy. I was targetting the thread author. He claimed that one approach is better than another, while both work perfectly fine under required circumstances.

    Some plugins do not require such encapsulation, there is a blurry line between good and premature optimization.

    Take as an example /date plugin that informs the sender about current date (server). Do you really require another class instead of 8 lines of extra code?
     
    minijaham likes this.
  10. minijaham

    minijaham Zombie Pigman

    Messages:
    693
    GitHub:
    minijaham
    Ah..I see :D

    I thought you were talking to me because you liked the post...
     

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.