aboutsummaryrefslogtreecommitdiffhomepage
path: root/paper-server/patches/sources/net/minecraft/network/PacketEncoder.java.patch
blob: ce1cafa04d0f6e902cdbf8092e4dd17924380def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
--- a/net/minecraft/network/PacketEncoder.java
+++ b/net/minecraft/network/PacketEncoder.java
@@ -17,10 +17,12 @@
         this.protocolInfo = state;
     }
 
+    static final ThreadLocal<java.util.Locale> ADVENTURE_LOCALE = ThreadLocal.withInitial(() -> null); // Paper - adventure; set player's locale
     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()) {
@@ -31,14 +33,40 @@
 
             JvmProfiler.INSTANCE.onPacketSent(this.protocolInfo.id(), packetType, channelHandlerContext.channel().remoteAddress(), i);
         } catch (Throwable var9) {
-            LOGGER.error("Error sending packet {}", packetType, var9);
+            LOGGER.error("Error sending packet {} (skippable? {})", packetType, packet.isSkippable(), var9);
             if (packet.isSkippable()) {
                 throw new SkipPacketException(var9);
             }
 
             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
 }