| 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 it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 8 | import java.util.List;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.HolderSet;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.core.registries.Registries;
|
| 14 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.world.level.WorldGenLevel;
|
| 18 | import net.minecraft.world.level.block.Block;
|
| 19 | import net.minecraft.world.level.block.Blocks;
|
| 20 | import net.minecraft.world.level.block.MultifaceSpreadeableBlock;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 23 |
|
| 24 | public record MultifaceGrowthFeature(
|
| 25 | Block placeBlock,
|
| 26 | int searchRange,
|
| 27 | boolean canPlaceOnFloor,
|
| 28 | boolean canPlaceOnCeiling,
|
| 29 | boolean canPlaceOnWall,
|
| 30 | float chanceOfSpreading,
|
| 31 | HolderSet<Block> canBePlacedOn
|
| 32 | ) implements Feature {
|
| 33 | public static final MapCodec<MultifaceGrowthFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> i.group(
|
| 35 | BuiltInRegistries.BLOCK.byNameCodec().validate(MultifaceGrowthFeature::validateBlock).fieldOf("block").forGetter(MultifaceGrowthFeature::placeBlock),
|
| 36 | Codec.intRange(1, 64).optionalFieldOf("search_range", 10).forGetter(MultifaceGrowthFeature::searchRange),
|
| 37 | Codec.BOOL.optionalFieldOf("can_place_on_floor", false).forGetter(MultifaceGrowthFeature::canPlaceOnFloor),
|
| 38 | Codec.BOOL.optionalFieldOf("can_place_on_ceiling", false).forGetter(MultifaceGrowthFeature::canPlaceOnCeiling),
|
| 39 | Codec.BOOL.optionalFieldOf("can_place_on_wall", false).forGetter(MultifaceGrowthFeature::canPlaceOnWall),
|
| 40 | Codec.floatRange(0.0F, 1.0F).optionalFieldOf("chance_of_spreading", 0.5F).forGetter(MultifaceGrowthFeature::chanceOfSpreading),
|
| 41 | RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("can_be_placed_on").forGetter(MultifaceGrowthFeature::canBePlacedOn)
|
| 42 | )
|
| 43 | .apply(i, MultifaceGrowthFeature::new)
|
| 44 | );
|
| 45 |
|
| 46 | private static DataResult<Block> validateBlock(final Block block) {
|
| 47 | return block instanceof MultifaceSpreadeableBlock multifaceBlock
|
| 48 | ? DataResult.success(multifaceBlock)
|
| 49 | : DataResult.error(() -> "Growth block should be a multiface spreadeable block");
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public MapCodec<MultifaceGrowthFeature> codec() {
|
| 54 | return CODEC;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 59 | if (!isAirOrWater(level.getBlockState(origin))) {
|
| 60 | return false;
|
| 61 | } else if (!(this.placeBlock instanceof MultifaceSpreadeableBlock placerBlock)) {
|
| 62 | return false;
|
| 63 | } else {
|
| 64 | List var13 = this.getShuffledDirections(random);
|
| 65 | if (this.placeGrowthIfPossible(placerBlock, level, origin, level.getBlockState(origin), random, var13)) {
|
| 66 | return true;
|
| 67 | }
|
| 68 |
|
| 69 | BlockPos.MutableBlockPos pos = origin.mutable();
|
| 70 |
|
| 71 | for (Direction searchDirection : var13) {
|
| 72 | pos.set(origin);
|
| 73 | List<Direction> placementDirections = this.getShuffledDirectionsExcept(random, searchDirection.getOpposite());
|
| 74 |
|
| 75 | for (int i = 0; i < this.searchRange; i++) {
|
| 76 | pos.setWithOffset(origin, searchDirection);
|
| 77 | BlockState state = level.getBlockState(pos);
|
| 78 | if (!isAirOrWater(state) && !state.is(this.placeBlock)) {
|
| 79 | break;
|
| 80 | }
|
| 81 |
|
| 82 | if (this.placeGrowthIfPossible(placerBlock, level, pos, state, random, placementDirections)) {
|
| 83 | return true;
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | return false;
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | public boolean placeGrowthIfPossible(
|
| 93 | final MultifaceSpreadeableBlock placerBlock,
|
| 94 | final WorldGenLevel level,
|
| 95 | final BlockPos pos,
|
| 96 | final BlockState oldState,
|
| 97 | final RandomSource random,
|
| 98 | final List<Direction> placementDirections
|
| 99 | ) {
|
| 100 | BlockPos.MutableBlockPos mutable = pos.mutable();
|
| 101 |
|
| 102 | for (Direction placementDirection : placementDirections) {
|
| 103 | BlockState neighbourState = level.getBlockState(mutable.setWithOffset(pos, placementDirection));
|
| 104 | if (neighbourState.is(this.canBePlacedOn)) {
|
| 105 | BlockState newState = placerBlock.getStateForPlacement(oldState, level, pos, placementDirection);
|
| 106 | if (newState == null) {
|
| 107 | return false;
|
| 108 | }
|
| 109 |
|
| 110 | level.setBlockAndUpdate(pos, newState);
|
| 111 | level.getChunk(pos).markPosForPostProcessing(pos);
|
| 112 | if (random.nextFloat() < this.chanceOfSpreading) {
|
| 113 | placerBlock.getSpreader().spreadFromFaceTowardRandomDirection(newState, level, pos, placementDirection, random, true);
|
| 114 | }
|
| 115 |
|
| 116 | return true;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | return false;
|
| 121 | }
|
| 122 |
|
| 123 | private ObjectArrayList<Direction> validDirections() {
|
| 124 | ObjectArrayList<Direction> validDirections = new ObjectArrayList<>(6);
|
| 125 | if (this.canPlaceOnCeiling) {
|
| 126 | validDirections.add(Direction.UP);
|
| 127 | }
|
| 128 |
|
| 129 | if (this.canPlaceOnFloor) {
|
| 130 | validDirections.add(Direction.DOWN);
|
| 131 | }
|
| 132 |
|
| 133 | if (this.canPlaceOnWall) {
|
| 134 | Direction.Plane.HORIZONTAL.forEach(validDirections::add);
|
| 135 | }
|
| 136 |
|
| 137 | return validDirections;
|
| 138 | }
|
| 139 |
|
| 140 | private List<Direction> getShuffledDirectionsExcept(final RandomSource random, final Direction excludeDirection) {
|
| 141 | return Util.toShuffledList(this.validDirections().stream().filter(direction -> direction != excludeDirection), random);
|
| 142 | }
|
| 143 |
|
| 144 | private List<Direction> getShuffledDirections(final RandomSource random) {
|
| 145 | return Util.shuffledCopy(this.validDirections(), random);
|
| 146 | }
|
| 147 |
|
| 148 | private static boolean isAirOrWater(final BlockState state) {
|
| 149 | return state.isAir() || state.is(Blocks.WATER);
|
| 150 | }
|
| 151 | }
|
| 152 |
|