| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.tags.FluidTags;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 15 | import net.minecraft.world.entity.EntityTypes;
|
| 16 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 17 | import net.minecraft.world.entity.animal.frog.Tadpole;
|
| 18 | import net.minecraft.world.level.BlockGetter;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.LevelReader;
|
| 21 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 22 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.material.FluidState;
|
| 25 | import net.minecraft.world.level.material.Fluids;
|
| 26 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 27 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 28 |
|
| 29 | public class FrogspawnBlock extends Block {
|
| 30 | private static final int MIN_TADPOLES_SPAWN = 2;
|
| 31 | private static final int MAX_TADPOLES_SPAWN = 5;
|
| 32 | private static final int DEFAULT_MIN_HATCH_TICK_DELAY = 3600;
|
| 33 | private static final int DEFAULT_MAX_HATCH_TICK_DELAY = 12000;
|
| 34 | private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 1.5);
|
| 35 | private static int minHatchTickDelay = 3600;
|
| 36 | private static int maxHatchTickDelay = 12000;
|
| 37 |
|
| 38 | public FrogspawnBlock(final BlockBehaviour.Properties properties) {
|
| 39 | super(properties);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 44 | return SHAPE;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 49 | return mayPlaceOn(level, pos.below());
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 54 | level.scheduleTick(pos, this, getFrogspawnHatchDelay(level.getRandom()));
|
| 55 | }
|
| 56 |
|
| 57 | private static int getFrogspawnHatchDelay(final RandomSource random) {
|
| 58 | return random.nextInt(minHatchTickDelay, maxHatchTickDelay);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected BlockState updateShape(
|
| 63 | final BlockState state,
|
| 64 | final LevelReader level,
|
| 65 | final ScheduledTickAccess ticks,
|
| 66 | final BlockPos pos,
|
| 67 | final Direction directionToNeighbour,
|
| 68 | final BlockPos neighbourPos,
|
| 69 | final BlockState neighbourState,
|
| 70 | final RandomSource random
|
| 71 | ) {
|
| 72 | return !this.canSurvive(state, level, pos)
|
| 73 | ? Blocks.AIR.defaultBlockState()
|
| 74 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 79 | if (!this.canSurvive(state, level, pos)) {
|
| 80 | this.destroyBlock(level, pos);
|
| 81 | } else {
|
| 82 | this.hatchFrogspawn(level, pos, random);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected void entityInside(
|
| 88 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 89 | ) {
|
| 90 | if (entity.is(EntityTypes.FALLING_BLOCK)) {
|
| 91 | this.destroyBlock(level, pos);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | private static boolean mayPlaceOn(final BlockGetter level, final BlockPos pos) {
|
| 96 | FluidState fluidState = level.getFluidState(pos);
|
| 97 | FluidState fluidAbove = level.getFluidState(pos.above());
|
| 98 | return (fluidState.is(FluidTags.SUPPORTS_FROGSPAWN) || level.getBlockState(pos).is(BlockTags.SUPPORTS_FROGSPAWN)) && fluidAbove.is(Fluids.EMPTY);
|
| 99 | }
|
| 100 |
|
| 101 | private void hatchFrogspawn(final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 102 | this.destroyBlock(level, pos);
|
| 103 | level.playSound(null, pos, SoundEvents.FROGSPAWN_HATCH, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 104 | this.spawnTadpoles(level, pos, random);
|
| 105 | }
|
| 106 |
|
| 107 | private void destroyBlock(final Level level, final BlockPos pos) {
|
| 108 | level.destroyBlock(pos, false);
|
| 109 | }
|
| 110 |
|
| 111 | private void spawnTadpoles(final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 112 | int tadpoleAmount = random.nextInt(2, 6);
|
| 113 |
|
| 114 | for (int i = 1; i <= tadpoleAmount; i++) {
|
| 115 | Tadpole tadpole = EntityTypes.TADPOLE.create(level, EntitySpawnReason.BREEDING);
|
| 116 | if (tadpole != null) {
|
| 117 | double xPos = pos.getX() + this.getRandomTadpolePositionOffset(random);
|
| 118 | double zPos = pos.getZ() + this.getRandomTadpolePositionOffset(random);
|
| 119 | int yRot = random.nextInt(1, 361);
|
| 120 | tadpole.snapTo(xPos, pos.getY() - 0.5, zPos, yRot, 0.0F);
|
| 121 | tadpole.setPersistenceRequired();
|
| 122 | level.addFreshEntity(tadpole);
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | private double getRandomTadpolePositionOffset(final RandomSource random) {
|
| 128 | double tadpoleHitboxCenter = 0.2F;
|
| 129 | return Mth.clamp(random.nextDouble(), 0.2F, 0.7999999970197678);
|
| 130 | }
|
| 131 |
|
| 132 | @VisibleForTesting
|
| 133 | public static void setHatchDelay(final int minDelay, final int maxDelay) {
|
| 134 | minHatchTickDelay = minDelay;
|
| 135 | maxHatchTickDelay = maxDelay;
|
| 136 | }
|
| 137 |
|
| 138 | @VisibleForTesting
|
| 139 | public static void setDefaultHatchDelay() {
|
| 140 | minHatchTickDelay = 3600;
|
| 141 | maxHatchTickDelay = 12000;
|
| 142 | }
|
| 143 | }
|
| 144 |
|