| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 5 | import java.util.Set;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Vec3i;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.SortedArraySet;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import net.minecraft.world.level.ChunkPos;
|
| 12 | import net.minecraft.world.level.StructureManager;
|
| 13 | import net.minecraft.world.level.WorldGenLevel;
|
| 14 | import net.minecraft.world.level.block.Blocks;
|
| 15 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 16 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 17 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 18 | import net.minecraft.world.level.levelgen.structure.SinglePieceStructure;
|
| 19 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 20 | import net.minecraft.world.level.levelgen.structure.StructurePiece;
|
| 21 | import net.minecraft.world.level.levelgen.structure.StructureType;
|
| 22 | import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer;
|
| 23 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 24 |
|
| 25 | public class DesertPyramidStructure extends SinglePieceStructure {
|
| 26 | public static final MapCodec<DesertPyramidStructure> CODEC = simpleCodec(DesertPyramidStructure::new);
|
| 27 |
|
| 28 | public DesertPyramidStructure(final Structure.StructureSettings settings) {
|
| 29 | super(DesertPyramidPiece::new, 21, 21, settings);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void afterPlace(
|
| 34 | final WorldGenLevel level,
|
| 35 | final StructureManager structureManager,
|
| 36 | final ChunkGenerator generator,
|
| 37 | final RandomSource random,
|
| 38 | final BoundingBox chunkBB,
|
| 39 | final ChunkPos chunkPos,
|
| 40 | final PiecesContainer pieces
|
| 41 | ) {
|
| 42 | Set<BlockPos> uniqueSandPlacements = SortedArraySet.create(Vec3i::compareTo);
|
| 43 |
|
| 44 | for (StructurePiece piece : pieces.pieces()) {
|
| 45 | if (piece instanceof DesertPyramidPiece desertPyramidPiece) {
|
| 46 | uniqueSandPlacements.addAll(desertPyramidPiece.getPotentialSuspiciousSandWorldPositions());
|
| 47 | placeSuspiciousSand(chunkBB, level, desertPyramidPiece.getRandomCollapsedRoofPos());
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | ObjectArrayList<BlockPos> shuffledSandPlacements = new ObjectArrayList<>(uniqueSandPlacements.stream().toList());
|
| 52 | RandomSource positionalRandom = RandomSource.createThreadLocalInstance(level.getSeed()).forkPositional().at(pieces.calculateBoundingBox().getCenter());
|
| 53 | Util.shuffle(shuffledSandPlacements, positionalRandom);
|
| 54 | int suspiciousSandToPlace = Math.min(uniqueSandPlacements.size(), positionalRandom.nextInt(5, 8));
|
| 55 |
|
| 56 | for (BlockPos blockPos : shuffledSandPlacements) {
|
| 57 | if (suspiciousSandToPlace > 0) {
|
| 58 | suspiciousSandToPlace--;
|
| 59 | placeSuspiciousSand(chunkBB, level, blockPos);
|
| 60 | } else if (chunkBB.isInside(blockPos)) {
|
| 61 | level.setBlock(blockPos, Blocks.SAND.defaultBlockState(), 2);
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | private static void placeSuspiciousSand(final BoundingBox chunkBB, final WorldGenLevel level, final BlockPos blockPos) {
|
| 67 | if (chunkBB.isInside(blockPos)) {
|
| 68 | level.setBlock(blockPos, Blocks.SUSPICIOUS_SAND.defaultBlockState(), 2);
|
| 69 | level.getBlockEntity(blockPos, BlockEntityTypes.BRUSHABLE_BLOCK)
|
| 70 | .ifPresent(entity -> entity.setLootTable(BuiltInLootTables.DESERT_PYRAMID_ARCHAEOLOGY, blockPos.asLong()));
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public StructureType<?> type() {
|
| 76 | return StructureType.DESERT_PYRAMID;
|
| 77 | }
|
| 78 | }
|
| 79 |
|