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

how to __construct?

Discussion in 'Facepalm' started by kazuya, Jun 5, 2017.

  1. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    I'm new to this and don't know how it works...

    I want my commands.php to link with main.php but I don't know how.
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I suggest learning what a constructor does and when it should be used before worrying about it. You can read a bit about it from php.net.

    I assume your commands.php and main.php are supposed to be instances of Command and PluginBase, respectively? Show what you've tried so far.
     
    jasonwynn10, Muqsit and kazuya like this.
  3. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    PHP:
    Main.php

    <?php

    namespace Kaz;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\block\Block;
    use  
    pocketmine\tile\Tile;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    Kaz\Tasks\MyTask;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\level\format\Chunk;
    use 
    pocketmine\level\Level;
    use 
    Kaz\Commands;

    class 
    Main extends PluginBase implements Listener
    {
        private 
    $commands;

        public function 
    onEnable()
        {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("§aKaz was enabled");
        }

        public function 
    onDisable()
        {
            
    $this->getLogger()->info("§cKaz was disabled");
        }
    }
    Commands.php

    PHP:
    <?php

    namespace Kaz;

    use 
    Kaz\Main;
    use 
    pocketmine\event\Listener;

    class 
    Commands implements Listener
    {
        private 
    $plugin;

        public function 
    __construct(Main $plugin)
        {
            
    $this->plugin $plugin;
        }
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args)
        {
            switch (
    $command->getName()) {

                case 
    "kaz":

                    
    $sender->sendMessage("§cCommands:\n§6-§7 /gmc\n§6-§7 /gms\n§6-§7 /awesome");
                    return 
    true;
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I suggest looking at this tutorial over Plugin Commands. There are several ways to use commands in plugins, but you're a little off the mark. You don't need a Listener interface for commands. You can extend PluginCommand or Command in a second class, or, as shown in the thread above, you can handle commands with your main class. I strongly suggest using that method, as it is much simpler.

    Other than that, you don't typically need to have 2 classes that implement the Listener interface, as one will do. If you were going to use a non-main class as the listener, you would need to use PluginManager->registerEvents with an instance of that class as the first parameter. The second parameter is fine.
     
    jasonwynn10, Muqsit and Sandertv like 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.