Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Tutorial How use commands in plugin

Comments in 'Resources' started by Kvetinac97, Sep 27, 2015.

  1. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    I watched PocketMine Plugin Development and I saw that many users didn't know how use function onCommand() and commands at all:
    PHP:
    <?php

    namespace Tutorials;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;

    class 
    CommandTutorial extends PluginBase implements Listener{

    public function 
    onEnable(){
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
    }

    public function 
    onCommand(CommandSender $sdCommand $cmd$label, array $args){
    $sd is Player/Console who has performed the command. You can add
    PHP:
    if ($sd instanceof Player){
    (or instanceof ConsoleCommandSender)
    $cmd is performed command, not name of the command. You can get the name(/command a b c) with
    PHP:
    $name $cmd->getName();
    I won't analyze $label, it'll be good if somebody write something about it.
    $args are array of arguments used:
    PHP:
    $args = array(
    => "hello",
    => "I'm",
    => "kvetinac97");
    Those are arguments for command /xxx hello I'm kvetinac97

    You can get only one argument by "array key" ($args[0] = "hello"...).
    Many players wanted to know how get all arguments to "hello I'm kvetinac97" string. For this, use function
    PHP:
    implode(" ",$args);
    If you want know if player used 1/2/3... arguments, use
    PHP:
     if ( isset($args[0/1/2...])) { 
    Easiest way how register commands is by writing them into plugin.yml:
    Code:
    api: [1.12.0,1.13.0,2.0.0]
    commands:
    testcommand:
      usage: "/testcommand <test>"
      description: "Testing command!"
      permission: "myplugin.testcommand
    
    You can also perfrom command as Console/Player:
    PHP:
    $this->getServer()->dispatchCommand(new ConsoleCommandSender,"say I dispatched the command!");
    These are all basic informations about commands and their using.

    Thanks,
    Kvetinac97
    Last edited: Sep 28, 2015
    madhon, AndrewBit and TheKofola12 like this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is used. It means the alias used by the user.
    applqpak, Creeperface and TheKofola12 like this.

Share This Page

Advertisement