From 0c9ace8f7819857b1fdc9e93ee89670349327bc0 Mon Sep 17 00:00:00 2001
From: Drex <nicknamedrex@gmail.com>
Date: Thu, 9 Feb 2023 18:38:00 +0100
Subject: [PATCH] Add AntiXray layered obfuscation mode (#8799)

---
 patches/server/0004-Paper-config-files.patch | 10 ++---
 patches/server/0344-Anti-Xray.patch          | 40 ++++++++++++++++++--
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/patches/server/0004-Paper-config-files.patch b/patches/server/0004-Paper-config-files.patch
index 488d7a0304..8fc5609219 100644
--- a/patches/server/0004-Paper-config-files.patch
+++ b/patches/server/0004-Paper-config-files.patch
@@ -14,7 +14,7 @@ public org.spigotmc.SpigotWorldConfig getString(Ljava/lang/String;Ljava/lang/Str
 public net.minecraft.world.level.NaturalSpawner SPAWNING_CATEGORIES
 
 diff --git a/build.gradle.kts b/build.gradle.kts
-index a2d2c817cdc1798cd30b3a852b3a495bcbe1b91b..ebf3d64afbb5679b97a7565a2f8e5490e4a38275 100644
+index 064a0c5b2a4b379ceec84652ff1e004d57419115..fc8a097b000e892630ddef00c350454a5f785281 100644
 --- a/build.gradle.kts
 +++ b/build.gradle.kts
 @@ -12,6 +12,7 @@ dependencies {
@@ -1448,7 +1448,7 @@ index 0000000000000000000000000000000000000000..1bb16fc7598cd53e822d84b69d6a9727
 +}
 diff --git a/src/main/java/io/papermc/paper/configuration/WorldConfiguration.java b/src/main/java/io/papermc/paper/configuration/WorldConfiguration.java
 new file mode 100644
-index 0000000000000000000000000000000000000000..2e9590c73d867c3ebb42f695df4c796a0f477452
+index 0000000000000000000000000000000000000000..4532f3a0d74feae0a1249b53e1bfbc18a8808b32
 --- /dev/null
 +++ b/src/main/java/io/papermc/paper/configuration/WorldConfiguration.java
 @@ -0,0 +1,475 @@
@@ -4002,7 +4002,7 @@ index 0000000000000000000000000000000000000000..fdc906b106a5c6fff2675d5399650f5b
 +}
 diff --git a/src/main/java/io/papermc/paper/configuration/type/EngineMode.java b/src/main/java/io/papermc/paper/configuration/type/EngineMode.java
 new file mode 100644
-index 0000000000000000000000000000000000000000..99e90636051fa0c770ee2eafb7f0d29c8195f9ae
+index 0000000000000000000000000000000000000000..7f8b685762f59049fde88e8d1bc10e1504916010
 --- /dev/null
 +++ b/src/main/java/io/papermc/paper/configuration/type/EngineMode.java
 @@ -0,0 +1,37 @@
@@ -4013,7 +4013,7 @@ index 0000000000000000000000000000000000000000..99e90636051fa0c770ee2eafb7f0d29c
 +
 +public enum EngineMode {
 +
-+    HIDE(1, "hide ores"), OBFUSCATE(2, "obfuscate");
++    HIDE(1, "hide ores"), OBFUSCATE(2, "obfuscate"), OBFUSCATE_LAYER(3, "obfuscate layer");
 +
 +    public static final ScalarSerializer<EngineMode> SERIALIZER = new EngineModeSerializer();
 +
@@ -4576,7 +4576,7 @@ index 9afc0881f6891ef2696b6f2b37c0826f3beb0666..8873bf84c71c48297a360df9c99e511f
              world.serverLevelData.setDifficulty(config.difficulty);
              world.setSpawnSettings(config.spawnMonsters, config.spawnAnimals);
 diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
-index 16c38195c3e4f5550122df0d65fcce8ca3a83822..1d4f6e7fcfaaee40b06f74d250d04f7a36b6458b 100644
+index 8842ac222e0dea1afb7ba4584512147bb53ccb56..7e9381fcdaafb15b22d9f79592422b165b3e2523 100644
 --- a/src/main/java/org/bukkit/craftbukkit/Main.java
 +++ b/src/main/java/org/bukkit/craftbukkit/Main.java
 @@ -131,6 +131,19 @@ public class Main {
diff --git a/patches/server/0344-Anti-Xray.patch b/patches/server/0344-Anti-Xray.patch
index cd81e41f75..eb92ff131b 100644
--- a/patches/server/0344-Anti-Xray.patch
+++ b/patches/server/0344-Anti-Xray.patch
@@ -199,10 +199,10 @@ index 0000000000000000000000000000000000000000..bd86dc2ad2f87969da4add06de2a629f
 +}
 diff --git a/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockControllerAntiXray.java b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockControllerAntiXray.java
 new file mode 100644
-index 0000000000000000000000000000000000000000..4f3670b2bdb8b1b252e9f074a6af56a018a8c465
+index 0000000000000000000000000000000000000000..cab91880a08c6fdc545804911d295e0f24f4d983
 --- /dev/null
 +++ b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockControllerAntiXray.java
-@@ -0,0 +1,636 @@
+@@ -0,0 +1,670 @@
 +package com.destroystokyo.paper.antixray;
 +
 +import io.papermc.paper.configuration.WorldConfiguration;
@@ -428,7 +428,31 @@ index 0000000000000000000000000000000000000000..4f3670b2bdb8b1b252e9f074a6af56a0
 +        bitStorageWriter.setBuffer(chunkPacketInfoAntiXray.getBuffer());
 +        int numberOfBlocks = presetBlockStateBits.length;
 +        // Keep the lambda expressions as simple as possible. They are used very frequently.
-+        IntSupplier random = numberOfBlocks == 1 ? (() -> 0) : new IntSupplier() {
++        LayeredIntSupplier random = numberOfBlocks == 1 ? (() -> 0) : engineMode == EngineMode.OBFUSCATE_LAYER ? new LayeredIntSupplier() {
++            // engine-mode: 3
++            private int state;
++            private int next;
++
++            {
++                while ((state = ThreadLocalRandom.current().nextInt()) == 0) ;
++            }
++
++            @Override
++            public void nextLayer() {
++                // https://en.wikipedia.org/wiki/Xorshift
++                state ^= state << 13;
++                state ^= state >>> 17;
++                state ^= state << 5;
++                // https://www.pcg-random.org/posts/bounded-rands.html
++                next = (int) ((Integer.toUnsignedLong(state) * numberOfBlocks) >>> 32);
++            }
++
++            @Override
++            public int getAsInt() {
++                return next;
++            }
++        } : new LayeredIntSupplier() {
++            // engine-mode: 2
 +            private int state;
 +
 +            {
@@ -509,6 +533,7 @@ index 0000000000000000000000000000000000000000..4f3670b2bdb8b1b252e9f074a6af56a0
 +                    current = next;
 +                    next = nextNext;
 +                    nextNext = temp;
++                    random.nextLayer();
 +                    obfuscateLayer(y, bitStorageReader, bitStorageWriter, solidTemp, obfuscateTemp, presetBlockStateBitsTemp, current, next, nextNext, nearbyChunkSections, random);
 +                }
 +
@@ -534,6 +559,7 @@ index 0000000000000000000000000000000000000000..4f3670b2bdb8b1b252e9f074a6af56a0
 +                        // There is nothing to read anymore
 +                        bitStorageReader.setBits(0);
 +                        solid[0] = true;
++                        random.nextLayer();
 +                        obfuscateLayer(15, bitStorageReader, bitStorageWriter, solid, obfuscateTemp, presetBlockStateBitsTemp, current, next, nextNext, nearbyChunkSections, random);
 +                    }
 +                } else {
@@ -546,6 +572,7 @@ index 0000000000000000000000000000000000000000..4f3670b2bdb8b1b252e9f074a6af56a0
 +                    current = next;
 +                    next = nextNext;
 +                    nextNext = temp;
++                    random.nextLayer();
 +                    obfuscateLayer(15, bitStorageReader, bitStorageWriter, solidTemp, obfuscateTemp, presetBlockStateBitsTemp, current, next, nextNext, nearbyChunkSections, random);
 +                }
 +
@@ -838,6 +865,13 @@ index 0000000000000000000000000000000000000000..4f3670b2bdb8b1b252e9f074a6af56a0
 +            ((ServerLevel) level).getChunkSource().blockChanged(blockPos);
 +        }
 +    }
++
++    @FunctionalInterface
++    private interface LayeredIntSupplier extends IntSupplier {
++        default void nextLayer() {
++
++        }
++    }
 +}
 diff --git a/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfo.java b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfo.java
 new file mode 100644