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

Command with args

Comments in 'Plugin Development' started by FlamingGenius, Mar 11, 2016.

  1. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Not sure if i coded this correctly?

    PHP:
       
        
    private function getQuestion($type){
            
    $questions $this->getConfig()->get($type);
            
    $draw array_rand($questions);
           
            
    $question $questions[$draw];
            return 
    $question;
        }
       
        public function 
    onCommand(CommandSender $sender,Command $command,$label,array $args){
            
    $cmd $command->getName();
            if(
    $cmd == "wyr"){
                if(!isset(
    $args[0])){
                    
    $sender->sendMessage("/wyr <type>");
                    return;
                } else{
                    switch(
    $args[0]){
                        case 
    "help":
                           
                        break;
                        case 
    "children":
                            
    $getQuestion $this->getQuestion("children-questions");
                            
    $this->getServer()->broadcastMessage($getQuestion);
                        break;
                        case 
    "adult":
                            
    $getQuestion $this->getQuestion("adult-questions");
                            
    $this->getServer()->broadcastMessage($getQuestion);
                        break;
                        case 
    "funny":
                            
    $getQuestion $this->getQuestion("funny-questions");
                            
    $this->getServer()->broadcastMessage($getQuestion);
                        break;
                        case 
    "hard":
                            
    $getQuestion $this->getQuestion("hard-questions");
                            
    $this->getServer()->broadcastMessage($getQuestion);
                        break;
                    }
                }
            }
        }
    }
    ?>

    edit:

    Wanting to make help option like /wyr help and have it display the options like
    1.funny
    2.children
    have them in like a list format would I just send a different message for each like this?

    PHP:
    $sender->sendMessage("1.type1");
    $sender->sendMessage("2.type2");
    //and so on
    Last edited: Mar 11, 2016
  2. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    You should check if the command is 'wyr' or not, then messing around with those args...
    applqpak likes this.
  3. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    that looking better
  4. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Yes it works, another example. You can also use commandMap.
    PHP:
    public function onCommand(CommandSender $issuerCommand $cmd$alias, array $args){
    $cmd strtolower($cmd->getName()){
    switch(
    $cmd){
    case 
    'wyr':
    if(!isset(
    $args[0])){
    $issuer->sendMessage("Usage: /wyr <type>");
    return;
    }
    if(
    strtolower($args[0]) === "children"){
    $getQuestion  $this->getQuestion("children-questions");
    $this->getServer()->broadcastMessage($getQuestion);
    }
    }
    }
    applqpak likes this.
  5. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    You should return after sending the messages, otherwise you'll get the usage set in plugin.yml. Also, you should use strtolower() around the command. I.E:
    PHP:
    if(strtolower($cmd->getName()) === "wyr") {
    // Do stuff
    }
    Vaivez66 likes this.
  6. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    If you don't return the code will keep running.
    applqpak likes this.
  7. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    no need to strtolower(), PocketMine will strtolower it immedetly
  8. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Ah ok.

Share This Page

Advertisement