PEMapModder PEMapModder - 2 months ago 12
PHP Question

How to create commands with localized description in PocketMine plugins?

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.

Answer

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);
    }
}
Comments