| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.Vec3i;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import net.minecraft.util.ExtraCodecs;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.level.ChunkPos;
|
| 15 | import net.minecraft.world.level.WorldGenLevel;
|
| 16 | import net.minecraft.world.level.block.Blocks;
|
| 17 | import net.minecraft.world.level.block.Mirror;
|
| 18 | import net.minecraft.world.level.block.Rotation;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 21 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 22 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 23 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 24 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList;
|
| 25 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
|
| 26 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 27 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 28 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 29 |
|
| 30 | public record FossilFeature(
|
| 31 | List<Identifier> fossilStructures,
|
| 32 | List<Identifier> overlayStructures,
|
| 33 | Holder<StructureProcessorList> fossilProcessors,
|
| 34 | Holder<StructureProcessorList> overlayProcessors,
|
| 35 | int maxEmptyCornersAllowed
|
| 36 | ) implements Feature {
|
| 37 | public static final MapCodec<FossilFeature> CODEC = RecordCodecBuilder.<FossilFeature>mapCodec(
|
| 38 | i -> i.group(
|
| 39 | ExtraCodecs.nonEmptyList(Identifier.CODEC.listOf()).fieldOf("fossil_structures").forGetter(FossilFeature::fossilStructures),
|
| 40 | ExtraCodecs.nonEmptyList(Identifier.CODEC.listOf()).fieldOf("overlay_structures").forGetter(FossilFeature::overlayStructures),
|
| 41 | StructureProcessorType.LIST_CODEC.fieldOf("fossil_processors").forGetter(FossilFeature::fossilProcessors),
|
| 42 | StructureProcessorType.LIST_CODEC.fieldOf("overlay_processors").forGetter(FossilFeature::overlayProcessors),
|
| 43 | Codec.intRange(0, 7).fieldOf("max_empty_corners_allowed").forGetter(FossilFeature::maxEmptyCornersAllowed)
|
| 44 | )
|
| 45 | .apply(i, FossilFeature::new)
|
| 46 | )
|
| 47 | .validate(
|
| 48 | fossil -> fossil.fossilStructures.size() == fossil.overlayStructures.size()
|
| 49 | ? DataResult.success(fossil)
|
| 50 | : DataResult.error(() -> "Fossil structure lists must be equal lengths")
|
| 51 | );
|
| 52 |
|
| 53 | @Override
|
| 54 | public MapCodec<FossilFeature> codec() {
|
| 55 | return CODEC;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 60 | Rotation rotation = Rotation.getRandom(random);
|
| 61 | int fossilIndex = random.nextInt(this.fossilStructures().size());
|
| 62 | StructureTemplateManager structureTemplateManager = level.getLevel().getServer().getStructureTemplateManager();
|
| 63 | StructureTemplate fossilBase = structureTemplateManager.getOrCreate(this.fossilStructures().get(fossilIndex));
|
| 64 | StructureTemplate fossilOverlay = structureTemplateManager.getOrCreate(this.overlayStructures().get(fossilIndex));
|
| 65 | ChunkPos chunkPos = ChunkPos.containing(origin);
|
| 66 | BoundingBox boundingBox = new BoundingBox(
|
| 67 | chunkPos.getMinBlockX() - 16, level.getMinY(), chunkPos.getMinBlockZ() - 16, chunkPos.getMaxBlockX() + 16, level.getMaxY(), chunkPos.getMaxBlockZ() + 16
|
| 68 | );
|
| 69 | StructurePlaceSettings settings = new StructurePlaceSettings().setRotation(rotation).setBoundingBox(boundingBox).setRandom(random);
|
| 70 | Vec3i size = fossilBase.getSize(rotation);
|
| 71 | BlockPos lowCorner = origin.offset(-size.getX() / 2, 0, -size.getZ() / 2);
|
| 72 | int lowestSurfaceY = origin.getY();
|
| 73 |
|
| 74 | for (int xscan = 0; xscan < size.getX(); xscan++) {
|
| 75 | for (int zscan = 0; zscan < size.getZ(); zscan++) {
|
| 76 | lowestSurfaceY = Math.min(lowestSurfaceY, level.getHeight(Heightmap.Types.OCEAN_FLOOR_WG, lowCorner.getX() + xscan, lowCorner.getZ() + zscan));
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | int targetY = Math.max(lowestSurfaceY - 15 - random.nextInt(10), level.getMinY() + 10);
|
| 81 | BlockPos targetPos = fossilBase.getZeroPositionWithTransform(lowCorner.atY(targetY), Mirror.NONE, rotation);
|
| 82 | if (countEmptyCorners(level, fossilBase.getBoundingBox(settings, targetPos)) > this.maxEmptyCornersAllowed()) {
|
| 83 | return false;
|
| 84 | }
|
| 85 |
|
| 86 | settings.clearProcessors();
|
| 87 | this.fossilProcessors().value().list().forEach(settings::addProcessor);
|
| 88 | fossilBase.placeInWorld(level, targetPos, targetPos, settings, random, 260);
|
| 89 | settings.clearProcessors();
|
| 90 | this.overlayProcessors().value().list().forEach(settings::addProcessor);
|
| 91 | fossilOverlay.placeInWorld(level, targetPos, targetPos, settings, random, 260);
|
| 92 | return true;
|
| 93 | }
|
| 94 |
|
| 95 | private static int countEmptyCorners(final WorldGenLevel level, final BoundingBox structureBounds) {
|
| 96 | MutableInt count = new MutableInt(0);
|
| 97 | structureBounds.forAllCorners(pos -> {
|
| 98 | BlockState state = level.getBlockState(pos);
|
| 99 | if (state.isAir() || state.is(Blocks.LAVA) || state.is(Blocks.WATER)) {
|
| 100 | count.add(1);
|
| 101 | }
|
| 102 | });
|
| 103 | return count.intValue();
|
| 104 | }
|
| 105 | }
|
| 106 |
|