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

How to detect players with a command.

Comments in 'Plugin Development' started by xFlare, Nov 21, 2014.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I was wondering if someone can show me in detail how to detect a player using a command. For example:

    /crash (Player)

    I don't know how to make to make my plugin detect that "player". Can someone help me? Thanks.;)


    Also please make your answer detailed. :)
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Step 1. Get the string that refers to the player. In this case, it should be $args[0].
    Step 2. Get the Player object from the string. You can use $server->getPlayer($name) to get a player with the shortest name that starts with $name. You can also use $server->getPlayerExact($name) so that the player must be exact $name and not anything else. Note that these searches are case-insensitive. Also, make a $player instanceof Player check so that you can handle errors if there is no player of such name.
    EvolSoft and 64FF00 like this.
  3. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    297
    Plugins:
    2
    Minecraft User:
    64FF00
    Code:
    public function onCommand(CommandSender $sender, Command $cmd, $label, array $args)
        {
            switch($cmd->getName())
            {
                case "crash":
                  
                    if(!isset($args[0]))
                    {
                        $sender->sendMessage("[EXAMPLEPLUGIN] Usage: /crash <player>");
                      
                        break;
                    }
                  
                    $player = $this->getServer()->getPlayer($args[0]);
                  
                    if(!$player instanceof Player)
                    {
                        // ...
                      
                        break;
                    }
                  
                    // ...
                  
                    break;
                  
                // ...
            }
    
            return true;
        }
    Last edited: Nov 21, 2014
    EvolSoft likes this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Remember the return value! :)
    EvolSoft and 64FF00 like this.
  5. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Thank you! I was looking for this :)
  6. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    By return the value do you mean:

    return true;

Share This Page

Advertisement