From d2b6bfa5073eecbb71432eee97cbe92741fabb67 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <git@lynxplay.dev>
Date: Fri, 22 Sep 2023 16:35:40 +0200
Subject: [PATCH] Work here, work there, work for everyone

---
 ...irement-before-suggesting-root-nodes.patch |  0
 ...-ServerboundCommandSuggestionPacket-.patch |  0
 ...Fix-Bukkit-NamespacedKey-shenanigans.patch |  0
 ...entory-not-closing-on-entity-removal.patch |  0
 ...rnColor-on-tropical-fish-bucket-meta.patch |  0
 ...thread-worker-count-for-low-core-cou.patch |  0
 .../Remove-streams-for-villager-AI.patch      |  0
 ...ocity-compression-and-cipher-natives.patch | 38 +++++++++----------
 .../Optimise-nearby-player-lookups.patch      |  0
 .../Optimise-non-flush-packet-sending.patch   |  0
 10 files changed, 17 insertions(+), 21 deletions(-)
 rename patches/{unapplied => }/server/Check-requirement-before-suggesting-root-nodes.patch (100%)
 rename patches/{unapplied => }/server/Don-t-respond-to-ServerboundCommandSuggestionPacket-.patch (100%)
 rename patches/{unapplied => }/server/Fix-Bukkit-NamespacedKey-shenanigans.patch (100%)
 rename patches/{unapplied => }/server/Fix-merchant-inventory-not-closing-on-entity-removal.patch (100%)
 rename patches/{unapplied => }/server/Fix-setPatternColor-on-tropical-fish-bucket-meta.patch (100%)
 rename patches/{unapplied => }/server/Reduce-worldgen-thread-worker-count-for-low-core-cou.patch (100%)
 rename patches/{unapplied => }/server/Remove-streams-for-villager-AI.patch (100%)
 rename patches/{unapplied => }/server/Use-Velocity-compression-and-cipher-natives.patch (94%)
 rename patches/unapplied/{server => skipped}/Optimise-nearby-player-lookups.patch (100%)
 rename patches/unapplied/{server => skipped}/Optimise-non-flush-packet-sending.patch (100%)

diff --git a/patches/unapplied/server/Check-requirement-before-suggesting-root-nodes.patch b/patches/server/Check-requirement-before-suggesting-root-nodes.patch
similarity index 100%
rename from patches/unapplied/server/Check-requirement-before-suggesting-root-nodes.patch
rename to patches/server/Check-requirement-before-suggesting-root-nodes.patch
diff --git a/patches/unapplied/server/Don-t-respond-to-ServerboundCommandSuggestionPacket-.patch b/patches/server/Don-t-respond-to-ServerboundCommandSuggestionPacket-.patch
similarity index 100%
rename from patches/unapplied/server/Don-t-respond-to-ServerboundCommandSuggestionPacket-.patch
rename to patches/server/Don-t-respond-to-ServerboundCommandSuggestionPacket-.patch
diff --git a/patches/unapplied/server/Fix-Bukkit-NamespacedKey-shenanigans.patch b/patches/server/Fix-Bukkit-NamespacedKey-shenanigans.patch
similarity index 100%
rename from patches/unapplied/server/Fix-Bukkit-NamespacedKey-shenanigans.patch
rename to patches/server/Fix-Bukkit-NamespacedKey-shenanigans.patch
diff --git a/patches/unapplied/server/Fix-merchant-inventory-not-closing-on-entity-removal.patch b/patches/server/Fix-merchant-inventory-not-closing-on-entity-removal.patch
similarity index 100%
rename from patches/unapplied/server/Fix-merchant-inventory-not-closing-on-entity-removal.patch
rename to patches/server/Fix-merchant-inventory-not-closing-on-entity-removal.patch
diff --git a/patches/unapplied/server/Fix-setPatternColor-on-tropical-fish-bucket-meta.patch b/patches/server/Fix-setPatternColor-on-tropical-fish-bucket-meta.patch
similarity index 100%
rename from patches/unapplied/server/Fix-setPatternColor-on-tropical-fish-bucket-meta.patch
rename to patches/server/Fix-setPatternColor-on-tropical-fish-bucket-meta.patch
diff --git a/patches/unapplied/server/Reduce-worldgen-thread-worker-count-for-low-core-cou.patch b/patches/server/Reduce-worldgen-thread-worker-count-for-low-core-cou.patch
similarity index 100%
rename from patches/unapplied/server/Reduce-worldgen-thread-worker-count-for-low-core-cou.patch
rename to patches/server/Reduce-worldgen-thread-worker-count-for-low-core-cou.patch
diff --git a/patches/unapplied/server/Remove-streams-for-villager-AI.patch b/patches/server/Remove-streams-for-villager-AI.patch
similarity index 100%
rename from patches/unapplied/server/Remove-streams-for-villager-AI.patch
rename to patches/server/Remove-streams-for-villager-AI.patch
diff --git a/patches/unapplied/server/Use-Velocity-compression-and-cipher-natives.patch b/patches/server/Use-Velocity-compression-and-cipher-natives.patch
similarity index 94%
rename from patches/unapplied/server/Use-Velocity-compression-and-cipher-natives.patch
rename to patches/server/Use-Velocity-compression-and-cipher-natives.patch
index 0dc605c80f..8ffa261553 100644
--- a/patches/unapplied/server/Use-Velocity-compression-and-cipher-natives.patch
+++ b/patches/server/Use-Velocity-compression-and-cipher-natives.patch
@@ -9,8 +9,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/build.gradle.kts
 +++ b/build.gradle.kts
 @@ -0,0 +0,0 @@ dependencies {
-     runtimeOnly("org.xerial:sqlite-jdbc:3.42.0.0")
-     runtimeOnly("com.mysql:mysql-connector-j:8.0.33")
+     runtimeOnly("org.xerial:sqlite-jdbc:3.43.0.0")
+     runtimeOnly("com.mysql:mysql-connector-j:8.1.0")
      runtimeOnly("com.lmax:disruptor:3.4.4") // Paper
 +    // Paper start - Use Velocity cipher
 +    implementation("com.velocitypowered:velocity-native:3.1.2-SNAPSHOT") {
@@ -131,18 +131,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
                      }
                  }
  
-+                // Paper start
-+                if (this.inflater != null) {
-                 byte[] bs = new byte[friendlyByteBuf.readableBytes()];
-                 friendlyByteBuf.readBytes(bs);
-                 this.inflater.setInput(bs);
-@@ -0,0 +0,0 @@ public class CompressionDecoder extends ByteToMessageDecoder {
-                 this.inflater.inflate(cs);
-                 list.add(Unpooled.wrappedBuffer(cs));
++                if (inflater != null) { // Paper - use velocity compression - fallback to vanilla inflater
+                 this.setupInflaterInput(byteBuf);
+                 ByteBuf byteBuf2 = this.inflate(channelHandlerContext, i);
                  this.inflater.reset();
-+                    return;
-+                }
+                 list.add(byteBuf2);
++                return; // Paper - use velocity compression
++                } // Paper - use velocity compression
 +
++                // Paper start - use velocity compression
 +                int claimedUncompressedSize = i; // OBFHELPER
 +                ByteBuf compatibleIn = com.velocitypowered.natives.util.MoreByteBufUtils.ensureCompatible(channelHandlerContext.alloc(), this.compressor, byteBuf);
 +                ByteBuf uncompressed = com.velocitypowered.natives.util.MoreByteBufUtils.preferredBuffer(channelHandlerContext.alloc(), this.compressor, claimedUncompressedSize);
@@ -156,7 +153,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +                } finally {
 +                    compatibleIn.release();
 +                }
-+                // Paper end
++                // Paper end - use velocity compression
              }
          }
      }
@@ -170,9 +167,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +    // Paper end
 +
-     public void setThreshold(int compressionThreshold, boolean rejectsBadPackets) {
-         this.threshold = compressionThreshold;
-         this.validateDecompressed = rejectsBadPackets;
+     private void setupInflaterInput(ByteBuf buf) {
+         ByteBuffer byteBuffer;
+         if (buf.nioBufferCount() > 0) {
 diff --git a/src/main/java/net/minecraft/network/CompressionEncoder.java b/src/main/java/net/minecraft/network/CompressionEncoder.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/network/CompressionEncoder.java
@@ -208,16 +205,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 -    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) {
 +    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) throws Exception { // Paper
          int i = byteBuf.readableBytes();
-         FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(byteBuf2);
          if (i < this.threshold) {
-             friendlyByteBuf.writeVarInt(0);
-             friendlyByteBuf.writeBytes(byteBuf);
+             VarInt.write(byteBuf2, 0);
+             byteBuf2.writeBytes(byteBuf);
          } else {
 +            // Paper start
 +            if (this.deflater != null) {
              byte[] bs = new byte[i];
              byteBuf.readBytes(bs);
-             friendlyByteBuf.writeVarInt(bs.length);
+             VarInt.write(byteBuf2, bs.length);
 @@ -0,0 +0,0 @@ public class CompressionEncoder extends MessageToByteEncoder<ByteBuf> {
              }
  
@@ -339,7 +335,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +
              this.channels.add(((ServerBootstrap) ((ServerBootstrap) (new ServerBootstrap()).channel(oclass)).childHandler(new ChannelInitializer<Channel>() {
                  protected void initChannel(Channel channel) {
-                     try {
+                     Connection.setInitialProtocolAttributes(channel);
 diff --git a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
diff --git a/patches/unapplied/server/Optimise-nearby-player-lookups.patch b/patches/unapplied/skipped/Optimise-nearby-player-lookups.patch
similarity index 100%
rename from patches/unapplied/server/Optimise-nearby-player-lookups.patch
rename to patches/unapplied/skipped/Optimise-nearby-player-lookups.patch
diff --git a/patches/unapplied/server/Optimise-non-flush-packet-sending.patch b/patches/unapplied/skipped/Optimise-non-flush-packet-sending.patch
similarity index 100%
rename from patches/unapplied/server/Optimise-non-flush-packet-sending.patch
rename to patches/unapplied/skipped/Optimise-non-flush-packet-sending.patch