PocketMine-MP/src/command/defaults/HelpCommand.php

117 lines
3.7 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\command\defaults;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\lang\KnownTranslationKeys;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\utils\TextFormat;
use function array_chunk;
use function array_pop;
use function count;
use function explode;
use function implode;
use function is_numeric;
use function ksort;
use function min;
use function strtolower;
use const SORT_FLAG_CASE;
use const SORT_NATURAL;
class HelpCommand extends VanillaCommand{
public function __construct(string $name){
parent::__construct(
$name,
KnownTranslationKeys::POCKETMINE_COMMAND_HELP_DESCRIPTION,
KnownTranslationKeys::COMMANDS_HELP_USAGE,
["?"]
);
$this->setPermission(DefaultPermissionNames::COMMAND_HELP);
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
$commandName = "";
$pageNumber = 1;
}elseif(is_numeric($args[count($args) - 1])){
$pageNumber = (int) array_pop($args);
if($pageNumber <= 0){
$pageNumber = 1;
}
$commandName = implode(" ", $args);
}else{
$commandName = implode(" ", $args);
$pageNumber = 1;
}
$pageHeight = $sender->getScreenLineHeight();
if($commandName === ""){
/** @var Command[][] $commands */
$commands = [];
foreach($sender->getServer()->getCommandMap()->getCommands() as $command){
if($command->testPermissionSilent($sender)){
$commands[$command->getName()] = $command;
}
}
ksort($commands, SORT_NATURAL | SORT_FLAG_CASE);
$commands = array_chunk($commands, $pageHeight);
$pageNumber = min(count($commands), $pageNumber);
if($pageNumber < 1){
$pageNumber = 1;
}
$sender->sendMessage(KnownTranslationFactory::commands_help_header((string) $pageNumber, (string) count($commands)));
$lang = $sender->getLanguage();
if(isset($commands[$pageNumber - 1])){
foreach($commands[$pageNumber - 1] as $command){
$sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::WHITE . $lang->translateString($command->getDescription()));
}
}
return true;
}else{
if(($cmd = $sender->getServer()->getCommandMap()->getCommand(strtolower($commandName))) instanceof Command){
if($cmd->testPermissionSilent($sender)){
$message = TextFormat::YELLOW . "--------- " . TextFormat::WHITE . " Help: /" . $cmd->getName() . TextFormat::YELLOW . " ---------\n";
$message .= TextFormat::GOLD . "Description: " . TextFormat::WHITE . $sender->getLanguage()->translateString($cmd->getDescription()) . "\n";
$message .= TextFormat::GOLD . "Usage: " . TextFormat::WHITE . implode("\n" . TextFormat::WHITE, explode("\n", $sender->getLanguage()->translateString($cmd->getUsage()))) . "\n";
$sender->sendMessage($message);
return true;
}
}
$sender->sendMessage(TextFormat::RED . "No help for " . strtolower($commandName));
return true;
}
}
}