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. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Take a look at your uses when you're building your plugin. Head over to the docs and then specify what you need, go through class members etc and you will find it :)
     
  2. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I add a config? Like this:
    $sender->broadcastMessage("<textInConfig>");
     
  3. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Go make another thread. This isn't the appropriate place.

    use statements will not define variables and functions in your plugin. You define those. The only exception is when you extend existing classes.
     
    Zuruki likes this.
  4. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Create a folder named resources. Put your config.yml inside there.
    Then create a value inside it. It would be in your case called BroadCast:
    Code:
    ###Config example
    
    Broadcast: <whatever you want>
    Then you need to call the value of broadcast, which will be
    PHP:
    $this->cfg = new Config($this->getDataFolder."config.yml"Config::YAML);
    PHP:
    this->cfg->get("Broadcast");
    You will then need to define it as a variable, which I expect you already know how to do.
     
  5. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    I should have wrote it more detailed, but this is what I meant. Let's say he had
    use pocketmine\entity\event\EntityDamageEvent
    In his uses, he would then go to the docs and specify through what he needed. When he has done this, it would provide him with his info.
     
    corytortoise likes this.
  6. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Is this correct? Because it doesn't work how I want it to be:
    PHP:
    <?php

    namespace pmmp\SenderPE;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\permission\ServerOperator;
    use 
    pocketmine\event\player\PlayerChatEvent;

    $this->yml = new Config($this->getDataFolder."config.yml"Config::YAML);

    class 
    Main extends PluginBase{
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
            switch(
    $command->getName()){
            case 
    "sendnews":
            if(
    $sender->hasPermission("senderpe.news")){
            if(isset(
    $args[0])) {
                
    $msg $this->yml->get("Broadcast");
                
    $sender->sendMessage("Sent the message " $msg);
                
    $sender->getLevel()->getServer()->broadcastMessage("§3§l[News]§r§3 " implode(" "$args));
                return 
    true;
                } else {
                
    $sender->sendMessage("§cYou cannot send an empty message!");
                }
             } else {
                
    $sender->sendMessage("§cYou need OP to do this!");
                return 
    true;
                }
            }
        }
    }
     
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Anything wrong with this? Or do I need to open a new thread/question?
     
  8. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Plus, what does the variable $label do? When do you use it?
     
  9. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    $label is the actual command that was used to execute your code, which is especially useful when using aliases. Say you have a command '/hello <player>'. The hello command has an alias of hi. If some executes the command /hi without a player, you could make it so it says /hi <player>, instead of the default usage.
     
    jasonwynn10 likes this.
  10. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    What do you mean without a player? I don't understand why $label...
     
  11. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    /msg and /tell are aliases for the same command. They aren't both registered separately, only one command is, but with an alias. When you do Command::getName(), that isn't always what the sender typed, $label is.
     
    Last edited: Apr 25, 2017
    jasonwynn10 likes this.
  12. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Say my command is /hello.
    /hello has the alias /hi.
    The usage of /hello is /hello <player>.
    A player uses /hi and does not enter a player, and thus gets to see a usage message.
    The usage message says /hello <player>, while the player used /hi.
    The label is the actual command used, AKA hi in this case, while the command is hello.
    This allows more detailed usage messages for example.
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    And I don't understand why you write CommandObject::getName() when there is something existent called Command::getName()...
     
    corytortoise and Jack Noordhuis like this.
  14. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    What would be a good short example code with the $label used?
     
  15. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
  16. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I was trying to specify that getName() isn't a static method, but I could have done a better job. I'll edit it.
     
  17. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Consider calling it as Command->getName() :) or $Command->getName() if you don't like the wrong syntax ;) or even $pocketmine_command_Command->getName() for FQN.
     
    Levi, jasonwynn10 and corytortoise like this.
  18. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    $this->getServer()->broadcastMessage("message");
     
  19. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    We had NO knowledge of what his code looks like, so you can't just assume his command was in a class extending PluginBase. Don't post useless posts please
     
    corytortoise and SalmonDE like this.
  20. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    True, but you can take a shortcut by doing $sender->getServer()
     
    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.