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

Calling player from a command

Comments in 'Plugin Development' started by RekkuzaRage, May 12, 2015.

  1. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    I am wondering how do I make a command that detects or calls a player? Here is an example:
    /hey <player>

    Sends a hey to the selected player.
  2. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    PHP:
    $this->getServer()->getPlayer($playername)->sendMessage("Hey! :)");
    iksaku likes this.
  3. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    Not really what I was looking for. Thanks tho. I looking into MassiveEconomy and saw you used args to define the player and money with the /pay <player> <amount>.
    Could you show me an example command that calls the player? /poke <player>
  4. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    $args is the array containing all command parameters. Each value of array is split by space.
    For example if you run: /example player, $args[0] will be example and $args[1] will be player
    Simply replace $playername with $args[1] and you'll get the name of the specified player
    iksaku likes this.
  5. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    this will cause an error if $this->getServer()->getPlayer($playername) is null.
    you should do this just to be safe:
    if($this->getServer()->getPlayer($playername) instanceof Player){
    $this->getServer()->getPlayer($playername)->sendMessage("Hey! :)");
    }
    iksaku and EvolSoft like this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    We don't always call it $args.

    Off-topic:
    Last time I got too addicted, whenever I use array_shfit() I want to put $args inside it. Since array_shift() accepts the parameter by reference, PhpStorm didn't show a warning. Eventually, it took me quite a while to find out the problem :(
    TigerHix and EvolSoft like this.
  7. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Yeah. But usually we call the arguments array $args :p
    iksaku likes this.

Share This Page

Advertisement