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

Commands

Discussion in 'Development' started by XdmingXD, May 5, 2017.

  1. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    Why some people use onCommand() function in the main class
    and some people register commands?
    Whats the different between them?
     
  2. Lowkey

    Lowkey Slime

    Messages:
    94
    with the onCommand func you can control what the command does, for example using an if statement.
     
  3. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    i know... but why register commands?
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Actually, you could say there's three types of command handling. One where you use the onCommand function is in your main class, with a plugin.yml. One with a CommandExecutor class, registering it and adding it in a plugin.yml, and lastly a method to extend Command and implement PluginIdentifiableCommand and stuff. Then registering it in the main file.

    Most people will agree that the last option is the most viable for bigger plugins. That is because it is easier to create a superclass and make the subclasses extend it, so you can prevent duplicate code. Furthermore, a lot of people (like me) simply dislike adding the commands in the plugin.yml file. There is not a big difference between the first and second method. It's essentially the same but in a different class.

    You register a command because a class won't just magically be executed without initiating it.
     
    Last edited: May 5, 2017
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    They aren't three "types of commands". They're all the same type of command. They are three ways of handling and declaring commands.
     
    HimbeersaftLP and jasonwynn10 like this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You can also prevent reduce duplicate code by simply declaring functions. Nonetheless, it allows you to have non-hardcoded description and usage more easily.
     
    jasonwynn10 likes this.
  7. Lowkey

    Lowkey Slime

    Messages:
    94
    To register commands via your plugin code, you can do
    PHP:
    public function onEnable(){

    $this->getServer()->getCommandMap()->register("mycommand", new MyCommand($this));
    }
    //"mycommand" should be replaced as your command label/name
    //You should also create a class for the command handling what is does, in this case the class is named "MyCommand".
    You can also register commands normally in the plugin.yml. However, if your plugin contains a numerous amount of commands, this is not recommended. EssentialsPE being a plugin which has an immense command count handles them this way.
     
    Sandertv likes this.
  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.