PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
HelpCommand.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\command\defaults;
23 
28 
30 
31  public function __construct($name){
32  parent::__construct(
33  $name,
34  "Shows the help menu",
35  "/help [pageNumber]\n/help <topic> [pageNumber]",
36  ["?"]
37  );
38  $this->setPermission("pocketmine.command.help");
39  }
40 
41  public function execute(CommandSender $sender, $currentAlias, array $args){
42  if(!$this->testPermission($sender)){
43  return true;
44  }
45 
46  if(count($args) === 0){
47  $command = "";
48  $pageNumber = 1;
49  }elseif(is_numeric($args[count($args) - 1])){
50  $pageNumber = (int) array_pop($args);
51  if($pageNumber <= 0){
52  $pageNumber = 1;
53  }
54  $command = implode(" ", $args);
55  }else{
56  $command = implode(" ", $args);
57  $pageNumber = 1;
58  }
59 
60  if($sender instanceof ConsoleCommandSender){
61  $pageHeight = PHP_INT_MAX;
62  }else{
63  $pageHeight = 5;
64  }
65 
66  if($command === ""){
68  $commands = [];
69  foreach($sender->getServer()->getCommandMap()->getCommands() as $command){
70  if($command->testPermissionSilent($sender)){
71  $commands[$command->getName()] = $command;
72  }
73  }
74  ksort($commands, SORT_NATURAL | SORT_FLAG_CASE);
75  $commands = array_chunk($commands, $pageHeight);
76  $pageNumber = (int) min(count($commands), $pageNumber);
77  if($pageNumber < 1){
78  $pageNumber = 1;
79  }
80  $message = TextFormat::RED . "-" . TextFormat::RESET . " Showing help page " . $pageNumber . " of " . count($commands) . " (/help <pageNumber>) " . TextFormat::RED . "-" . TextFormat::RESET . "\n";
81  if(isset($commands[$pageNumber - 1])){
82  foreach($commands[$pageNumber - 1] as $command){
83  $message .= TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::WHITE . $command->getDescription() . "\n";
84  }
85  }
86  $sender->sendMessage($message);
87 
88  return true;
89  }else{
90  if(($command = $sender->getServer()->getCommandMap()->getCommand(strtolower($command))) instanceof Command){
91  if($command->testPermissionSilent($sender)){
92  $message = TextFormat::YELLOW . "--------- " . TextFormat::WHITE . " Help: /" . $command->getName() . TextFormat::YELLOW . " ---------\n";
93  $message .= TextFormat::GOLD . "Description: " . TextFormat::WHITE . $command->getDescription() . "\n";
94  $message .= TextFormat::GOLD . "Usage: " . TextFormat::WHITE . implode("\n" . TextFormat::WHITE, explode("\n", $command->getUsage())) . "\n";
95  $sender->sendMessage($message);
96 
97  return true;
98  }
99  }
100  $sender->sendMessage(TextFormat::RED . "No help for " . strtolower($command));
101 
102  return true;
103  }
104  }
105 
106 }
execute(CommandSender $sender, $commandLabel, array $args)
testPermission(CommandSender $target)
Definition: Command.php:121
setPermission($permission)
Definition: Command.php:112