mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-19 17:15:24 +01:00
hollow-cube/custom-component-translator
Signed-off-by: mworzala <mattheworzala@gmail.com>
Allow custom component translator implementation
(cherry picked from commit 31fd6317ad4f514cd2fad11221607d401fdbae0a)
(cherry picked from commit d40e85dedb
)
This commit is contained in:
parent
11e184874e
commit
9cb4e34955
@ -2,7 +2,8 @@
|
|||||||
Not a "proper" versioned changelog, just a list of the changes from Minestom master.
|
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
|
Some of these are pending, some deserve PRs, others are just minor tweaks
|
||||||
|
|
||||||
* Delete extensions (`mworzala/Minestom` @ `no_more_extensions`)
|
* **breaking** Delete extensions (`mworzala/Minestom` @ `no_more_extensions`)
|
||||||
* Block face in digging events (`mworzala/Minestom` @ `block_break_face`)
|
* **breaking** 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** Add cursor position to block place and neighbor updates (`Moulberry/Minestom` @ `block_placement_rewrite_2`)
|
||||||
* Change `Entity#getInstance` to @UnknownNullability
|
* Change `Entity#getInstance` to @UnknownNullability
|
||||||
|
* Support custom component translator for serverside translation
|
||||||
|
@ -2,12 +2,15 @@ package net.minestom.server.adventure;
|
|||||||
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.translation.GlobalTranslator;
|
||||||
import net.kyori.adventure.util.Codec;
|
import net.kyori.adventure.util.Codec;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBTException;
|
import org.jglrxavpok.hephaistos.nbt.NBTException;
|
||||||
@ -27,6 +30,8 @@ public final class MinestomAdventure {
|
|||||||
* If components should be automatically translated in outgoing packets.
|
* If components should be automatically translated in outgoing packets.
|
||||||
*/
|
*/
|
||||||
public static boolean AUTOMATIC_COMPONENT_TRANSLATION = false;
|
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;
|
static final Localizable NULL_LOCALIZABLE = () -> null;
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.adventure.provider;
|
package net.minestom.server.adventure.provider;
|
||||||
|
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.TranslatableComponent;
|
import net.kyori.adventure.text.TranslatableComponent;
|
||||||
import net.kyori.adventure.text.flattener.ComponentFlattener;
|
import net.kyori.adventure.text.flattener.ComponentFlattener;
|
||||||
import net.kyori.adventure.translation.GlobalTranslator;
|
import net.kyori.adventure.translation.GlobalTranslator;
|
||||||
@ -15,10 +16,13 @@ final class MinestomFlattenerProvider {
|
|||||||
// handle server-side translations if needed
|
// handle server-side translations if needed
|
||||||
builder.complexMapper(TranslatableComponent.class, ((component, consumer) -> {
|
builder.complexMapper(TranslatableComponent.class, ((component, consumer) -> {
|
||||||
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION) {
|
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION) {
|
||||||
for (final Translator source : GlobalTranslator.translator().sources()) {
|
final Component translated = MinestomAdventure.COMPONENT_TRANSLATOR.apply(component, MinestomAdventure.getDefaultLocale());
|
||||||
if (source instanceof TranslationRegistry registry && registry.contains(component.key())) {
|
|
||||||
consumer.accept(GlobalTranslator.render(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -365,7 +365,7 @@ public class PlayerSocketConnection extends PlayerConnection {
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION && serverPacket instanceof ComponentHoldingServerPacket) {
|
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION && serverPacket instanceof ComponentHoldingServerPacket) {
|
||||||
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
|
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()) {
|
try (var hold = ObjectPool.PACKET_POOL.hold()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user