| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.tags.BlockTags;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import net.minecraft.world.level.LevelReader;
|
| 12 | import net.minecraft.world.level.block.Block;
|
| 13 | import net.minecraft.world.level.block.Blocks;
|
| 14 | import net.minecraft.world.level.block.StairBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.properties.Half;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class BlockAgeProcessor implements StructureProcessor {
|
| 20 | public static final MapCodec<BlockAgeProcessor> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(Codec.FLOAT.fieldOf("mossiness").forGetter(p -> p.mossiness)).apply(i, BlockAgeProcessor::new)
|
| 22 | );
|
| 23 | private static final float PROBABILITY_OF_REPLACING_FULL_BLOCK = 0.5F;
|
| 24 | private static final float PROBABILITY_OF_REPLACING_STAIRS = 0.5F;
|
| 25 | private static final float PROBABILITY_OF_REPLACING_OBSIDIAN = 0.15F;
|
| 26 | private static final BlockState[] NON_MOSSY_REPLACEMENTS = new BlockState[]{
|
| 27 | Blocks.STONE_SLAB.defaultBlockState(), Blocks.STONE_BRICK_SLAB.defaultBlockState()
|
| 28 | };
|
| 29 | private final float mossiness;
|
| 30 |
|
| 31 | public BlockAgeProcessor(final float mossiness) {
|
| 32 | this.mossiness = mossiness;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public StructureTemplate.@Nullable StructureBlockInfo processBlock(
|
| 37 | final LevelReader level,
|
| 38 | final BlockPos targetPosition,
|
| 39 | final BlockPos referencePos,
|
| 40 | final BlockPos templateRelativePos,
|
| 41 | final StructureTemplate.StructureBlockInfo processedBlockInfo,
|
| 42 | final StructurePlaceSettings settings
|
| 43 | ) {
|
| 44 | RandomSource random = settings.getRandom(processedBlockInfo.pos());
|
| 45 | BlockState state = processedBlockInfo.state();
|
| 46 | BlockPos pos = processedBlockInfo.pos();
|
| 47 | BlockState newState = null;
|
| 48 | if (state.is(Blocks.STONE_BRICKS) || state.is(Blocks.STONE) || state.is(Blocks.CHISELED_STONE_BRICKS)) {
|
| 49 | newState = this.maybeReplaceFullStoneBlock(random);
|
| 50 | } else if (state.is(BlockTags.STAIRS)) {
|
| 51 | newState = this.maybeReplaceStairs(state, random);
|
| 52 | } else if (state.is(BlockTags.SLABS)) {
|
| 53 | newState = this.maybeReplaceSlab(state, random);
|
| 54 | } else if (state.is(BlockTags.WALLS)) {
|
| 55 | newState = this.maybeReplaceWall(state, random);
|
| 56 | } else if (state.is(Blocks.OBSIDIAN)) {
|
| 57 | newState = this.maybeReplaceObsidian(random);
|
| 58 | }
|
| 59 |
|
| 60 | return newState != null ? new StructureTemplate.StructureBlockInfo(pos, newState, processedBlockInfo.nbt()) : processedBlockInfo;
|
| 61 | }
|
| 62 |
|
| 63 | private @Nullable BlockState maybeReplaceFullStoneBlock(final RandomSource random) {
|
| 64 | if (random.nextFloat() >= 0.5F) {
|
| 65 | return null;
|
| 66 | }
|
| 67 |
|
| 68 | BlockState[] nonMossyReplacements = new BlockState[]{
|
| 69 | Blocks.CRACKED_STONE_BRICKS.defaultBlockState(), getRandomFacingStairs(random, Blocks.STONE_BRICK_STAIRS)
|
| 70 | };
|
| 71 | BlockState[] mossyReplacements = new BlockState[]{
|
| 72 | Blocks.MOSSY_STONE_BRICKS.defaultBlockState(), getRandomFacingStairs(random, Blocks.MOSSY_STONE_BRICK_STAIRS)
|
| 73 | };
|
| 74 | return this.getRandomBlock(random, nonMossyReplacements, mossyReplacements);
|
| 75 | }
|
| 76 |
|
| 77 | private @Nullable BlockState maybeReplaceStairs(final BlockState blockState, final RandomSource random) {
|
| 78 | if (random.nextFloat() >= 0.5F) {
|
| 79 | return null;
|
| 80 | }
|
| 81 |
|
| 82 | BlockState[] mossyReplacements = new BlockState[]{
|
| 83 | Blocks.MOSSY_STONE_BRICK_STAIRS.withPropertiesOf(blockState), Blocks.MOSSY_STONE_BRICK_SLAB.defaultBlockState()
|
| 84 | };
|
| 85 | return this.getRandomBlock(random, NON_MOSSY_REPLACEMENTS, mossyReplacements);
|
| 86 | }
|
| 87 |
|
| 88 | private @Nullable BlockState maybeReplaceSlab(final BlockState blockState, final RandomSource random) {
|
| 89 | return random.nextFloat() < this.mossiness ? Blocks.MOSSY_STONE_BRICK_SLAB.withPropertiesOf(blockState) : null;
|
| 90 | }
|
| 91 |
|
| 92 | private @Nullable BlockState maybeReplaceWall(final BlockState blockState, final RandomSource random) {
|
| 93 | return random.nextFloat() < this.mossiness ? Blocks.MOSSY_STONE_BRICK_WALL.withPropertiesOf(blockState) : null;
|
| 94 | }
|
| 95 |
|
| 96 | private @Nullable BlockState maybeReplaceObsidian(final RandomSource random) {
|
| 97 | return random.nextFloat() < 0.15F ? Blocks.CRYING_OBSIDIAN.defaultBlockState() : null;
|
| 98 | }
|
| 99 |
|
| 100 | private static BlockState getRandomFacingStairs(final RandomSource random, final Block stairBlock) {
|
| 101 | return stairBlock.defaultBlockState()
|
| 102 | .setValue(StairBlock.FACING, Direction.Plane.HORIZONTAL.getRandomDirection(random))
|
| 103 | .setValue(StairBlock.HALF, Util.getRandom(Half.values(), random));
|
| 104 | }
|
| 105 |
|
| 106 | private BlockState getRandomBlock(final RandomSource random, final BlockState[] nonMossyBlocks, final BlockState[] mossyBlocks) {
|
| 107 | return random.nextFloat() < this.mossiness ? getRandomBlock(random, mossyBlocks) : getRandomBlock(random, nonMossyBlocks);
|
| 108 | }
|
| 109 |
|
| 110 | private static BlockState getRandomBlock(final RandomSource random, final BlockState[] blocks) {
|
| 111 | return blocks[random.nextInt(blocks.length)];
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public MapCodec<BlockAgeProcessor> codec() {
|
| 116 | return MAP_CODEC;
|
| 117 | }
|
| 118 | }
|
| 119 |
|