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

Tutorial Plugin command

Discussion in 'Resources' started by Intyre, Dec 4, 2016.

  1. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    [​IMG]

    The final directory structure looks like:
    Code:
    MyPlugin
    ├── plugin.yml
    └── src
        └── pmmp
            └── MyPlugin
                └── Main.php
    
    Code:
    name: MyPlugin
    main: pmmp\MyPlugin\Main
    version: 0.0.1
    api: 2.1.0
    description: My Plugin
    author: pmmp
    
    commands:
      hello:
        description: "Sends 'Hello world!' to the sender"
        usage: "/hello"
    
    PHP:
    <?php

    namespace pmmp\MyPlugin;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;

    class 
    Main extends PluginBase{
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
            switch(
    $command->getName()){
            case 
    "hello":
                
    $sender->sendMessage("Hello world!");
                return 
    true;
            }
        }
    }
     
  2. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    What IDE is that??
     
    jasonwynn10 and applqpak like this.
  3. Gold

    Gold Silverfish

    Messages:
    23
    GitHub:
    goldcodes
    jasonwynn10 and applqpak like this.
  4. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    Thanks. I should really open my eyes. It says spacemacs right in the GIF.
     
    applqpak likes this.
  5. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    How does this work if you don't register events in onEnable()?
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You only have to register the events if you work with Listeners, they aren't needed for just commands. :p
     
    HimbeersaftLP and applqpak like this.
  7. PianoRalph04

    PianoRalph04 Witch

    Messages:
    60
    GitHub:
    MechRalph04
    It helps, how can I add that a player used the command and it will display their name?
    Exampe: PianoRalph04 said Hello World!
     
  8. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    PHP:
    $name $sender->getName();
    $this->getServer()->broadcastMessage("$name said {whatever}");
    If you want custom messages, you can use $args to make it happen
     
  9. PianoRalph04

    PianoRalph04 Witch

    Messages:
    60
    GitHub:
    MechRalph04
    Thanks
     
  10. M4nt0s

    M4nt0s Spider Jockey

    Messages:
    31
    GitHub:
    M4nt0s
    What meens the CASE in front of the "hello"?
    But hello is the Command?
     
  11. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    It's part of the PHP switch function.
     
    HimbeersaftLP likes this.
  12. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    What we really need is a tutorial on how to register commands per file, using the command map. I use it in all my plugins because its just cleaner imo
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    1. It is called extending the Command class directly, not registering commands per file.
    2. Usually it results in more redundant code rather than cleaner code.
    3. You shouldn't use this method if you don't understand why it works anyway.
    4. You shouldn't use this method if you think in files rather than thinking in classes.
     
    Jack Noordhuis likes this.
  14. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    What can I do that everyone will see the message "Hello world!"?
     
  15. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    $sender->level->broadcastMessage("Message");
     
  16. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    The function broadcastMessage is not defined, how do I define it that everything will work?
     
  17. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
  18. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  19. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Oops, I meant to add getServer...

    It should be $sender->getLevel()->getServer()->broadcastMessage("message");
     
  20. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Plus, how can I find out where a variable or a function is defined?
    Do I need to get used how PMMP works?
     
  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.