| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.resources.ResourceKey;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.level.BlockGetter;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.LevelReader;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.levelgen.feature.AbstractHugeMushroomFeature;
|
| 17 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 18 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 19 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 20 |
|
| 21 | public class MushroomBlock extends VegetationBlock implements BonemealableBlock {
|
| 22 | private static final int SEARCH_RADIUS = 4;
|
| 23 | private static final int MAX_NEIGHBORS_TO_GROW = 4;
|
| 24 | private static final VoxelShape SHAPE = Block.column(6.0, 0.0, 6.0);
|
| 25 | private final ResourceKey<Feature> feature;
|
| 26 |
|
| 27 | public MushroomBlock(final ResourceKey<Feature> feature, final BlockBehaviour.Properties properties) {
|
| 28 | super(properties);
|
| 29 | this.feature = feature;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 34 | return SHAPE;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected void randomTick(final BlockState state, final ServerLevel level, BlockPos pos, final RandomSource random) {
|
| 39 | if (random.nextInt(25) == 0) {
|
| 40 | if (!this.canSpread(level, pos)) {
|
| 41 | return;
|
| 42 | }
|
| 43 |
|
| 44 | BlockPos offset = pos.offset(random.nextInt(3) - 1, random.nextInt(2) - random.nextInt(2), random.nextInt(3) - 1);
|
| 45 |
|
| 46 | for (int i = 0; i < 4; i++) {
|
| 47 | if (level.isEmptyBlock(offset) && state.canSurvive(level, offset)) {
|
| 48 | pos = offset;
|
| 49 | }
|
| 50 |
|
| 51 | offset = pos.offset(random.nextInt(3) - 1, random.nextInt(2) - random.nextInt(2), random.nextInt(3) - 1);
|
| 52 | }
|
| 53 |
|
| 54 | if (level.isEmptyBlock(offset) && state.canSurvive(level, offset)) {
|
| 55 | level.setBlock(offset, state, 2);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | private boolean canSpread(final ServerLevel level, final BlockPos pos) {
|
| 61 | BlockPos minPos = pos.offset(-4, -1, -4);
|
| 62 | BlockPos maxPos = pos.offset(4, 1, 4);
|
| 63 | return level.findBlocksIn(minPos, maxPos).filterState(state -> state.is(this)).atMostMatched(4);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 68 | return state.isSolidRender();
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 73 | BlockPos belowPos = pos.below();
|
| 74 | BlockState below = level.getBlockState(belowPos);
|
| 75 | return below.is(BlockTags.OVERRIDES_MUSHROOM_LIGHT_REQUIREMENT) ? true : level.getRawBrightness(pos, 0) < 13 && this.mayPlaceOn(below, level, belowPos);
|
| 76 | }
|
| 77 |
|
| 78 | public boolean growMushroom(final ServerLevel level, final BlockPos pos, final BlockState state, final RandomSource random) {
|
| 79 | Optional<? extends Holder<Feature>> feature = level.registryAccess().lookupOrThrow(Registries.FEATURE).get(this.feature);
|
| 80 | if (feature.isEmpty()) {
|
| 81 | return false;
|
| 82 | }
|
| 83 |
|
| 84 | level.removeBlock(pos, false);
|
| 85 | if (feature.get().value().place(level, level.getChunkSource().getGenerator(), random, pos)) {
|
| 86 | return true;
|
| 87 | }
|
| 88 |
|
| 89 | level.setBlockAndUpdate(pos, state);
|
| 90 | return false;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 95 | if (level instanceof ServerLevel serverLevel) {
|
| 96 | Optional<? extends Holder<Feature>> featureHolder = serverLevel.registryAccess().lookupOrThrow(Registries.FEATURE).get(this.feature);
|
| 97 | if (featureHolder.isPresent()) {
|
| 98 | Feature feature = featureHolder.get().value();
|
| 99 | if (feature instanceof AbstractHugeMushroomFeature mushroomFeature) {
|
| 100 | int minHeight = 4 + mushroomFeature.foliageRadius();
|
| 101 | return level.isInsideBuildHeight(pos.above(minHeight));
|
| 102 | } else {
|
| 103 | return false;
|
| 104 | }
|
| 105 | } else {
|
| 106 | return false;
|
| 107 | }
|
| 108 | } else {
|
| 109 | return false;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 115 | return random.nextFloat() < 0.4;
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 120 | this.growMushroom(level, pos, state, random);
|
| 121 | }
|
| 122 | }
|
| 123 |
|