PaperMC/paper-server/patches/sources/net/minecraft/network/PacketEncoder.java.patch
Nassim Jahnke 7caf863b52
Small diff cleanups from patch updating
And remove a dumb log change in PacketEncoder
2024-12-17 16:50:59 +01:00

50 lines
2.2 KiB
Diff

--- a/net/minecraft/network/PacketEncoder.java
+++ b/net/minecraft/network/PacketEncoder.java
@@ -17,11 +_,13 @@
this.protocolInfo = protocolInfo;
}
+ static final ThreadLocal<java.util.Locale> ADVENTURE_LOCALE = ThreadLocal.withInitial(() -> null); // Paper - adventure; set player's locale
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Packet<T> packet, ByteBuf byteBuf) throws Exception {
PacketType<? extends Packet<? super T>> packetType = packet.type();
try {
+ ADVENTURE_LOCALE.set(channelHandlerContext.channel().attr(io.papermc.paper.adventure.PaperAdventure.LOCALE_ATTRIBUTE).get()); // Paper - adventure; set player's locale
this.protocolInfo.codec().encode(byteBuf, packet);
int i = byteBuf.readableBytes();
if (LOGGER.isDebugEnabled()) {
@@ -39,7 +_,33 @@
throw var9;
} finally {
+ // Paper start - Handle large packets disconnecting client
+ int packetLength = byteBuf.readableBytes();
+ if (packetLength > MAX_PACKET_SIZE || (packetLength > MAX_FINAL_PACKET_SIZE && packet.hasLargePacketFallback())) {
+ throw new PacketTooLargeException(packet, packetLength);
+ }
+ // Paper end - Handle large packets disconnecting client
ProtocolSwapHandler.handleOutboundTerminalPacket(channelHandlerContext, packet);
}
}
+
+ // Paper start
+ // packet size is encoded into 3-byte varint
+ private static final int MAX_FINAL_PACKET_SIZE = (1 << 21) - 1;
+ // Vanilla Max size for the encoder (before compression)
+ private static final int MAX_PACKET_SIZE = 8388608;
+
+ public static class PacketTooLargeException extends RuntimeException {
+ private final Packet<?> packet;
+
+ PacketTooLargeException(Packet<?> packet, int packetLength) {
+ super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE);
+ this.packet = packet;
+ }
+
+ public Packet<?> getPacket() {
+ return this.packet;
+ }
+ }
+ // Paper end
}