From efca0b5a246898d242b45be9ba6226e2701cdffc Mon Sep 17 00:00:00 2001 From: David Mayr Date: Fri, 9 Jul 2021 15:18:12 +0200 Subject: [PATCH 1/6] Added method to specify a custom locale for the proxy to use for each player --- .../com/velocitypowered/api/proxy/Player.java | 12 ++++++++++ .../connection/client/ConnectedPlayer.java | 22 ++++++++++++++----- 2 files changed, 29 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 f4207477d8..5f8aec5f8e 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.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,17 @@ public interface Player extends CommandSource, Identified, InboundConnection, */ String getUsername(); + /** + * + * @return the locale the velocity proxy is translating its messages to. By default {@link PlayerSettings#getLocale()} + * Can be null when the settings have not been initialized yet. + */ + @Nullable Locale getProxyLocale(); + + /* + Change the locale the proxy is translating its messages to. + */ + void setProxyLocale(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 10d05c8b82..bb17730072 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.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.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.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 proxyLocale; ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, @Nullable InetSocketAddress virtualHost, boolean onlineMode) { @@ -178,6 +175,21 @@ public String getUsername() { return profile.getName(); } + @Override + public Locale getProxyLocale() { + if(proxyLocale == null){ + if(settings == null) + return null; + return settings.getLocale(); + } + return proxyLocale; + } + + @Override + public void setProxyLocale(Locale locale) { + proxyLocale = locale; + } + @Override public UUID getUniqueId() { return profile.getId(); @@ -276,7 +288,7 @@ public ProtocolVersion getProtocolVersion() { */ public Component translateMessage(Component message) { Locale locale = ClosestLocaleMatcher.INSTANCE - .lookupClosest(this.settings == null ? Locale.getDefault() : this.settings.getLocale()); + .lookupClosest(getProxyLocale() == null ? Locale.getDefault() : getProxyLocale()); return GlobalTranslator.render(message, locale); } From d5cf598e9e801bd93c0c940c162f90236f8f767f Mon Sep 17 00:00:00 2001 From: David Mayr Date: Fri, 9 Jul 2021 15:49:58 +0200 Subject: [PATCH 2/6] Fixed checkstyle problems --- .../main/java/com/velocitypowered/api/proxy/Player.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 5f8aec5f8e..9be3389394 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -47,9 +47,12 @@ public interface Player extends CommandSource, Identified, InboundConnection, String getUsername(); /** + * The locale the velocity proxy is translating its messages to. By default {@link PlayerSettings#getLocale()} + * + *

Can be null when the settings have not been initialized yet.

+ * + * @return the locale. * - * @return the locale the velocity proxy is translating its messages to. By default {@link PlayerSettings#getLocale()} - * Can be null when the settings have not been initialized yet. */ @Nullable Locale getProxyLocale(); From 0c1c2daaa1f83f63f64f7f864cd5bf3f3e227c65 Mon Sep 17 00:00:00 2001 From: David Mayr Date: Fri, 9 Jul 2021 15:54:01 +0200 Subject: [PATCH 3/6] Fixed checkstyle problems in impl --- .../proxy/connection/client/ConnectedPlayer.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 bb17730072..a07bd8463a 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 @@ -177,9 +177,10 @@ public String getUsername() { @Override public Locale getProxyLocale() { - if(proxyLocale == null){ - if(settings == null) + if (proxyLocale == null) { + if (settings == null) { return null; + } return settings.getLocale(); } return proxyLocale; From ea489c18844b61334d73a58ce2032d8302c8773b Mon Sep 17 00:00:00 2001 From: David Mayr Date: Sun, 25 Jul 2021 02:17:04 +0200 Subject: [PATCH 4/6] Changed name to effectiveLocale, corrected javadocs --- .../java/com/velocitypowered/api/proxy/Player.java | 13 +++++++------ .../proxy/connection/client/ConnectedPlayer.java | 14 +++++++------- 2 files changed, 14 insertions(+), 13 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 9be3389394..425a9287b6 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -49,17 +49,18 @@ public interface Player extends CommandSource, Identified, InboundConnection, /** * The locale the velocity proxy is translating its messages to. By default {@link PlayerSettings#getLocale()} * - *

Can be null when the settings have not been initialized yet.

+ *

Can be {@code null} when the settings have not been initialized yet.

* * @return the locale. - * */ - @Nullable Locale getProxyLocale(); + @Nullable Locale getEffectiveLocale(); - /* - Change the locale the proxy is translating its messages to. + /** + * Change the locale the proxy is translating its messages to. + * + * @param locale the locale to translate to */ - void setProxyLocale(Locale locale); + 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 a07bd8463a..e8ba13c010 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 @@ -145,7 +145,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { .withStatic(PermissionChecker.POINTER, getPermissionChecker()) .build(); private @Nullable String clientBrand; - private @Nullable Locale proxyLocale; + private @Nullable Locale effectiveLocale; ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, @Nullable InetSocketAddress virtualHost, boolean onlineMode) { @@ -176,19 +176,19 @@ public String getUsername() { } @Override - public Locale getProxyLocale() { - if (proxyLocale == null) { + public Locale getEffectiveLocale() { + if (effectiveLocale == null) { if (settings == null) { return null; } return settings.getLocale(); } - return proxyLocale; + return effectiveLocale; } @Override - public void setProxyLocale(Locale locale) { - proxyLocale = locale; + public void setEffectiveLocale(Locale locale) { + effectiveLocale = locale; } @Override @@ -289,7 +289,7 @@ public ProtocolVersion getProtocolVersion() { */ public Component translateMessage(Component message) { Locale locale = ClosestLocaleMatcher.INSTANCE - .lookupClosest(getProxyLocale() == null ? Locale.getDefault() : getProxyLocale()); + .lookupClosest(getEffectiveLocale() == null ? Locale.getDefault() : getEffectiveLocale()); return GlobalTranslator.render(message, locale); } From 178f40a93bcc2d9e20aaccaca2d4a44888bac455 Mon Sep 17 00:00:00 2001 From: David Mayr Date: Sun, 25 Jul 2021 14:12:37 +0200 Subject: [PATCH 5/6] Simplified getEffectiveLocale, javadoc updates --- api/src/main/java/com/velocitypowered/api/proxy/Player.java | 4 ++-- .../proxy/connection/client/ConnectedPlayer.java | 5 +---- 2 files changed, 3 insertions(+), 6 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 425a9287b6..caee0b4c95 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -49,14 +49,14 @@ public interface Player extends CommandSource, Identified, InboundConnection, /** * The locale the velocity proxy is translating its messages to. By default {@link PlayerSettings#getLocale()} * - *

Can be {@code null} when the settings have not been initialized yet.

+ *

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 is translating its messages to. + * Change the locale the proxy will be translating its messages to. * * @param locale the locale to translate to */ 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 e8ba13c010..f89e0258d4 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 @@ -177,10 +177,7 @@ public String getUsername() { @Override public Locale getEffectiveLocale() { - if (effectiveLocale == null) { - if (settings == null) { - return null; - } + if (effectiveLocale == null && settings != null) { return settings.getLocale(); } return effectiveLocale; From 089a936faccf0e9b5bb3d7a8363a2e9216668ed6 Mon Sep 17 00:00:00 2001 From: David Mayr Date: Mon, 26 Jul 2021 03:40:34 +0200 Subject: [PATCH 6/6] Used the javadoc suggested by astei --- api/src/main/java/com/velocitypowered/api/proxy/Player.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 caee0b4c95..76278f90fc 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -47,7 +47,8 @@ public interface Player extends CommandSource, Identified, InboundConnection, String getUsername(); /** - * The locale the velocity proxy is translating its messages to. By default {@link PlayerSettings#getLocale()} + * 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.

*