From 8dc7328c76d3121e158ba4e7284547a329baeb74 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Mon, 15 Mar 2021 14:19:43 +0000 Subject: [PATCH] Off-by-default component translation --- .../minestom/server/adventure/SerializationManager.java | 5 +++++ .../server/network/player/NettyPlayerConnection.java | 3 ++- src/main/java/net/minestom/server/utils/PacketUtils.java | 7 ++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/adventure/SerializationManager.java b/src/main/java/net/minestom/server/adventure/SerializationManager.java index ad2f71f6f..939afdccc 100644 --- a/src/main/java/net/minestom/server/adventure/SerializationManager.java +++ b/src/main/java/net/minestom/server/adventure/SerializationManager.java @@ -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 serializer = component -> GsonComponentSerializer.gson().serialize(component); diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 91042081d..3cfe40052 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -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())); } diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 0108ecbd7..5264fad8f 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -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); } } }