From d74824c8d5e979d2c3147aabad54b41345dd5310 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 18 Oct 2022 19:34:12 +0100 Subject: [PATCH] Correctly use Command->getLabel() instead of Command->getName() getName() essentially serves as an ID for the command for CommandExecutors. It has no other sane use case. Since it's not unique (multiple commands with the same name may be registered, and the fallback alias will be used on conflict), it cannot be used for array indexing. It's also not correct to use it for any display purpose, since the command may not be able to be invoked by its 'name' if there was a conflict. There is an open debate about what to do with getName() and the wider CommandExecutor ecosystem, but that's a topic for another discussion. closes #5344 --- src/command/SimpleCommandMap.php | 2 +- src/command/defaults/HelpCommand.php | 6 +++--- src/network/mcpe/NetworkSession.php | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/command/SimpleCommandMap.php b/src/command/SimpleCommandMap.php index 5c33bf25a..bab101d5d 100644 --- a/src/command/SimpleCommandMap.php +++ b/src/command/SimpleCommandMap.php @@ -139,7 +139,7 @@ class SimpleCommandMap implements CommandMap{ public function register(string $fallbackPrefix, Command $command, ?string $label = null) : bool{ if($label === null){ - $label = $command->getName(); + $label = $command->getLabel(); } $label = trim($label); $fallbackPrefix = strtolower(trim($fallbackPrefix)); diff --git a/src/command/defaults/HelpCommand.php b/src/command/defaults/HelpCommand.php index 7fe12e039..3dc581838 100644 --- a/src/command/defaults/HelpCommand.php +++ b/src/command/defaults/HelpCommand.php @@ -80,7 +80,7 @@ class HelpCommand extends VanillaCommand{ $commands = []; foreach($sender->getServer()->getCommandMap()->getCommands() as $command){ if($command->testPermissionSilent($sender)){ - $commands[$command->getName()] = $command; + $commands[$command->getLabel()] = $command; } } ksort($commands, SORT_NATURAL | SORT_FLAG_CASE); @@ -95,7 +95,7 @@ class HelpCommand extends VanillaCommand{ foreach($commands[$pageNumber - 1] as $command){ $description = $command->getDescription(); $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description; - $sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::WHITE . $descriptionString); + $sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getLabel() . ": " . TextFormat::WHITE . $descriptionString); } } @@ -106,7 +106,7 @@ class HelpCommand extends VanillaCommand{ $lang = $sender->getLanguage(); $description = $cmd->getDescription(); $descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description; - $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_header($cmd->getName()) + $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_header($cmd->getLabel()) ->format(TextFormat::YELLOW . "--------- " . TextFormat::WHITE, TextFormat::YELLOW . " ---------")); $sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::WHITE . $descriptionString) ->prefix(TextFormat::GOLD)); diff --git a/src/network/mcpe/NetworkSession.php b/src/network/mcpe/NetworkSession.php index ffd80e49d..5b090cc35 100644 --- a/src/network/mcpe/NetworkSession.php +++ b/src/network/mcpe/NetworkSession.php @@ -888,11 +888,11 @@ class NetworkSession{ public function syncAvailableCommands() : void{ $commandData = []; foreach($this->server->getCommandMap()->getCommands() as $name => $command){ - if(isset($commandData[$command->getName()]) || $command->getName() === "help" || !$command->testPermissionSilent($this->player)){ + if(isset($commandData[$command->getLabel()]) || $command->getLabel() === "help" || !$command->testPermissionSilent($this->player)){ continue; } - $lname = strtolower($command->getName()); + $lname = strtolower($command->getLabel()); $aliases = $command->getAliases(); $aliasObj = null; if(count($aliases) > 0){ @@ -900,7 +900,7 @@ class NetworkSession{ //work around a client bug which makes the original name not show when aliases are used $aliases[] = $lname; } - $aliasObj = new CommandEnum(ucfirst($command->getName()) . "Aliases", array_values($aliases)); + $aliasObj = new CommandEnum(ucfirst($command->getLabel()) . "Aliases", array_values($aliases)); } $description = $command->getDescription(); @@ -915,7 +915,7 @@ class NetworkSession{ ] ); - $commandData[$command->getName()] = $data; + $commandData[$command->getLabel()] = $data; } $this->sendDataPacket(AvailableCommandsPacket::create($commandData, [], [], []));