Custom localization for each player (#537)

This commit is contained in:
David Mayr 2021-07-26 03:50:44 +02:00 committed by GitHub
parent adcf428e8f
commit 3d8e9091c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 5 deletions

View File

@ -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.
*
* <p>This can be {@code null} when the client has not yet connected to any server.</p>
*
* @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.

View File

@ -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);
}