13
0
geforkt von Mirrors/Velocity

Custom localization for each player (#537)

Dieser Commit ist enthalten in:
David Mayr 2021-07-26 03:50:44 +02:00 committet von GitHub
Ursprung adcf428e8f
Commit 3d8e9091c0
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 32 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -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.

Datei anzeigen

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