diff --git a/CHANGELOG.md b/CHANGELOG.md index c1d5b52af..80613f886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/net/minestom/server/adventure/MinestomAdventure.java b/src/main/java/net/minestom/server/adventure/MinestomAdventure.java index 27a51f5c9..fe62f85e6 100644 --- a/src/main/java/net/minestom/server/adventure/MinestomAdventure.java +++ b/src/main/java/net/minestom/server/adventure/MinestomAdventure.java @@ -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_TRANSLATOR = GlobalTranslator::render; static final Localizable NULL_LOCALIZABLE = () -> null; diff --git a/src/main/java/net/minestom/server/adventure/provider/MinestomFlattenerProvider.java b/src/main/java/net/minestom/server/adventure/provider/MinestomFlattenerProvider.java index cecc8ba6a..32055fb1c 100644 --- a/src/main/java/net/minestom/server/adventure/provider/MinestomFlattenerProvider.java +++ b/src/main/java/net/minestom/server/adventure/provider/MinestomFlattenerProvider.java @@ -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); } } })); diff --git a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java index cc335a6f6..a13733f4b 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -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()) {