| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.tags.BlockTags;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 11 | import net.minecraft.world.entity.EntityTypes;
|
| 12 | import net.minecraft.world.entity.animal.sniffer.Sniffer;
|
| 13 | import net.minecraft.world.level.BlockGetter;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 18 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 19 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 20 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 21 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 22 | import net.minecraft.world.phys.Vec3;
|
| 23 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 24 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 25 |
|
| 26 | public class SnifferEggBlock extends Block {
|
| 27 | public static final int MAX_HATCH_LEVEL = 2;
|
| 28 | public static final IntegerProperty HATCH = BlockStateProperties.HATCH;
|
| 29 | private static final int REGULAR_HATCH_TIME_TICKS = 24000;
|
| 30 | private static final int BOOSTED_HATCH_TIME_TICKS = 12000;
|
| 31 | private static final int RANDOM_HATCH_OFFSET_TICKS = 300;
|
| 32 | private static final VoxelShape SHAPE = Block.column(14.0, 12.0, 0.0, 16.0);
|
| 33 |
|
| 34 | public SnifferEggBlock(final BlockBehaviour.Properties properties) {
|
| 35 | super(properties);
|
| 36 | this.registerDefaultState(this.stateDefinition.any().setValue(HATCH, 0));
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 41 | builder.add(HATCH);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 46 | return SHAPE;
|
| 47 | }
|
| 48 |
|
| 49 | public int getHatchLevel(final BlockState state) {
|
| 50 | return state.getValue(HATCH);
|
| 51 | }
|
| 52 |
|
| 53 | private boolean isReadyToHatch(final BlockState state) {
|
| 54 | return this.getHatchLevel(state) == 2;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void tick(final BlockState state, final ServerLevel level, final BlockPos position, final RandomSource random) {
|
| 59 | if (!this.isReadyToHatch(state)) {
|
| 60 | level.playSound(null, position, SoundEvents.SNIFFER_EGG_CRACK, SoundSource.BLOCKS, 0.7F, 0.9F + random.nextFloat() * 0.2F);
|
| 61 | level.setBlock(position, state.setValue(HATCH, this.getHatchLevel(state) + 1), 2);
|
| 62 | } else {
|
| 63 | level.playSound(null, position, SoundEvents.SNIFFER_EGG_HATCH, SoundSource.BLOCKS, 0.7F, 0.9F + random.nextFloat() * 0.2F);
|
| 64 | level.destroyBlock(position, false);
|
| 65 | Sniffer sniffer = EntityTypes.SNIFFER.create(level, EntitySpawnReason.BREEDING);
|
| 66 | if (sniffer != null) {
|
| 67 | Vec3 spawnAt = Vec3.atCenterOf(position);
|
| 68 | sniffer.setBaby(true);
|
| 69 | sniffer.snapTo(spawnAt.x(), spawnAt.y(), spawnAt.z(), Mth.wrapDegrees(level.getRandom().nextFloat() * 360.0F), 0.0F);
|
| 70 | level.addFreshEntity(sniffer);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 77 | boolean boosted = hatchBoost(level, pos);
|
| 78 | if (!level.isClientSide() && boosted) {
|
| 79 | level.levelEvent(3009, pos, 0);
|
| 80 | }
|
| 81 |
|
| 82 | int hatchTime = boosted ? 12000 : 24000;
|
| 83 | int progressionTickDelay = hatchTime / 3;
|
| 84 | level.gameEvent(GameEvent.BLOCK_PLACE, pos, GameEvent.Context.of(state));
|
| 85 | level.scheduleTick(pos, this, progressionTickDelay + level.getRandom().nextInt(300));
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 90 | return false;
|
| 91 | }
|
| 92 |
|
| 93 | public static boolean hatchBoost(final BlockGetter level, final BlockPos pos) {
|
| 94 | return level.getBlockState(pos.below()).is(BlockTags.SNIFFER_EGG_HATCH_BOOST);
|
| 95 | }
|
| 96 | }
|
| 97 |
|