PocketMine-MP 5.25.3 git-afc4a3c7f18d42b41cbfde84ab6a2e4dd7c03045
All Classes Namespaces Functions Variables Enumerations Enumerator 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
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
32use function array_chunk;
33use function array_pop;
34use function count;
35use function explode;
36use function implode;
37use function is_numeric;
38use function ksort;
39use function min;
40use function sort;
41use function strtolower;
42use const PHP_INT_MAX;
43use const SORT_FLAG_CASE;
44use const SORT_NATURAL;
45
47
48 public function __construct(){
49 parent::__construct(
50 "help",
51 KnownTranslationFactory::pocketmine_command_help_description(),
52 KnownTranslationFactory::commands_help_usage(),
53 ["?"]
54 );
55 $this->setPermission(DefaultPermissionNames::COMMAND_HELP);
56 }
57
58 public function execute(CommandSender $sender, string $commandLabel, array $args){
59 if(count($args) === 0){
60 $commandName = "";
61 $pageNumber = 1;
62 }elseif(is_numeric($args[count($args) - 1])){
63 $pageNumber = (int) array_pop($args);
64 if($pageNumber <= 0){
65 $pageNumber = 1;
66 }
67 $commandName = implode(" ", $args);
68 }else{
69 $commandName = implode(" ", $args);
70 $pageNumber = 1;
71 }
72
73 $pageHeight = $sender->getScreenLineHeight();
74
75 if($commandName === ""){
76 $commands = [];
77 foreach($sender->getServer()->getCommandMap()->getCommands() as $command){
78 if($command->testPermissionSilent($sender)){
79 $commands[$command->getLabel()] = $command;
80 }
81 }
82 ksort($commands, SORT_NATURAL | SORT_FLAG_CASE);
83 $commands = array_chunk($commands, $pageHeight);
84 $pageNumber = min(count($commands), $pageNumber);
85 if($pageNumber < 1){
86 $pageNumber = 1;
87 }
88 $sender->sendMessage(KnownTranslationFactory::commands_help_header((string) $pageNumber, (string) count($commands)));
89 $lang = $sender->getLanguage();
90 if(isset($commands[$pageNumber - 1])){
91 foreach($commands[$pageNumber - 1] as $command){
92 $description = $command->getDescription();
93 $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description;
94 $sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getLabel() . ": " . TextFormat::RESET . $descriptionString);
95 }
96 }
97
98 return true;
99 }else{
100 if(($cmd = $sender->getServer()->getCommandMap()->getCommand(strtolower($commandName))) instanceof Command){
101 if($cmd->testPermissionSilent($sender)){
102 $lang = $sender->getLanguage();
103 $description = $cmd->getDescription();
104 $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description;
105 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_header($cmd->getLabel())
106 ->format(TextFormat::YELLOW . "--------- " . TextFormat::RESET, TextFormat::YELLOW . " ---------"));
107 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::RESET . $descriptionString)
108 ->prefix(TextFormat::GOLD));
109
110 $usage = $cmd->getUsage();
111 $usageString = $usage instanceof Translatable ? $lang->translate($usage) : $usage;
112 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_usage(TextFormat::RESET . implode("\n" . TextFormat::RESET, explode("\n", $usageString, limit: PHP_INT_MAX)))
113 ->prefix(TextFormat::GOLD));
114
115 $aliases = $cmd->getAliases();
116 sort($aliases, SORT_NATURAL);
117 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_aliases(TextFormat::RESET . implode(", ", $aliases))
118 ->prefix(TextFormat::GOLD));
119
120 return true;
121 }
122 }
123 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($commandName, "/help")->prefix(TextFormat::RED));
124
125 return true;
126 }
127 }
128}
execute(CommandSender $sender, string $commandLabel, array $args)