How do I make a command argument? And how do I make it so I can do something like "/command <player>"
Here is an example. This is my example command, /hi <player> It will send a message to a player when executed. PHP: case "Hi": // The command!if(!isset($args[0]) { // $args[0] represents/is the entered player, <player> // Checks if user typed the player, if not it will output the correct way to use the command. $sender->sendMessage("/hey <player>");} $player = $this->getServer()->getPlayer($args[0]); if($player instanceof Player) { // Checks if the player/$args[0] is a player. If true, then it will send the message to the player. $player->sendMessage("Hi!");}else { // If not then... $sender->sendMessaage("Player not found");}
Ok that helped a lot. But the plugin i'm making is something that lets the player send the console a message. Like how you can do "/tell <player> <message>" How could I do something like that?
This is the code I used to create my InventoryClear plugin: PHP: public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) { if(strtolower($cmd->getName()) === "clearinv") { if($sender->hasPermission("inventoryclear.command.clearinv")) { if(count($args) === 1) { $name = $args[0]; $target = $this->getServer()->getPlayer($name); if($target instanceof Player) { $target->getInventory()->setContents(array(Item::get(0, 0, 0))); $target->sendMessage("Your inventory has been cleared by " . $sender->getName()); $sender->sendMessage("Cleared " . $target->getName() . "'s inventory."); } else { $sender->sendMessage("Sorry, " . $args[0] . " is not online!"); } } else { $sender->sendMessage("Usage: /clearinv <playername>"); } } else { $sender->sendMessage("You don't have permissions to use this command."); } } } Hope it helps
Why are you guys so good at going off-topic? I like using this method to handle optional unordered (in any order) parameters like this. For example, if we have a command like this: Code: /cmd <mandatory argument a> <mandatory argument b> [.c <optional argument c>] [.d <optional argument d>] I would handle like this: PHP: public function onCommand(CommandSender $issuer, Command $cmd, $lbl, array $params){ if($cmd->getName() === "cmd"){ if(!isset($params[1])){ // if parameter 1 (the first parameter is parameter 0, second is parameter 1) doesn't exist, it is wrong usage. return false; // exit with "false" implying wrong usage } $a = array_shift($params); // pull out the first parameter, and shift the parameters behind front, i.e. parameter 1 now becomes parameter 0, vice versa $b = array_shift($params); // pull out the CURRENT first parameter, i.e. the second parameter // now we have got $a and $b as mandatory arguments a and b // now let's detect the optional arguments $c = "c"; // default value of optional argument c $d = 1; // default value of optional argument d while(isset($params[0])){ // while there are still values in $params, i.e. you still have to handle other arguments $name = array_shift($params); // shfit the next argument as $name switch(strtolower($name)){ // convert $name to lowercase case ".c": if(!isset($params[0])){ $issuer->sendMessage(TextFormat::YELLOW . "No value passed for \".c\", assuming default value (\"c\")"); break; // assume .c didn't get included } $c = array_shift($args); break; case ".d": if(!isset($params[0])){ $issuer->sendMessage(TextFormat::YELLOW . "No value passed for \".d\", assuming default value (\"d\")"); break; // assume .d didn't get included } $d = (int) array_shift($args); // convert to integer break; } } // execute code with $a, $b, $c and $d }} See WorldEditArt source for more details.
When I implode the argument should it be something like this? PHP: implode($args[0]); Or this PHP: implode($args);
It is a better practice to put it in the correct order, although PHP will swap them for you anyway: PHP: implode($glue, $pieces);