PocketMine-MP 5.36.1 git-eaa7c4834c8fe2f379d24e7f0ee6cc63cfb18ccc
Loading...
Searching...
No Matches
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
35use function array_chunk;
36use function array_key_first;
37use function array_pop;
38use function count;
39use function explode;
40use function implode;
41use function is_array;
42use function is_numeric;
43use function ksort;
44use function min;
45use function sort;
46use function strtolower;
47use const PHP_INT_MAX;
48use const SORT_FLAG_CASE;
49use const SORT_NATURAL;
50
52
53 public function __construct(string $namespace, string $name){
54 parent::__construct(
55 $namespace,
56 $name,
57 KnownTranslationFactory::pocketmine_command_help_description(),
58 KnownTranslationFactory::commands_help_usage()
59 );
60 $this->setPermission(DefaultPermissionNames::COMMAND_HELP);
61 }
62
63 public function execute(CommandSender $sender, string $commandLabel, array $args){
64 if(count($args) === 0){
65 $commandName = "";
66 $pageNumber = 1;
67 }elseif(is_numeric($args[count($args) - 1])){
68 $pageNumber = (int) array_pop($args);
69 if($pageNumber <= 0){
70 $pageNumber = 1;
71 }
72 $commandName = implode(" ", $args);
73 }else{
74 $commandName = implode(" ", $args);
75 $pageNumber = 1;
76 }
77
78 $pageHeight = $sender->getScreenLineHeight();
79
80 //TODO: maybe inject this in the constructor instead of assuming the server's command map?
81 $commandMap = $sender->getServer()->getCommandMap();
82 $userAliasMap = $sender->getCommandAliasMap();
83 if($commandName === ""){
84 $commands = [];
85 foreach($commandMap->getUniqueCommands() as $command){
86 if($command->testPermissionSilent($sender)){
87 $userAliases = $userAliasMap->getMergedAliases($command->getId(), $commandMap->getAliasMap());
88 $preferredAlias = $userAliases[array_key_first($userAliases)];
89 if(isset($commands[$preferredAlias])){
90 throw new AssumptionFailedError("Something weird happened during user/global alias resolving");
91 }
92 $commands[$preferredAlias] = $command;
93 }
94 }
95 ksort($commands, SORT_NATURAL | SORT_FLAG_CASE);
96 $commands = array_chunk($commands, $pageHeight, preserve_keys: true);
97 $pageNumber = min(count($commands), $pageNumber);
98 if($pageNumber < 1){
99 $pageNumber = 1;
100 }
101 $sender->sendMessage(KnownTranslationFactory::commands_help_header((string) $pageNumber, (string) count($commands)));
102 $lang = $sender->getLanguage();
103 if(isset($commands[$pageNumber - 1])){
104 foreach(Utils::promoteKeys($commands[$pageNumber - 1]) as $preferredAlias => $command){
105 $description = $command->getDescription();
106 $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description;
107 $sender->sendMessage(TextFormat::DARK_GREEN . "/$preferredAlias: " . TextFormat::RESET . $descriptionString);
108 }
109 }
110
111 return true;
112 }else{
113 if(($command = $commandMap->getCommand(strtolower($commandName), $userAliasMap)) !== null){
114 if(is_array($command)){
115 SimpleCommandMap::handleConflicted($sender, $commandName, $command, $commandMap->getAliasMap());
116 return true;
117 }
118 if($command->testPermissionSilent($sender)){
119 $lang = $sender->getLanguage();
120 $description = $command->getDescription();
121 $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description;
122 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_header($commandName)
123 ->format(TextFormat::YELLOW . "--------- " . TextFormat::RESET, TextFormat::YELLOW . " ---------"));
124 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::RESET . $descriptionString)
125 ->prefix(TextFormat::GOLD));
126
127 $usage = $command->getUsage() ?? "/$commandName";
128 $usageString = $usage instanceof Translatable ? $lang->translate($usage) : $usage;
129 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_usage(TextFormat::RESET . implode("\n" . TextFormat::RESET, explode("\n", $usageString, limit: PHP_INT_MAX)))
130 ->prefix(TextFormat::GOLD));
131
132 $aliases = $userAliasMap->getMergedAliases($command->getId(), $commandMap->getAliasMap());
133 sort($aliases, SORT_NATURAL);
134 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_aliases(TextFormat::RESET . implode(", ", $aliases))
135 ->prefix(TextFormat::GOLD));
136
137 return true;
138 }
139 }
140 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($commandName, "/" . $userAliasMap->getPreferredAlias("pocketmine:help", $commandMap->getAliasMap()))->prefix(TextFormat::RED));
141
142 return true;
143 }
144 }
145}
static handleConflicted(CommandSender $sender, string $alias, array $conflictedEntries, CommandAliasMap $fallbackAliasMap)
execute(CommandSender $sender, string $commandLabel, array $args)