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