Off-by-default component translation

This commit is contained in:
Kieran Wallbanks 2021-03-15 14:19:43 +00:00
parent 96826e03d1
commit 8dc7328c76
3 changed files with 11 additions and 4 deletions

View File

@ -27,6 +27,11 @@ import java.util.function.Function;
* {@link TranslationRegistry} or your own implementation of {@link Translator}.
*/
public class SerializationManager {
/**
* If components should be automatically translated in outgoing packets.
*/
public static final boolean AUTOMATIC_COMPONENT_TRANSLATION = false;
protected static final Localizable NULL_LOCALIZABLE = () -> null;
private Function<Component, String> serializer = component -> GsonComponentSerializer.gson().serialize(component);

View File

@ -5,6 +5,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.socket.SocketChannel;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.SerializationManager;
import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.extras.mojangAuth.Decrypter;
import net.minestom.server.extras.mojangAuth.Encrypter;
@ -187,7 +188,7 @@ public class NettyPlayerConnection extends PlayerConnection {
} else if (message instanceof ServerPacket) {
final ServerPacket serverPacket = (ServerPacket) message;
if (!skipTranslating && getPlayer() != null && serverPacket instanceof ComponentHoldingServerPacket) {
if ((SerializationManager.AUTOMATIC_COMPONENT_TRANSLATION && !skipTranslating) && getPlayer() != null && serverPacket instanceof ComponentHoldingServerPacket) {
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component -> MinecraftServer.getSerializationManager().translate(component, getPlayer()));
}

View File

@ -3,6 +3,7 @@ package net.minestom.server.utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.SerializationManager;
import net.minestom.server.entity.Player;
import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.network.netty.packet.FramedPacket;
@ -49,7 +50,7 @@ public final class PacketUtils {
// work out if the packet needs to be sent individually due to server-side translating
boolean needsTranslating = false;
if (packet instanceof ComponentHoldingServerPacket) {
if (SerializationManager.AUTOMATIC_COMPONENT_TRANSLATION && packet instanceof ComponentHoldingServerPacket) {
needsTranslating = MinecraftServer.getSerializationManager().areAnyTranslatable(((ComponentHoldingServerPacket) packet).components());
}
@ -75,7 +76,7 @@ public final class PacketUtils {
final PlayerConnection playerConnection = player.getPlayerConnection();
if (playerConnection instanceof NettyPlayerConnection) {
final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection;
nettyPlayerConnection.write(framedPacket);
nettyPlayerConnection.write(framedPacket, true);
} else {
playerConnection.sendPacket(packet);
}
@ -93,7 +94,7 @@ public final class PacketUtils {
continue;
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(packet);
playerConnection.sendPacket(packet, false);
}
}
}