|
| 1 | +package me.mapacheee.extendedhorizons.viewdistance.service; |
| 2 | + |
| 3 | +import net.minecraft.world.level.block.Block; |
| 4 | +import net.minecraft.world.level.block.Blocks; |
| 5 | +import net.minecraft.world.level.block.state.BlockState; |
| 6 | +import net.minecraft.world.level.chunk.LevelChunk; |
| 7 | +import net.minecraft.world.level.chunk.LevelChunkSection; |
| 8 | +import net.minecraft.world.level.chunk.PalettedContainer; |
| 9 | + |
| 10 | +import java.util.Random; |
| 11 | + |
| 12 | +/** |
| 13 | + * Anti-Xray utility for obfuscating chunks |
| 14 | + * Replaces valuable ores with stone/deepslate and adds fake ores |
| 15 | + * |
| 16 | + */ |
| 17 | +public class ChunkAntiXray { |
| 18 | + |
| 19 | + private static final BlockState STONE = Blocks.STONE.defaultBlockState(); |
| 20 | + private static final BlockState DEEPSLATE = Blocks.DEEPSLATE.defaultBlockState(); |
| 21 | + |
| 22 | + private static final Block[] VALUABLE_ORES = { |
| 23 | + Blocks.DIAMOND_ORE, Blocks.DEEPSLATE_DIAMOND_ORE, |
| 24 | + Blocks.EMERALD_ORE, Blocks.DEEPSLATE_EMERALD_ORE, |
| 25 | + Blocks.GOLD_ORE, Blocks.DEEPSLATE_GOLD_ORE, |
| 26 | + Blocks.IRON_ORE, Blocks.DEEPSLATE_IRON_ORE, |
| 27 | + Blocks.LAPIS_ORE, Blocks.DEEPSLATE_LAPIS_ORE, |
| 28 | + Blocks.REDSTONE_ORE, Blocks.DEEPSLATE_REDSTONE_ORE, |
| 29 | + Blocks.ANCIENT_DEBRIS, |
| 30 | + Blocks.COPPER_ORE, Blocks.DEEPSLATE_COPPER_ORE |
| 31 | + }; |
| 32 | + |
| 33 | + private static final BlockState[] FAKE_ORES = { |
| 34 | + Blocks.DIAMOND_ORE.defaultBlockState(), |
| 35 | + Blocks.DEEPSLATE_DIAMOND_ORE.defaultBlockState(), |
| 36 | + Blocks.GOLD_ORE.defaultBlockState(), |
| 37 | + Blocks.DEEPSLATE_GOLD_ORE.defaultBlockState(), |
| 38 | + Blocks.IRON_ORE.defaultBlockState() |
| 39 | + }; |
| 40 | + |
| 41 | + /** |
| 42 | + * Obfuscates a chunk by hiding real ores and adding fake ones |
| 43 | + */ |
| 44 | + public static void obfuscateChunk(LevelChunk chunk, boolean hideOres, boolean addFakeOres, double fakeOreDensity) { |
| 45 | + Random random = new Random(chunk.getPos().toLong()); |
| 46 | + LevelChunkSection[] sections = chunk.getSections(); |
| 47 | + |
| 48 | + for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) { |
| 49 | + LevelChunkSection section = sections[sectionIndex]; |
| 50 | + if (section == null || section.hasOnlyAir()) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + int sectionY = chunk.getMinSectionY() + sectionIndex; |
| 55 | + int baseY = sectionY * 16; |
| 56 | + |
| 57 | + PalettedContainer<BlockState> blocks = section.getStates(); |
| 58 | + |
| 59 | + for (int x = 0; x < 16; x++) { |
| 60 | + for (int y = 0; y < 16; y++) { |
| 61 | + for (int z = 0; z < 16; z++) { |
| 62 | + BlockState state = blocks.get(x, y, z); |
| 63 | + if (state == null) |
| 64 | + continue; |
| 65 | + |
| 66 | + Block block = state.getBlock(); |
| 67 | + int worldY = baseY + y; |
| 68 | + |
| 69 | + if (hideOres && isValuableOre(block)) { |
| 70 | + BlockState replacement = worldY < 0 ? DEEPSLATE : STONE; |
| 71 | + blocks.set(x, y, z, replacement); |
| 72 | + } else if (addFakeOres && random.nextDouble() < fakeOreDensity) { |
| 73 | + if (block == Blocks.STONE || block == Blocks.DEEPSLATE) { |
| 74 | + BlockState fakeOre = getFakeOre(random, worldY); |
| 75 | + blocks.set(x, y, z, fakeOre); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean isValuableOre(Block block) { |
| 85 | + for (Block ore : VALUABLE_ORES) { |
| 86 | + if (block == ore) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + private static BlockState getFakeOre(Random random, int y) { |
| 94 | + if (y < 0) { |
| 95 | + return random.nextBoolean() |
| 96 | + ? Blocks.DEEPSLATE_DIAMOND_ORE.defaultBlockState() |
| 97 | + : Blocks.DEEPSLATE_GOLD_ORE.defaultBlockState(); |
| 98 | + } else { |
| 99 | + return FAKE_ORES[random.nextInt(FAKE_ORES.length)]; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments