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

Error when trying to get ClientID of sender of command

Discussion in 'Development' started by Kabluinc, Mar 5, 2017.

  1. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    In this code I'm trying to get the ClientID of the sender of the command

    PHP:
    public function onCommand(CommandSender $senderCommand $command$label, array $args){

    switch(
    $command->getName()){

    case 
    "test":
    $clientID $sender->getClientID();
    $sender->sendMessage("Your Client ID is: " $clientID);
    }
    But this error shows in console when running the command

    Code:
    [20:18:49] [Server thread/INFO]: An unknown error occurred while attempting to perform this command
    [20:18:49] [Server thread/CRITICAL]: Unhandled exception executing command 'test': Call to undefined method pocketmine\command\ConsoleCommandSender::getClientID()
    [20:18:49] [Server thread/CRITICAL]: Error: "Call to undefined method pocketmine\command\ConsoleCommandSender::getClientID()" (EXCEPTION) in "/plugins/Test/TestPlugin/Main" at line 313
    
    Please help.
     
  2. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    You have to run the command as the player, looking at the error you tried running it on the console
     
  3. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    How would I do that?
     
  4. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Go on the server and run the command.
     
  5. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    Ohhhhh sorry. wow how could I forget. it shouldn't work on console anyway XD
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    You forget to check for instance of Player
    PHP:
    //not exact DO NOT COPY
    if ($sender instanceof Player$sender->sendMessage("Your Client ID is: " $sender->getClientID()); else $sender->sendMessage("Client ID not available")
    i probably could shorten it with shorthands
     
  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.