3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Run all components in cases where the locale is known through the translation registry.

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-04-17 07:02:39 -04:00
Ursprung 4318c179a4
Commit 3d0ca2732e
3 geänderte Dateien mit 40 neuen und 16 gelöschten Zeilen

Datei anzeigen

@ -80,6 +80,7 @@ import java.net.SocketAddress;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -95,6 +96,9 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@ -252,14 +256,21 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
return connection.getProtocolVersion(); return connection.getProtocolVersion();
} }
public Component translateMessage(Component message) {
Locale locale = this.settings == null ? Locale.getDefault() : this.settings.getLocale();
return GlobalTranslator.render(message, locale);
}
@Override @Override
public void sendMessage(@NonNull Identity identity, @NonNull Component message, public void sendMessage(@NonNull Identity identity, @NonNull Component message,
@NonNull MessageType type) { @NonNull MessageType type) {
Preconditions.checkNotNull(message, "message"); Preconditions.checkNotNull(message, "message");
Preconditions.checkNotNull(type, "type"); Preconditions.checkNotNull(type, "type");
Component translated = translateMessage(message);
connection.write(new ClientboundChatPacket( connection.write(new ClientboundChatPacket(
ProtocolUtils.getJsonChatSerializer(this.protocolVersion()).serialize(message), ProtocolUtils.getJsonChatSerializer(this.protocolVersion()).serialize(translated),
type == MessageType.CHAT type == MessageType.CHAT
? ClientboundChatPacket.CHAT_TYPE ? ClientboundChatPacket.CHAT_TYPE
: ClientboundChatPacket.SYSTEM_TYPE, : ClientboundChatPacket.SYSTEM_TYPE,
@ -269,18 +280,21 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
@Override @Override
public void sendActionBar(net.kyori.adventure.text.@NonNull Component message) { public void sendActionBar(net.kyori.adventure.text.@NonNull Component message) {
Component translated = translateMessage(message);
ProtocolVersion playerVersion = protocolVersion(); ProtocolVersion playerVersion = protocolVersion();
if (playerVersion.gte(ProtocolVersion.MINECRAFT_1_11)) { if (playerVersion.gte(ProtocolVersion.MINECRAFT_1_11)) {
// Use the title packet instead. // Use the title packet instead.
connection.write(new ClientboundTitlePacket( connection.write(new ClientboundTitlePacket(
ClientboundTitlePacket.SET_ACTION_BAR, ClientboundTitlePacket.SET_ACTION_BAR,
ProtocolUtils.getJsonChatSerializer(playerVersion).serialize(message) ProtocolUtils.getJsonChatSerializer(playerVersion).serialize(translated)
)); ));
} else { } else {
// Due to issues with action bar packets, we'll need to convert the text message into a // Due to issues with action bar packets, we'll need to convert the text message into a
// legacy message and then inject the legacy text into a component... yuck! // legacy message and then inject the legacy text into a component... yuck!
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
object.addProperty("text", LegacyComponentSerializer.legacySection().serialize(message)); object.addProperty("text", LegacyComponentSerializer.legacySection()
.serialize(translated));
connection.write(new ClientboundChatPacket( connection.write(new ClientboundChatPacket(
object.toString(), object.toString(),
ClientboundChatPacket.GAME_INFO_TYPE, ClientboundChatPacket.GAME_INFO_TYPE,
@ -307,22 +321,22 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
} }
@Override @Override
public void showTitle(net.kyori.adventure.title.@NonNull Title title) { public void showTitle(@NonNull Title title) {
if (this.protocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) { if (this.protocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_8) >= 0) {
GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(this GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(this
.protocolVersion()); .protocolVersion());
connection.delayedWrite(new ClientboundTitlePacket( connection.delayedWrite(new ClientboundTitlePacket(
ClientboundTitlePacket.SET_TITLE, ClientboundTitlePacket.SET_TITLE,
serializer.serialize(title.title()) serializer.serialize(translateMessage(title.title()))
)); ));
connection.delayedWrite(new ClientboundTitlePacket( connection.delayedWrite(new ClientboundTitlePacket(
ClientboundTitlePacket.SET_SUBTITLE, ClientboundTitlePacket.SET_SUBTITLE,
serializer.serialize(title.subtitle()) serializer.serialize(translateMessage(title.subtitle()))
)); ));
net.kyori.adventure.title.Title.Times times = title.times(); Times times = title.times();
if (times != null) { if (times != null) {
connection.delayedWrite(ClientboundTitlePacket.times(this.protocolVersion(), times)); connection.delayedWrite(ClientboundTitlePacket.times(this.protocolVersion(), times));
} }
@ -389,9 +403,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
* @param duringLogin whether the disconnect happened during login * @param duringLogin whether the disconnect happened during login
*/ */
public void disconnect0(Component reason, boolean duringLogin) { public void disconnect0(Component reason, boolean duringLogin) {
Component translated = this.translateMessage(reason);
logger.info("{} has disconnected: {}", this, logger.info("{} has disconnected: {}", this,
LegacyComponentSerializer.legacySection().serialize(reason)); LegacyComponentSerializer.legacySection().serialize(translated));
connection.closeWith(ClientboundDisconnectPacket.create(reason, this.protocolVersion())); connection.closeWith(ClientboundDisconnectPacket.create(translated, this.protocolVersion()));
} }
public @Nullable VelocityServerConnection getConnectedServer() { public @Nullable VelocityServerConnection getConnectedServer() {

Datei anzeigen

@ -55,6 +55,10 @@ public class VelocityTabList implements TabList {
Preconditions.checkNotNull(footer, "footer"); Preconditions.checkNotNull(footer, "footer");
GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer( GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(
connection.getProtocolVersion()); connection.getProtocolVersion());
Component translatedHeader = player.translateMessage(header);
Component translatedFooter = player.translateMessage(footer);
connection.write(new ClientboundHeaderAndFooterPacket( connection.write(new ClientboundHeaderAndFooterPacket(
serializer.serialize(header), serializer.serialize(header),
serializer.serialize(footer) serializer.serialize(footer)

Datei anzeigen

@ -19,6 +19,7 @@ package com.velocitypowered.proxy.util.bossbar;
import com.google.common.collect.MapMaker; import com.google.common.collect.MapMaker;
import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.network.ProtocolUtils; import com.velocitypowered.proxy.network.ProtocolUtils;
import com.velocitypowered.proxy.network.packet.clientbound.ClientboundBossBarPacket; import com.velocitypowered.proxy.network.packet.clientbound.ClientboundBossBarPacket;
@ -101,7 +102,7 @@ public class AdventureBossBarManager implements BossBar.Listener {
public void addBossBar(ConnectedPlayer player, BossBar bar) { public void addBossBar(ConnectedPlayer player, BossBar bar) {
BossBarHolder holder = this.getOrCreateHandler(bar); BossBarHolder holder = this.getOrCreateHandler(bar);
if (holder.subscribers.add(player)) { if (holder.subscribers.add(player)) {
player.getConnection().write(holder.createAddPacket(player.protocolVersion())); player.getConnection().write(holder.createAddPacket(player));
} }
} }
@ -124,14 +125,16 @@ public class AdventureBossBarManager implements BossBar.Listener {
if (holder == null) { if (holder == null) {
return; return;
} }
ClientboundBossBarPacket pre116Packet = holder.createTitleUpdate(
newName, ProtocolVersion.MINECRAFT_1_15_2);
ClientboundBossBarPacket rgbPacket = holder.createTitleUpdate(
newName, ProtocolVersion.MINECRAFT_1_16);
for (ConnectedPlayer player : holder.subscribers) { for (ConnectedPlayer player : holder.subscribers) {
Component translated = player.translateMessage(newName);
if (player.protocolVersion().gte(ProtocolVersion.MINECRAFT_1_16)) { if (player.protocolVersion().gte(ProtocolVersion.MINECRAFT_1_16)) {
ClientboundBossBarPacket rgbPacket = holder.createTitleUpdate(
translated, ProtocolVersion.MINECRAFT_1_16);
player.getConnection().write(rgbPacket); player.getConnection().write(rgbPacket);
} else { } else {
ClientboundBossBarPacket pre116Packet = holder.createTitleUpdate(
translated, ProtocolVersion.MINECRAFT_1_15_2);
player.getConnection().write(pre116Packet); player.getConnection().write(pre116Packet);
} }
} }
@ -207,11 +210,12 @@ public class AdventureBossBarManager implements BossBar.Listener {
return ClientboundBossBarPacket.createRemovePacket(this.id); return ClientboundBossBarPacket.createRemovePacket(this.id);
} }
ClientboundBossBarPacket createAddPacket(ProtocolVersion version) { ClientboundBossBarPacket createAddPacket(ConnectedPlayer player) {
ClientboundBossBarPacket packet = new ClientboundBossBarPacket(); ClientboundBossBarPacket packet = new ClientboundBossBarPacket();
packet.setUuid(this.id); packet.setUuid(this.id);
packet.setAction(ClientboundBossBarPacket.ADD); packet.setAction(ClientboundBossBarPacket.ADD);
packet.setName(ProtocolUtils.getJsonChatSerializer(version).serialize(bar.name())); packet.setName(ProtocolUtils.getJsonChatSerializer(player.protocolVersion())
.serialize(player.translateMessage(bar.name())));
packet.setColor(COLORS_TO_PROTOCOL.get(bar.color())); packet.setColor(COLORS_TO_PROTOCOL.get(bar.color()));
packet.setOverlay(OVERLAY_TO_PROTOCOL.get(bar.overlay())); packet.setOverlay(OVERLAY_TO_PROTOCOL.get(bar.overlay()));
packet.setPercent(bar.progress()); packet.setPercent(bar.progress());