-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
0397-Anti-Xray.patch
1714 lines (1653 loc) · 81.9 KB
/
0397-Anti-Xray.patch
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From a226c1b147367c8bd55c5f0e9327ac1ee156837c Mon Sep 17 00:00:00 2001
From: stonar96 <[email protected]>
Date: Mon, 20 Aug 2018 03:03:58 +0200
Subject: [PATCH] Anti-Xray
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index 55b78f54c..cfc4156bc 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -1,7 +1,11 @@
package com.destroystokyo.paper;
+import java.util.Arrays;
import java.util.List;
+import com.destroystokyo.paper.antixray.ChunkPacketBlockControllerAntiXray.ChunkEdgeMode;
+import com.destroystokyo.paper.antixray.ChunkPacketBlockControllerAntiXray.EngineMode;
+import net.minecraft.server.MinecraftServer;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.spigotmc.SpigotWorldConfig;
@@ -509,4 +513,43 @@ public class PaperWorldConfig {
private void maxAutoSaveChunksPerTick() {
maxAutoSaveChunksPerTick = getInt("max-auto-save-chunks-per-tick", 24);
}
+
+ public boolean antiXray;
+ public boolean asynchronous;
+ public EngineMode engineMode;
+ public ChunkEdgeMode chunkEdgeMode;
+ public int maxChunkSectionIndex;
+ public int updateRadius;
+ public List<String> hiddenBlocks;
+ public List<String> replacementBlocks;
+ private void antiXray() {
+ antiXray = getBoolean("anti-xray.enabled", false);
+ asynchronous = true;
+ engineMode = EngineMode.getById(getInt("anti-xray.engine-mode", EngineMode.HIDE.getId()));
+ engineMode = engineMode == null ? EngineMode.HIDE : engineMode;
+ chunkEdgeMode = ChunkEdgeMode.getById(getInt("anti-xray.chunk-edge-mode", ChunkEdgeMode.WAIT.getId()));
+ chunkEdgeMode = chunkEdgeMode == null ? ChunkEdgeMode.DEFAULT : chunkEdgeMode;
+
+ if (chunkEdgeMode != ChunkEdgeMode.WAIT) {
+ log("Migrating anti-xray chunk edge mode to " + ChunkEdgeMode.WAIT + " (" + ChunkEdgeMode.WAIT.getId() + ")");
+ chunkEdgeMode = ChunkEdgeMode.WAIT;
+ set("anti-xray.chunk-edge-mode", ChunkEdgeMode.WAIT.getId());
+ }
+
+ maxChunkSectionIndex = getInt("anti-xray.max-chunk-section-index", 3);
+ maxChunkSectionIndex = maxChunkSectionIndex > 15 ? 15 : maxChunkSectionIndex;
+ updateRadius = getInt("anti-xray.update-radius", 2);
+ hiddenBlocks = getList("anti-xray.hidden-blocks", Arrays.asList("gold_ore", "iron_ore", "coal_ore", "lapis_ore", "mossy_cobblestone", "obsidian", "chest", "diamond_ore", "redstone_ore", "clay", "emerald_ore", "ender_chest"));
+ replacementBlocks = getList("anti-xray.replacement-blocks", Arrays.asList("stone", "oak_planks"));
+ if (PaperConfig.version < 19) {
+ hiddenBlocks.remove("lit_redstone_ore");
+ int index = replacementBlocks.indexOf("planks");
+ if (index != -1) {
+ replacementBlocks.set(index, "oak_planks");
+ }
+ set("anti-xray.hidden-blocks", hiddenBlocks);
+ set("anti-xray.replacement-blocks", replacementBlocks);
+ }
+ log("Anti-Xray: " + (antiXray ? "enabled" : "disabled") + " / Engine Mode: " + engineMode.getDescription() + " / Chunk Edge Mode: " + chunkEdgeMode.getDescription() + " / Up to " + ((maxChunkSectionIndex + 1) * 16) + " blocks / Update Radius: " + updateRadius);
+ }
}
diff --git a/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockController.java b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockController.java
new file mode 100644
index 000000000..f7e376ce6
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockController.java
@@ -0,0 +1,46 @@
+package com.destroystokyo.paper.antixray;
+
+import net.minecraft.server.BlockPosition;
+import net.minecraft.server.Chunk;
+import net.minecraft.server.ChunkSection;
+import net.minecraft.server.EnumDirection;
+import net.minecraft.server.IBlockData;
+import net.minecraft.server.IChunkAccess;
+import net.minecraft.server.IWorldReader;
+import net.minecraft.server.PacketPlayOutMapChunk;
+import net.minecraft.server.PlayerInteractManager;
+import net.minecraft.server.World;
+
+public class ChunkPacketBlockController {
+
+ public static final ChunkPacketBlockController NO_OPERATION_INSTANCE = new ChunkPacketBlockController();
+
+ protected ChunkPacketBlockController() {
+
+ }
+
+ public IBlockData[] getPredefinedBlockData(IWorldReader world, IChunkAccess chunk, ChunkSection chunkSection, boolean initializeBlocks) {
+ return null;
+ }
+
+ public boolean onChunkPacketCreate(Chunk chunk, int chunkSectionSelector, boolean force) {
+ return true;
+ }
+
+ public ChunkPacketInfo<IBlockData> getChunkPacketInfo(PacketPlayOutMapChunk packetPlayOutMapChunk, Chunk chunk,
+ int chunkSectionSelector, boolean forceLoad) {
+ return null;
+ }
+
+ public void modifyBlocks(PacketPlayOutMapChunk packetPlayOutMapChunk, ChunkPacketInfo<IBlockData> chunkPacketInfo, boolean loadChunks, Integer ticketHold) {
+ packetPlayOutMapChunk.setReady(true);
+ }
+
+ public void onBlockChange(World world, BlockPosition blockPosition, IBlockData newBlockData, IBlockData oldBlockData, int flag) {
+
+ }
+
+ public void onPlayerLeftClickBlock(PlayerInteractManager playerInteractManager, BlockPosition blockPosition, EnumDirection enumDirection) {
+
+ }
+}
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 000000000..23626bef3
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketBlockControllerAntiXray.java
@@ -0,0 +1,782 @@
+package com.destroystokyo.paper.antixray;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Supplier;
+
+import net.minecraft.server.*;
+import org.bukkit.Bukkit;
+import org.bukkit.World.Environment;
+
+import com.destroystokyo.paper.PaperWorldConfig;
+
+public class ChunkPacketBlockControllerAntiXray extends ChunkPacketBlockController {
+
+ private static ExecutorService executorServiceInstance = null;
+ private final ExecutorService executorService;
+ private final boolean asynchronous;
+ private final EngineMode engineMode;
+ private final ChunkEdgeMode chunkEdgeMode;
+ private final int maxChunkSectionIndex;
+ private final int updateRadius;
+ private final IBlockData[] predefinedBlockData;
+ private final IBlockData[] predefinedBlockDataStone;
+ private final IBlockData[] predefinedBlockDataNetherrack;
+ private final IBlockData[] predefinedBlockDataEndStone;
+ private final int[] predefinedBlockDataBitsGlobal;
+ private final int[] predefinedBlockDataBitsStoneGlobal;
+ private final int[] predefinedBlockDataBitsNetherrackGlobal;
+ private final int[] predefinedBlockDataBitsEndStoneGlobal;
+ private final boolean[] solidGlobal = new boolean[Block.REGISTRY_ID.size()];
+ private final boolean[] obfuscateGlobal = new boolean[Block.REGISTRY_ID.size()];
+ private final ChunkSection[] emptyNearbyChunkSections = {Chunk.EMPTY_CHUNK_SECTION, Chunk.EMPTY_CHUNK_SECTION, Chunk.EMPTY_CHUNK_SECTION, Chunk.EMPTY_CHUNK_SECTION};
+ private final int maxBlockYUpdatePosition;
+
+ public ChunkPacketBlockControllerAntiXray(PaperWorldConfig paperWorldConfig) {
+ asynchronous = paperWorldConfig.asynchronous;
+ engineMode = paperWorldConfig.engineMode;
+ chunkEdgeMode = paperWorldConfig.chunkEdgeMode;
+ maxChunkSectionIndex = paperWorldConfig.maxChunkSectionIndex;
+ updateRadius = paperWorldConfig.updateRadius;
+
+ if (asynchronous) {
+ executorService = getExecutorServiceInstance();
+ } else {
+ executorService = null;
+ }
+
+ List<String> toObfuscate;
+
+ if (engineMode == EngineMode.HIDE) {
+ toObfuscate = paperWorldConfig.hiddenBlocks;
+ predefinedBlockData = null;
+ predefinedBlockDataStone = new IBlockData[] {Blocks.STONE.getBlockData()};
+ predefinedBlockDataNetherrack = new IBlockData[] {Blocks.NETHERRACK.getBlockData()};
+ predefinedBlockDataEndStone = new IBlockData[] {Blocks.END_STONE.getBlockData()};
+ predefinedBlockDataBitsGlobal = null;
+ predefinedBlockDataBitsStoneGlobal = new int[] {ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(Blocks.STONE.getBlockData())};
+ predefinedBlockDataBitsNetherrackGlobal = new int[] {ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(Blocks.NETHERRACK.getBlockData())};
+ predefinedBlockDataBitsEndStoneGlobal = new int[] {ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(Blocks.END_STONE.getBlockData())};
+ } else {
+ toObfuscate = new ArrayList<>(paperWorldConfig.replacementBlocks);
+ Set<IBlockData> predefinedBlockDataSet = new HashSet<IBlockData>();
+
+ for (String id : paperWorldConfig.hiddenBlocks) {
+ Block block = IRegistry.BLOCK.getOptional(new MinecraftKey(id)).orElse(null);
+
+ if (block != null && !block.isTileEntity()) {
+ toObfuscate.add(id);
+ predefinedBlockDataSet.add(block.getBlockData());
+ }
+ }
+
+ predefinedBlockData = predefinedBlockDataSet.size() == 0 ? new IBlockData[] {Blocks.DIAMOND_ORE.getBlockData()} : predefinedBlockDataSet.toArray(new IBlockData[0]);
+ predefinedBlockDataStone = null;
+ predefinedBlockDataNetherrack = null;
+ predefinedBlockDataEndStone = null;
+ predefinedBlockDataBitsGlobal = new int[predefinedBlockData.length];
+
+ for (int i = 0; i < predefinedBlockData.length; i++) {
+ predefinedBlockDataBitsGlobal[i] = ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(predefinedBlockData[i]);
+ }
+
+ predefinedBlockDataBitsStoneGlobal = null;
+ predefinedBlockDataBitsNetherrackGlobal = null;
+ predefinedBlockDataBitsEndStoneGlobal = null;
+ }
+
+ for (String id : toObfuscate) {
+ Block block = IRegistry.BLOCK.getOptional(new MinecraftKey(id)).orElse(null);
+
+ if (block != null) {
+ obfuscateGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(block.getBlockData())] = true;
+ }
+ }
+
+ ChunkEmpty emptyChunk = new ChunkEmpty(null, new ChunkCoordIntPair(0, 0));
+ BlockPosition zeroPos = new BlockPosition(0, 0, 0);
+
+ for (int i = 0; i < solidGlobal.length; i++) {
+ IBlockData blockData = ChunkSection.GLOBAL_PALETTE.getObject(i);
+
+ if (blockData != null) {
+ solidGlobal[i] = blockData.getBlock().isOccluding(blockData, emptyChunk, zeroPos)
+ && blockData.getBlock() != Blocks.SPAWNER && blockData.getBlock() != Blocks.BARRIER && blockData.getBlock() != Blocks.SHULKER_BOX;
+ // shulker box checks TE.
+ }
+ }
+
+ this.maxBlockYUpdatePosition = (maxChunkSectionIndex + 1) * 16 + updateRadius - 1;
+ }
+
+ private static ExecutorService getExecutorServiceInstance() {
+ if (executorServiceInstance == null) {
+ executorServiceInstance = Executors.newSingleThreadExecutor();
+ }
+
+ return executorServiceInstance;
+ }
+
+ @Override
+ public IBlockData[] getPredefinedBlockData(IWorldReader world, IChunkAccess chunk, ChunkSection chunkSection, boolean initializeBlocks) {
+ //Return the block data which should be added to the data palettes so that they can be used for the obfuscation
+ if (chunkSection.getYPosition() >> 4 <= maxChunkSectionIndex) {
+ switch (engineMode) {
+ case HIDE:
+ if (world instanceof GeneratorAccess) {
+ switch (((GeneratorAccess) world).getMinecraftWorld().getWorld().getEnvironment()) {
+ case NETHER:
+ return predefinedBlockDataNetherrack;
+ case THE_END:
+ return predefinedBlockDataEndStone;
+ default:
+ return predefinedBlockDataStone;
+ }
+ }
+
+ return null;
+ default:
+ return predefinedBlockData;
+ }
+ }
+
+ return null;
+ }
+
+ private final AtomicInteger xrayRequests = new AtomicInteger();
+
+ private Integer addXrayTickets(final int x, final int z, final ChunkProviderServer chunkProvider) {
+ final Integer hold = Integer.valueOf(this.xrayRequests.getAndIncrement());
+
+ // Add at ticket level 33, which is just enough to keep chunks loaded
+ chunkProvider.addTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x, z), 0, hold);
+ chunkProvider.addTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x - 1, z), 0, hold);
+ chunkProvider.addTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x + 1, z), 0, hold);
+ chunkProvider.addTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x, z - 1), 0, hold);
+ chunkProvider.addTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x, z + 1), 0, hold);
+
+ return hold;
+ }
+
+ private void removeXrayTickets(final int x, final int z, final ChunkProviderServer chunkProvider, final Integer hold) {
+ // Remove at ticket level 33 (same one we added as), which is just enough to keep chunks loaded
+ chunkProvider.removeTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x, z), 0, hold);
+ chunkProvider.removeTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x - 1, z), 0, hold);
+ chunkProvider.removeTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x + 1, z), 0, hold);
+ chunkProvider.removeTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x, z - 1), 0, hold);
+ chunkProvider.removeTicket(TicketType.ANTIXRAY, new ChunkCoordIntPair(x, z + 1), 0, hold);
+ }
+
+ private void loadNeighbours(Chunk chunk) {
+ int locX = chunk.getPos().x;
+ int locZ = chunk.getPos().z;
+ chunk.world.getChunkAt(locX - 1, locZ);
+ chunk.world.getChunkAt(locX + 1, locZ);
+ chunk.world.getChunkAt(locX, locZ - 1);
+ chunk.world.getChunkAt(locX, locZ + 1);
+ }
+
+ @Override
+ public boolean onChunkPacketCreate(Chunk chunk, int chunkSectionSelector, boolean force) {
+ int locX = chunk.getPos().x;
+ int locZ = chunk.getPos().z;
+ WorldServer world = (WorldServer)chunk.world;
+ ChunkProviderServer chunkProvider = world.getChunkProvider();
+
+ //Load nearby chunks if necessary
+ if (force || chunkEdgeMode == ChunkEdgeMode.LOAD) { // TODO temporary
+ // if forced, load NOW;
+ this.loadNeighbours(chunk);
+ } else if (chunkEdgeMode == ChunkEdgeMode.WAIT) {
+ if (chunkProvider.getChunkAtIfCachedImmediately(locX - 1, locZ) == null ||
+ chunkProvider.getChunkAtIfCachedImmediately(locX + 1, locZ) == null ||
+ chunkProvider.getChunkAtIfCachedImmediately(locX, locZ - 1) == null ||
+ chunkProvider.getChunkAtIfCachedImmediately(locX, locZ + 1) == null) {
+ //Don't create the chunk packet now, wait until nearby chunks are loaded and create it later
+ return false;
+ }
+ } else if (false && chunkEdgeMode == ChunkEdgeMode.LOAD) {
+ // TODO Note: These should be asynchronous loads; however we have no such thing in 1.14.
+ boolean missingChunk = false;
+ //noinspection ConstantConditions
+ /*
+ missingChunk |= ((WorldServer)chunk.world).getChunkProvider().getChunkAt(chunk.locX - 1, chunk.locZ, true, true, c -> {}) == null;
+ missingChunk |= ((WorldServer)chunk.world).getChunkProvider().getChunkAt(chunk.locX + 1, chunk.locZ, true, true, c -> {}) == null;
+ missingChunk |= ((WorldServer)chunk.world).getChunkProvider().getChunkAt(chunk.locX, chunk.locZ - 1, true, true, c -> {}) == null;
+ missingChunk |= ((WorldServer)chunk.world).getChunkProvider().getChunkAt(chunk.locX, chunk.locZ + 1, true, true, c -> {}) == null;
+ */
+ if (missingChunk) {
+ return false;
+ }
+ }
+
+ //Create the chunk packet now
+ return true;
+ }
+
+ @Override
+ public ChunkPacketInfoAntiXray getChunkPacketInfo(PacketPlayOutMapChunk packetPlayOutMapChunk, Chunk chunk,
+ int chunkSectionSelector, boolean forceLoad) {
+ // Return a new instance to collect data and objects in the right state while creating the chunk packet for thread safe access later
+ // Note: As of 1.14 this has to be moved later due to the chunk system.
+
+ ChunkPacketInfoAntiXray chunkPacketInfoAntiXray = new ChunkPacketInfoAntiXray(packetPlayOutMapChunk, chunk, chunkSectionSelector, this);
+ return chunkPacketInfoAntiXray;
+ }
+
+ @Override
+ public void modifyBlocks(PacketPlayOutMapChunk packetPlayOutMapChunk, ChunkPacketInfo<IBlockData> chunkPacketInfo, boolean loadChunks, Integer hold) {
+ if (!Bukkit.isPrimaryThread()) {
+ // plugins?
+ final Integer finalHold = hold;
+ MinecraftServer.getServer().scheduleOnMain(() -> {
+ this.modifyBlocks(packetPlayOutMapChunk, chunkPacketInfo, loadChunks, finalHold);
+ });
+ return;
+ }
+ Chunk chunk = chunkPacketInfo.getChunk();
+ int locX = chunk.getPos().x;
+ int locZ = chunk.getPos().z;
+ WorldServer world = (WorldServer)chunk.world;
+
+ Chunk[] chunks = new Chunk[] {
+ (Chunk)world.getChunkIfLoadedImmediately(locX - 1, locZ),
+ (Chunk)world.getChunkIfLoadedImmediately(locX + 1, locZ),
+ (Chunk)world.getChunkIfLoadedImmediately(locX, locZ - 1),
+ (Chunk)world.getChunkIfLoadedImmediately(locX, locZ + 1)
+ };
+
+ if (loadChunks) {
+ // Note: This ugly hack is to get us out of the general chunk load/unload queue to prevent deadlock
+
+ if (chunks[0] == null || chunks[1] == null || chunks[2] == null || chunks[3] == null) {
+ // we need to load
+ MinecraftServer.getServer().scheduleOnMain(() -> {
+ Integer ticketHold = this.addXrayTickets(locX, locZ, world.getChunkProvider());
+ this.loadNeighbours(chunk);
+ this.modifyBlocks(packetPlayOutMapChunk, chunkPacketInfo, false, ticketHold);
+ });
+ return;
+ }
+
+ hold = this.addXrayTickets(locX, locZ, world.getChunkProvider());
+ // fall through to normal behavior, our chunks are now loaded & have a ticket
+ }
+
+ ((ChunkPacketInfoAntiXray)chunkPacketInfo).setNearbyChunks(chunks);
+ ((ChunkPacketInfoAntiXray)chunkPacketInfo).ticketHold = hold;
+
+ if (asynchronous) {
+ executorService.submit((ChunkPacketInfoAntiXray) chunkPacketInfo);
+ } else {
+ obfuscate((ChunkPacketInfoAntiXray) chunkPacketInfo);
+ }
+ }
+
+ //Actually these fields should be variables inside the obfuscate method but in sync mode or with SingleThreadExecutor in async mode it's okay
+ private int[] predefinedBlockDataBits;
+ private final boolean[] solid = new boolean[Block.REGISTRY_ID.size()];
+ private final boolean[] obfuscate = new boolean[Block.REGISTRY_ID.size()];
+ //These boolean arrays represent chunk layers, true means don't obfuscate, false means obfuscate
+ private boolean[][] current = new boolean[16][16];
+ private boolean[][] next = new boolean[16][16];
+ private boolean[][] nextNext = new boolean[16][16];
+ private final DataBitsReader dataBitsReader = new DataBitsReader();
+ private final DataBitsWriter dataBitsWriter = new DataBitsWriter();
+ private final ChunkSection[] nearbyChunkSections = new ChunkSection[4];
+
+ public void obfuscate(ChunkPacketInfoAntiXray chunkPacketInfoAntiXray) {
+ try {
+ boolean[] solidTemp = null;
+ boolean[] obfuscateTemp = null;
+ dataBitsReader.setDataBits(chunkPacketInfoAntiXray.getData());
+ dataBitsWriter.setDataBits(chunkPacketInfoAntiXray.getData());
+ int counter = 0;
+
+ for (int chunkSectionIndex = 0; chunkSectionIndex <= maxChunkSectionIndex; chunkSectionIndex++) {
+ if (chunkPacketInfoAntiXray.isWritten(chunkSectionIndex) && chunkPacketInfoAntiXray.getPredefinedObjects(chunkSectionIndex) != null) {
+ int[] predefinedBlockDataBitsTemp;
+
+ if (chunkPacketInfoAntiXray.getDataPalette(chunkSectionIndex) == ChunkSection.GLOBAL_PALETTE) {
+ predefinedBlockDataBitsTemp = engineMode == EngineMode.HIDE ? chunkPacketInfoAntiXray.getChunk().world.getWorld().getEnvironment() == Environment.NETHER ? predefinedBlockDataBitsNetherrackGlobal : chunkPacketInfoAntiXray.getChunk().world.getWorld().getEnvironment() == Environment.THE_END ? predefinedBlockDataBitsEndStoneGlobal : predefinedBlockDataBitsStoneGlobal : predefinedBlockDataBitsGlobal;
+ } else {
+ predefinedBlockDataBitsTemp = predefinedBlockDataBits == null ? predefinedBlockDataBits = engineMode == EngineMode.HIDE ? new int[1] : new int[predefinedBlockData.length] : predefinedBlockDataBits;
+
+ for (int i = 0; i < predefinedBlockDataBitsTemp.length; i++) {
+ predefinedBlockDataBitsTemp[i] = chunkPacketInfoAntiXray.getDataPalette(chunkSectionIndex).getOrCreateIdFor(chunkPacketInfoAntiXray.getPredefinedObjects(chunkSectionIndex)[i]);
+ }
+ }
+
+ dataBitsWriter.setIndex(chunkPacketInfoAntiXray.getOrCreateIdForIndex(chunkSectionIndex));
+
+ //Check if the chunk section below was not obfuscated
+ if (chunkSectionIndex == 0 || !chunkPacketInfoAntiXray.isWritten(chunkSectionIndex - 1) || chunkPacketInfoAntiXray.getPredefinedObjects(chunkSectionIndex - 1) == null) {
+ //If so, initialize some stuff
+ dataBitsReader.setBitsPerObject(chunkPacketInfoAntiXray.getBitsPerObject(chunkSectionIndex));
+ dataBitsReader.setIndex(chunkPacketInfoAntiXray.getOrCreateIdForIndex(chunkSectionIndex));
+ solidTemp = readDataPalette(chunkPacketInfoAntiXray.getDataPalette(chunkSectionIndex), solid, solidGlobal);
+ obfuscateTemp = readDataPalette(chunkPacketInfoAntiXray.getDataPalette(chunkSectionIndex), obfuscate, obfuscateGlobal);
+ //Read the blocks of the upper layer of the chunk section below if it exists
+ ChunkSection belowChunkSection = null;
+ boolean skipFirstLayer = chunkSectionIndex == 0 || (belowChunkSection = chunkPacketInfoAntiXray.getChunk().getSections()[chunkSectionIndex - 1]) == Chunk.EMPTY_CHUNK_SECTION;
+
+ for (int z = 0; z < 16; z++) {
+ for (int x = 0; x < 16; x++) {
+ current[z][x] = true;
+ next[z][x] = skipFirstLayer || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(belowChunkSection.getType(x, 15, z))];
+ }
+ }
+
+ //Abuse the obfuscateLayer method to read the blocks of the first layer of the current chunk section
+ dataBitsWriter.setBitsPerObject(0);
+ obfuscateLayer(-1, dataBitsReader, dataBitsWriter, solidTemp, obfuscateTemp, predefinedBlockDataBitsTemp, current, next, nextNext, emptyNearbyChunkSections, counter);
+ }
+
+ dataBitsWriter.setBitsPerObject(chunkPacketInfoAntiXray.getBitsPerObject(chunkSectionIndex));
+ nearbyChunkSections[0] = chunkPacketInfoAntiXray.getNearbyChunks()[0] == null ? Chunk.EMPTY_CHUNK_SECTION : chunkPacketInfoAntiXray.getNearbyChunks()[0].getSections()[chunkSectionIndex];
+ nearbyChunkSections[1] = chunkPacketInfoAntiXray.getNearbyChunks()[1] == null ? Chunk.EMPTY_CHUNK_SECTION : chunkPacketInfoAntiXray.getNearbyChunks()[1].getSections()[chunkSectionIndex];
+ nearbyChunkSections[2] = chunkPacketInfoAntiXray.getNearbyChunks()[2] == null ? Chunk.EMPTY_CHUNK_SECTION : chunkPacketInfoAntiXray.getNearbyChunks()[2].getSections()[chunkSectionIndex];
+ nearbyChunkSections[3] = chunkPacketInfoAntiXray.getNearbyChunks()[3] == null ? Chunk.EMPTY_CHUNK_SECTION : chunkPacketInfoAntiXray.getNearbyChunks()[3].getSections()[chunkSectionIndex];
+
+ //Obfuscate all layers of the current chunk section except the upper one
+ for (int y = 0; y < 15; y++) {
+ boolean[][] temp = current;
+ current = next;
+ next = nextNext;
+ nextNext = temp;
+ counter = obfuscateLayer(y, dataBitsReader, dataBitsWriter, solidTemp, obfuscateTemp, predefinedBlockDataBitsTemp, current, next, nextNext, nearbyChunkSections, counter);
+ }
+
+ //Check if the chunk section above doesn't need obfuscation
+ if (chunkSectionIndex == maxChunkSectionIndex || !chunkPacketInfoAntiXray.isWritten(chunkSectionIndex + 1) || chunkPacketInfoAntiXray.getPredefinedObjects(chunkSectionIndex + 1) == null) {
+ //If so, obfuscate the upper layer of the current chunk section by reading blocks of the first layer from the chunk section above if it exists
+ ChunkSection aboveChunkSection;
+
+ if (chunkSectionIndex != 15 && (aboveChunkSection = chunkPacketInfoAntiXray.getChunk().getSections()[chunkSectionIndex + 1]) != Chunk.EMPTY_CHUNK_SECTION) {
+ boolean[][] temp = current;
+ current = next;
+ next = nextNext;
+ nextNext = temp;
+
+ for (int z = 0; z < 16; z++) {
+ for (int x = 0; x < 16; x++) {
+ if (!solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(aboveChunkSection.getType(x, 0, z))]) {
+ current[z][x] = true;
+ }
+ }
+ }
+
+ //There is nothing to read anymore
+ dataBitsReader.setBitsPerObject(0);
+ solid[0] = true;
+ counter = obfuscateLayer(15, dataBitsReader, dataBitsWriter, solid, obfuscateTemp, predefinedBlockDataBitsTemp, current, next, nextNext, nearbyChunkSections, counter);
+ }
+ } else {
+ //If not, initialize the reader and other stuff for the chunk section above to obfuscate the upper layer of the current chunk section
+ dataBitsReader.setBitsPerObject(chunkPacketInfoAntiXray.getBitsPerObject(chunkSectionIndex + 1));
+ dataBitsReader.setIndex(chunkPacketInfoAntiXray.getOrCreateIdForIndex(chunkSectionIndex + 1));
+ solidTemp = readDataPalette(chunkPacketInfoAntiXray.getDataPalette(chunkSectionIndex + 1), solid, solidGlobal);
+ obfuscateTemp = readDataPalette(chunkPacketInfoAntiXray.getDataPalette(chunkSectionIndex + 1), obfuscate, obfuscateGlobal);
+ boolean[][] temp = current;
+ current = next;
+ next = nextNext;
+ nextNext = temp;
+ counter = obfuscateLayer(15, dataBitsReader, dataBitsWriter, solidTemp, obfuscateTemp, predefinedBlockDataBitsTemp, current, next, nextNext, nearbyChunkSections, counter);
+ }
+
+ dataBitsWriter.finish();
+ }
+ }
+
+ chunkPacketInfoAntiXray.getPacketPlayOutMapChunk().setReady(true);
+
+ } finally {
+ if (chunkPacketInfoAntiXray.ticketHold != null) {
+ Runnable runnable = () -> {
+ Chunk chunk = chunkPacketInfoAntiXray.getChunk();
+ ChunkCoordIntPair chunkPos = chunk.getPos();
+
+ ChunkPacketBlockControllerAntiXray.this.removeXrayTickets(chunkPos.x, chunkPos.z, (ChunkProviderServer) chunk.world.getChunkProvider(),
+ chunkPacketInfoAntiXray.ticketHold);
+ };
+ if (MinecraftServer.getServer().isMainThread()) {
+ runnable.run();
+ } else {
+ MinecraftServer.getServer().scheduleOnMain(runnable);
+ }
+ }
+ }
+ }
+
+ private int obfuscateLayer(int y, DataBitsReader dataBitsReader, DataBitsWriter dataBitsWriter, boolean[] solid, boolean[] obfuscate, int[] predefinedBlockDataBits, boolean[][] current, boolean[][] next, boolean[][] nextNext, ChunkSection[] nearbyChunkSections, int counter) {
+ //First block of first line
+ int dataBits = dataBitsReader.read();
+
+ if (nextNext[0][0] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[0][1] = true;
+ next[1][0] = true;
+ } else {
+ if (nearbyChunkSections[2] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[2].getType(0, y, 15))] || nearbyChunkSections[0] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[0].getType(15, y, 0))] || current[0][0]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[0][0] = true;
+ }
+
+ //First line
+ for (int x = 1; x < 15; x++) {
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[0][x] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[0][x - 1] = true;
+ next[0][x + 1] = true;
+ next[1][x] = true;
+ } else {
+ if (nearbyChunkSections[2] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[2].getType(x, y, 15))] || current[0][x]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[0][x] = true;
+ }
+ }
+
+ //Last block of first line
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[0][15] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[0][14] = true;
+ next[1][15] = true;
+ } else {
+ if (nearbyChunkSections[2] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[2].getType(15, y, 15))] || nearbyChunkSections[1] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[1].getType(0, y, 0))] || current[0][15]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[0][15] = true;
+ }
+
+ //All inner lines
+ for (int z = 1; z < 15; z++) {
+ //First block
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[z][0] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[z][1] = true;
+ next[z - 1][0] = true;
+ next[z + 1][0] = true;
+ } else {
+ if (nearbyChunkSections[0] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[0].getType(15, y, z))] || current[z][0]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[z][0] = true;
+ }
+
+ //All inner blocks
+ for (int x = 1; x < 15; x++) {
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[z][x] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[z][x - 1] = true;
+ next[z][x + 1] = true;
+ next[z - 1][x] = true;
+ next[z + 1][x] = true;
+ } else {
+ if (current[z][x]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[z][x] = true;
+ }
+ }
+
+ //Last block
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[z][15] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[z][14] = true;
+ next[z - 1][15] = true;
+ next[z + 1][15] = true;
+ } else {
+ if (nearbyChunkSections[1] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[1].getType(0, y, z))] || current[z][15]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[z][15] = true;
+ }
+ }
+
+ //First block of last line
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[15][0] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[15][1] = true;
+ next[14][0] = true;
+ } else {
+ if (nearbyChunkSections[3] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[3].getType(0, y, 0))] || nearbyChunkSections[0] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[0].getType(15, y, 15))] || current[15][0]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[15][0] = true;
+ }
+
+ //Last line
+ for (int x = 1; x < 15; x++) {
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[15][x] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[15][x - 1] = true;
+ next[15][x + 1] = true;
+ next[14][x] = true;
+ } else {
+ if (nearbyChunkSections[3] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[3].getType(x, y, 0))] || current[15][x]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[15][x] = true;
+ }
+ }
+
+ //Last block of last line
+ dataBits = dataBitsReader.read();
+
+ if (nextNext[15][15] = !solid[dataBits]) {
+ dataBitsWriter.skip();
+ next[15][14] = true;
+ next[14][15] = true;
+ } else {
+ if (nearbyChunkSections[3] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[3].getType(15, y, 0))] || nearbyChunkSections[1] == Chunk.EMPTY_CHUNK_SECTION || !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(nearbyChunkSections[1].getType(0, y, 15))] || current[15][15]) {
+ dataBitsWriter.skip();
+ } else {
+ if (counter >= predefinedBlockDataBits.length) {
+ counter = 0;
+ }
+
+ dataBitsWriter.write(predefinedBlockDataBits[counter++]);
+ }
+ }
+
+ if (!obfuscate[dataBits]) {
+ next[15][15] = true;
+ }
+
+ return counter;
+ }
+
+ private boolean[] readDataPalette(DataPalette<IBlockData> dataPalette, boolean[] temp, boolean[] global) {
+ if (dataPalette == ChunkSection.GLOBAL_PALETTE) {
+ return global;
+ }
+
+ IBlockData blockData;
+
+ for (int i = 0; (blockData = dataPalette.getObject(i)) != null; i++) {
+ temp[i] = global[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(blockData)];
+ }
+
+ return temp;
+ }
+
+ @Override
+ public void onBlockChange(World world, BlockPosition blockPosition, IBlockData newBlockData, IBlockData oldBlockData, int flag) {
+ if (oldBlockData != null && solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(oldBlockData)] && !solidGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(newBlockData)] && blockPosition.getY() <= maxBlockYUpdatePosition) {
+ updateNearbyBlocks(world, blockPosition);
+ }
+ }
+
+ @Override
+ public void onPlayerLeftClickBlock(PlayerInteractManager playerInteractManager, BlockPosition blockPosition, EnumDirection enumDirection) {
+ if (blockPosition.getY() <= maxBlockYUpdatePosition) {
+ updateNearbyBlocks(playerInteractManager.world, blockPosition);
+ }
+ }
+
+ private void updateNearbyBlocks(World world, BlockPosition blockPosition) {
+ if (updateRadius >= 2) {
+ BlockPosition temp = blockPosition.west();
+ updateBlock(world, temp);
+ updateBlock(world, temp.west());
+ updateBlock(world, temp.down());
+ updateBlock(world, temp.up());
+ updateBlock(world, temp.north());
+ updateBlock(world, temp.south());
+ updateBlock(world, temp = blockPosition.east());
+ updateBlock(world, temp.east());
+ updateBlock(world, temp.down());
+ updateBlock(world, temp.up());
+ updateBlock(world, temp.north());
+ updateBlock(world, temp.south());
+ updateBlock(world, temp = blockPosition.down());
+ updateBlock(world, temp.down());
+ updateBlock(world, temp.north());
+ updateBlock(world, temp.south());
+ updateBlock(world, temp = blockPosition.up());
+ updateBlock(world, temp.up());
+ updateBlock(world, temp.north());
+ updateBlock(world, temp.south());
+ updateBlock(world, temp = blockPosition.north());
+ updateBlock(world, temp.north());
+ updateBlock(world, temp = blockPosition.south());
+ updateBlock(world, temp.south());
+ } else if (updateRadius == 1) {
+ updateBlock(world, blockPosition.west());
+ updateBlock(world, blockPosition.east());
+ updateBlock(world, blockPosition.down());
+ updateBlock(world, blockPosition.up());
+ updateBlock(world, blockPosition.north());
+ updateBlock(world, blockPosition.south());
+ } else {
+ //Do nothing if updateRadius <= 0 (test mode)
+ }
+ }
+
+ private void updateBlock(World world, BlockPosition blockPosition) {
+ IBlockData blockData = world.getTypeIfLoaded(blockPosition);
+
+ if (blockData != null && obfuscateGlobal[ChunkSection.GLOBAL_PALETTE.getOrCreateIdFor(blockData)]) {
+ //world.notify(blockPosition, blockData, blockData, 3);
+ ((WorldServer)world).getChunkProvider().flagDirty(blockPosition); // We only need to re-send to client
+ }
+ }
+
+ public enum EngineMode {
+
+ HIDE(1, "hide ores"),
+ OBFUSCATE(2, "obfuscate");
+
+ private final int id;
+ private final String description;
+
+ EngineMode(int id, String description) {
+ this.id = id;
+ this.description = description;
+ }
+
+ public static EngineMode getById(int id) {
+ for (EngineMode engineMode : values()) {
+ if (engineMode.id == id) {
+ return engineMode;
+ }
+ }
+
+ return null;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+ }
+
+ public enum ChunkEdgeMode {
+
+ DEFAULT(1, "default"),
+ WAIT(2, "wait until nearby chunks are loaded"),
+ LOAD(3, "load nearby chunks");
+
+ private final int id;
+ private final String description;
+
+ ChunkEdgeMode(int id, String description) {
+ this.id = id;
+ this.description = description;
+ }
+
+ public static ChunkEdgeMode getById(int id) {
+ for (ChunkEdgeMode chunkEdgeMode : values()) {
+ if (chunkEdgeMode.id == id) {
+ return chunkEdgeMode;
+ }
+ }
+
+ return null;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+ }
+}
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
index 000000000..a68bace35
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfo.java
@@ -0,0 +1,81 @@
+package com.destroystokyo.paper.antixray;
+
+import net.minecraft.server.Chunk;
+import net.minecraft.server.DataPalette;
+import net.minecraft.server.PacketPlayOutMapChunk;
+
+public class ChunkPacketInfo<T> {
+
+ private final PacketPlayOutMapChunk packetPlayOutMapChunk;
+ private final Chunk chunk;
+ private final int chunkSectionSelector;
+ private byte[] data;
+ private final int[] bitsPerObject = new int[16];
+ private final Object[] dataPalettes = new Object[16];
+ private final int[] dataBitsIndexes = new int[16];
+ private final Object[][] predefinedObjects = new Object[16][];
+
+ public ChunkPacketInfo(PacketPlayOutMapChunk packetPlayOutMapChunk, Chunk chunk, int chunkSectionSelector) {
+ this.packetPlayOutMapChunk = packetPlayOutMapChunk;
+ this.chunk = chunk;
+ this.chunkSectionSelector = chunkSectionSelector;
+ }
+
+ public PacketPlayOutMapChunk getPacketPlayOutMapChunk() {
+ return packetPlayOutMapChunk;
+ }
+
+ public Chunk getChunk() {
+ return chunk;
+ }
+
+ public int getChunkSectionSelector() {
+ return chunkSectionSelector;
+ }
+
+ public byte[] getData() {
+ return data;
+ }
+
+ public void setData(byte[] data) {
+ this.data = data;
+ }
+
+ public int getBitsPerObject(int chunkSectionIndex) {
+ return bitsPerObject[chunkSectionIndex];
+ }
+
+ public void setBitsPerObject(int chunkSectionIndex, int bitsPerObject) {
+ this.bitsPerObject[chunkSectionIndex] = bitsPerObject;
+ }
+
+ @SuppressWarnings("unchecked")
+ public DataPalette<T> getDataPalette(int chunkSectionIndex) {
+ return (DataPalette<T>) dataPalettes[chunkSectionIndex];
+ }
+
+ public void setDataPalette(int chunkSectionIndex, DataPalette<T> dataPalette) {
+ dataPalettes[chunkSectionIndex] = dataPalette;
+ }
+
+ public int getOrCreateIdForIndex(int chunkSectionIndex) {
+ return dataBitsIndexes[chunkSectionIndex];
+ }
+
+ public void setDataBitsIndex(int chunkSectionIndex, int dataBitsIndex) {
+ dataBitsIndexes[chunkSectionIndex] = dataBitsIndex;
+ }
+
+ @SuppressWarnings("unchecked")
+ public T[] getPredefinedObjects(int chunkSectionIndex) {
+ return (T[]) predefinedObjects[chunkSectionIndex];
+ }
+
+ public void setPredefinedObjects(int chunkSectionIndex, T[] predefinedObjects) {
+ this.predefinedObjects[chunkSectionIndex] = predefinedObjects;
+ }
+
+ public boolean isWritten(int chunkSectionIndex) {
+ return bitsPerObject[chunkSectionIndex] != 0;
+ }
+}
diff --git a/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfoAntiXray.java b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfoAntiXray.java
new file mode 100644
index 000000000..067dfb2f1
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/antixray/ChunkPacketInfoAntiXray.java
@@ -0,0 +1,31 @@
+package com.destroystokyo.paper.antixray;