| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.tags.FluidTags;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.WorldGenLevel;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 13 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 14 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 15 |
|
| 16 | @Deprecated
|
| 17 | public record LakeFeature(
|
| 18 | Holder<BlockStateProvider> fluid,
|
| 19 | Holder<BlockStateProvider> barrier,
|
| 20 | BlockPredicate canPlaceFeature,
|
| 21 | BlockPredicate canReplaceWithAirOrFluid,
|
| 22 | BlockPredicate canReplaceWithBarrier
|
| 23 | ) implements Feature {
|
| 24 | public static final MapCodec<LakeFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 25 | i -> i.group(
|
| 26 | BlockStateProvider.CODEC.fieldOf("fluid").forGetter(LakeFeature::fluid),
|
| 27 | BlockStateProvider.CODEC.fieldOf("barrier").forGetter(LakeFeature::barrier),
|
| 28 | BlockPredicate.CODEC.fieldOf("can_place_feature").forGetter(LakeFeature::canPlaceFeature),
|
| 29 | BlockPredicate.CODEC.fieldOf("can_replace_with_air_or_fluid").forGetter(LakeFeature::canReplaceWithAirOrFluid),
|
| 30 | BlockPredicate.CODEC.fieldOf("can_replace_with_barrier").forGetter(LakeFeature::canReplaceWithBarrier)
|
| 31 | )
|
| 32 | .apply(i, LakeFeature::new)
|
| 33 | );
|
| 34 | private static final BlockState AIR = Blocks.CAVE_AIR.defaultBlockState();
|
| 35 |
|
| 36 | @Override
|
| 37 | public MapCodec<LakeFeature> codec() {
|
| 38 | return CODEC;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, BlockPos origin) {
|
| 43 | if (origin.getY() <= level.getMinY() + 4) {
|
| 44 | return false;
|
| 45 | }
|
| 46 |
|
| 47 | origin = origin.offset(-8, -4, -8);
|
| 48 | boolean[] grid = new boolean[2048];
|
| 49 | int spots = random.nextInt(4) + 4;
|
| 50 |
|
| 51 | for (int i = 0; i < spots; i++) {
|
| 52 | double xr = random.nextDouble() * 6.0 + 3.0;
|
| 53 | double yr = random.nextDouble() * 4.0 + 2.0;
|
| 54 | double zr = random.nextDouble() * 6.0 + 3.0;
|
| 55 | double xp = random.nextDouble() * (16.0 - xr - 2.0) + 1.0 + xr / 2.0;
|
| 56 | double yp = random.nextDouble() * (8.0 - yr - 4.0) + 2.0 + yr / 2.0;
|
| 57 | double zp = random.nextDouble() * (16.0 - zr - 2.0) + 1.0 + zr / 2.0;
|
| 58 |
|
| 59 | for (int xx = 1; xx < 15; xx++) {
|
| 60 | for (int zz = 1; zz < 15; zz++) {
|
| 61 | for (int yy = 1; yy < 7; yy++) {
|
| 62 | double xd = (xx - xp) / (xr / 2.0);
|
| 63 | double yd = (yy - yp) / (yr / 2.0);
|
| 64 | double zd = (zz - zp) / (zr / 2.0);
|
| 65 | double d = xd * xd + yd * yd + zd * zd;
|
| 66 | if (d < 1.0) {
|
| 67 | grid[(xx * 16 + zz) * 8 + yy] = true;
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | BlockState fluid = this.fluid.value().getState(level, random, origin);
|
| 75 |
|
| 76 | for (int xx = 0; xx < 16; xx++) {
|
| 77 | for (int zz = 0; zz < 16; zz++) {
|
| 78 | for (int yy = 0; yy < 8; yy++) {
|
| 79 | boolean check = !grid[(xx * 16 + zz) * 8 + yy]
|
| 80 | && (
|
| 81 | xx < 15 && grid[((xx + 1) * 16 + zz) * 8 + yy]
|
| 82 | || xx > 0 && grid[((xx - 1) * 16 + zz) * 8 + yy]
|
| 83 | || zz < 15 && grid[(xx * 16 + zz + 1) * 8 + yy]
|
| 84 | || zz > 0 && grid[(xx * 16 + (zz - 1)) * 8 + yy]
|
| 85 | || yy < 7 && grid[(xx * 16 + zz) * 8 + yy + 1]
|
| 86 | || yy > 0 && grid[(xx * 16 + zz) * 8 + (yy - 1)]
|
| 87 | );
|
| 88 | if (check) {
|
| 89 | BlockPos offsetPos = origin.offset(xx, yy, zz);
|
| 90 | BlockState blockState = level.getBlockState(offsetPos);
|
| 91 | if (yy >= 4 && blockState.liquid()) {
|
| 92 | return false;
|
| 93 | }
|
| 94 |
|
| 95 | if (yy < 4 && !blockState.isSolid() && blockState != fluid) {
|
| 96 | return false;
|
| 97 | }
|
| 98 |
|
| 99 | if (!this.canPlaceFeature.test(level, offsetPos)) {
|
| 100 | return false;
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | for (int xx = 0; xx < 16; xx++) {
|
| 108 | for (int zz = 0; zz < 16; zz++) {
|
| 109 | for (int yy = 0; yy < 8; yy++) {
|
| 110 | if (grid[(xx * 16 + zz) * 8 + yy]) {
|
| 111 | BlockPos placePos = origin.offset(xx, yy, zz);
|
| 112 | if (this.canReplaceWithAirOrFluid.test(level, placePos)) {
|
| 113 | boolean placeAir = yy >= 4;
|
| 114 | level.setBlock(placePos, placeAir ? AIR : fluid, 2);
|
| 115 | if (placeAir) {
|
| 116 | level.scheduleTick(placePos, AIR.getBlock(), 0);
|
| 117 | this.markAboveForPostProcessing(level, placePos);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | BlockState barrier = this.barrier.value().getState(level, random, origin);
|
| 126 | if (!barrier.isAir()) {
|
| 127 | for (int xx = 0; xx < 16; xx++) {
|
| 128 | for (int zz = 0; zz < 16; zz++) {
|
| 129 | for (int yy = 0; yy < 8; yy++) {
|
| 130 | boolean check = !grid[(xx * 16 + zz) * 8 + yy]
|
| 131 | && (
|
| 132 | xx < 15 && grid[((xx + 1) * 16 + zz) * 8 + yy]
|
| 133 | || xx > 0 && grid[((xx - 1) * 16 + zz) * 8 + yy]
|
| 134 | || zz < 15 && grid[(xx * 16 + zz + 1) * 8 + yy]
|
| 135 | || zz > 0 && grid[(xx * 16 + (zz - 1)) * 8 + yy]
|
| 136 | || yy < 7 && grid[(xx * 16 + zz) * 8 + yy + 1]
|
| 137 | || yy > 0 && grid[(xx * 16 + zz) * 8 + (yy - 1)]
|
| 138 | );
|
| 139 | if (check && (yy < 4 || random.nextInt(2) != 0)) {
|
| 140 | BlockPos offset = origin.offset(xx, yy, zz);
|
| 141 | BlockState blockState = level.getBlockState(offset);
|
| 142 | if (blockState.isSolid() && this.canReplaceWithBarrier.test(level, offset)) {
|
| 143 | BlockPos barrierPos = origin.offset(xx, yy, zz);
|
| 144 | level.setBlock(barrierPos, barrier, 2);
|
| 145 | this.markAboveForPostProcessing(level, barrierPos);
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | if (fluid.getFluidState().is(FluidTags.WATER)) {
|
| 154 | for (int xx = 0; xx < 16; xx++) {
|
| 155 | for (int zz = 0; zz < 16; zz++) {
|
| 156 | int yy = 4;
|
| 157 | BlockPos offset = origin.offset(xx, 4, zz);
|
| 158 | if (level.getBiome(offset).value().shouldFreeze(level, offset, false) && this.canReplaceWithAirOrFluid.test(level, offset)) {
|
| 159 | level.setBlock(offset, Blocks.ICE.defaultBlockState(), 2);
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | return true;
|
| 166 | }
|
| 167 | }
|
| 168 |
|