2

How to create commands with translated description or usage using the PocketMine API in plugins?

In vanilla commands, strings of translation codes such as %pocketmine.command.help.description are passed. They are references to the lang file in the original PocketMine, and plugins cannot modify them.

When a player executes /help, the HelpCommand class concatenates the result of Command::getDescription() with the help client output format. Then, it is passed to Player::sendMessage, which passes the string to BaseLang::translateString and then BaseLang::parseTranslation, and then internally lookup translations at occurrences of /%[a-zA-Z0-9\.\-]+/. Throughout this process, it is not possible to add anything, unless the server BaseLang is replaced.

SOFe
  • 7,867
  • 4
  • 33
  • 61

1 Answers1

2

Handle DataPacketSendEvent and modify TextPackets. Make your identifier something special that you can extract from arbitrary string.

Use this event handler:

public function e_dps(DataPacketSendEvent $event){
    if(($pk = $event->getPacket()) instanceof TextPacket){
        $pk->message = preg_replace_callback(
            '/PluginName\-SomeRandomBytesHere\-ForSecurityGenerateThemEveryRestart\-([0-9]+)\-AnotherDelimiterHere/', function($match){
                return $this->translate($match[1], $event->getPlayer());
            }, $pk->message);
    }
}
SOFe
  • 7,867
  • 4
  • 33
  • 61