From 3d8e9091c0d91f16d78da06662b77f11b9b6edfb Mon Sep 17 00:00:00 2001 From: David Mayr Date: Mon, 26 Jul 2021 03:50:44 +0200 Subject: [PATCH] Custom localization for each player (#537) --- .../com/velocitypowered/api/proxy/Player.java | 17 ++++++++++++++++ .../connection/client/ConnectedPlayer.java | 20 ++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index f4207477d..76278f90f 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -19,6 +19,7 @@ import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.util.ModInfo; import java.util.List; +import java.util.Locale; import java.util.Optional; import java.util.UUID; import java.util.function.UnaryOperator; @@ -45,6 +46,22 @@ public interface Player extends CommandSource, Identified, InboundConnection, */ String getUsername(); + /** + * Returns the locale the proxy will use to send messages translated via the Adventure global translator. + * By default, the value of {@link PlayerSettings#getLocale()} is used. + * + *

This can be {@code null} when the client has not yet connected to any server.

+ * + * @return the locale. + */ + @Nullable Locale getEffectiveLocale(); + + /** + * Change the locale the proxy will be translating its messages to. + * + * @param locale the locale to translate to + */ + void setEffectiveLocale(Locale locale); /** * Returns the player's UUID. diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 10d05c8b8..f89e0258d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -48,7 +48,6 @@ import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.util.ModInfo; import com.velocitypowered.proxy.VelocityServer; -import com.velocitypowered.proxy.config.VelocityConfiguration; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; @@ -79,7 +78,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.Objects; import java.util.Optional; import java.util.Queue; import java.util.UUID; @@ -97,8 +95,6 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; -import net.kyori.adventure.title.Title; -import net.kyori.adventure.title.Title.Times; import net.kyori.adventure.translation.GlobalTranslator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -149,6 +145,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { .withStatic(PermissionChecker.POINTER, getPermissionChecker()) .build(); private @Nullable String clientBrand; + private @Nullable Locale effectiveLocale; ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, @Nullable InetSocketAddress virtualHost, boolean onlineMode) { @@ -178,6 +175,19 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return profile.getName(); } + @Override + public Locale getEffectiveLocale() { + if (effectiveLocale == null && settings != null) { + return settings.getLocale(); + } + return effectiveLocale; + } + + @Override + public void setEffectiveLocale(Locale locale) { + effectiveLocale = locale; + } + @Override public UUID getUniqueId() { return profile.getId(); @@ -276,7 +286,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { */ public Component translateMessage(Component message) { Locale locale = ClosestLocaleMatcher.INSTANCE - .lookupClosest(this.settings == null ? Locale.getDefault() : this.settings.getLocale()); + .lookupClosest(getEffectiveLocale() == null ? Locale.getDefault() : getEffectiveLocale()); return GlobalTranslator.render(message, locale); }