hollow-cube/custom-component-translator

Signed-off-by: mworzala <mattheworzala@gmail.com>

Allow custom component translator implementation

(cherry picked from commit 31fd6317ad4f514cd2fad11221607d401fdbae0a)
This commit is contained in:
mworzala 2022-12-10 21:16:07 -05:00
parent bc73a6cbf8
commit d40e85dedb
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
4 changed files with 18 additions and 8 deletions

View File

@ -2,7 +2,8 @@
Not a "proper" versioned changelog, just a list of the changes from Minestom master.
Some of these are pending, some deserve PRs, others are just minor tweaks
* Delete extensions (`mworzala/Minestom` @ `no_more_extensions`)
* Block face in digging events (`mworzala/Minestom` @ `block_break_face`)
* Add cursor position to block place and neighbor updates (`Moulberry/Minestom` @ `block_placement_rewrite_2`)
* **breaking** Delete extensions (`mworzala/Minestom` @ `no_more_extensions`)
* **breaking** Block face in digging events (`mworzala/Minestom` @ `block_break_face`)
* **breaking** Add cursor position to block place and neighbor updates (`Moulberry/Minestom` @ `block_placement_rewrite_2`)
* Change `Entity#getInstance` to @UnknownNullability
* Support custom component translator for serverside translation

View File

@ -2,12 +2,15 @@ package net.minestom.server.adventure;
import java.io.StringReader;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.util.Codec;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTException;
@ -27,6 +30,8 @@ public final class MinestomAdventure {
* If components should be automatically translated in outgoing packets.
*/
public static boolean AUTOMATIC_COMPONENT_TRANSLATION = false;
// todo: Need to properly add a translator interface so it can check for presence of a key for the flattener.
public static BiFunction<Component, Locale, Component> COMPONENT_TRANSLATOR = GlobalTranslator::render;
static final Localizable NULL_LOCALIZABLE = () -> null;

View File

@ -1,5 +1,6 @@
package net.minestom.server.adventure.provider;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.translation.GlobalTranslator;
@ -15,10 +16,13 @@ final class MinestomFlattenerProvider {
// handle server-side translations if needed
builder.complexMapper(TranslatableComponent.class, ((component, consumer) -> {
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION) {
for (final Translator source : GlobalTranslator.translator().sources()) {
if (source instanceof TranslationRegistry registry && registry.contains(component.key())) {
consumer.accept(GlobalTranslator.render(component, MinestomAdventure.getDefaultLocale()));
}
final Component translated = MinestomAdventure.COMPONENT_TRANSLATOR.apply(component, MinestomAdventure.getDefaultLocale());
// In case the translated component is also a translatable component, we just leave the key to avoid infinite recursion
if (translated instanceof TranslatableComponent translatable) {
consumer.accept(Component.text(translatable.key()));
} else {
consumer.accept(translated);
}
}
}));

View File

@ -365,7 +365,7 @@ public class PlayerSocketConnection extends PlayerConnection {
if (player != null) {
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION && serverPacket instanceof ComponentHoldingServerPacket) {
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
MinestomAdventure.COMPONENT_TRANSLATOR.apply(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
}
}
try (var hold = ObjectPool.PACKET_POOL.hold()) {