| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 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.util.RandomSource;
|
| 11 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 12 | import net.minecraft.world.entity.EntityTypes;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.animal.happyghast.HappyGhast;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.LevelAccessor;
|
| 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.block.state.StateDefinition;
|
| 25 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 26 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 27 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 28 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 29 | import net.minecraft.world.level.material.FluidState;
|
| 30 | import net.minecraft.world.level.material.Fluids;
|
| 31 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 32 | import net.minecraft.world.phys.Vec3;
|
| 33 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 34 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 |
|
| 37 | public class DriedGhastBlock extends HorizontalDirectionalBlock implements SimpleWaterloggedBlock {
|
| 38 | public static final int MAX_HYDRATION_LEVEL = 3;
|
| 39 | public static final IntegerProperty HYDRATION_LEVEL = BlockStateProperties.DRIED_GHAST_HYDRATION_LEVELS;
|
| 40 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 41 | public static final int HYDRATION_TICK_DELAY = 5000;
|
| 42 | private static final VoxelShape SHAPE = Block.column(10.0, 10.0, 0.0, 10.0);
|
| 43 |
|
| 44 | public DriedGhastBlock(final BlockBehaviour.Properties properties) {
|
| 45 | super(properties);
|
| 46 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(HYDRATION_LEVEL, 0).setValue(WATERLOGGED, false));
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 51 | builder.add(FACING, HYDRATION_LEVEL, WATERLOGGED);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected BlockState updateShape(
|
| 56 | final BlockState state,
|
| 57 | final LevelReader level,
|
| 58 | final ScheduledTickAccess ticks,
|
| 59 | final BlockPos pos,
|
| 60 | final Direction directionToNeighbour,
|
| 61 | final BlockPos neighbourPos,
|
| 62 | final BlockState neighbourState,
|
| 63 | final RandomSource random
|
| 64 | ) {
|
| 65 | if (state.getValue(WATERLOGGED)) {
|
| 66 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 67 | }
|
| 68 |
|
| 69 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 74 | return SHAPE;
|
| 75 | }
|
| 76 |
|
| 77 | public int getHydrationLevel(final BlockState state) {
|
| 78 | return state.getValue(HYDRATION_LEVEL);
|
| 79 | }
|
| 80 |
|
| 81 | private boolean isReadyToSpawn(final BlockState state) {
|
| 82 | return this.getHydrationLevel(state) == 3;
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos position, final RandomSource random) {
|
| 87 | if (state.getValue(WATERLOGGED)) {
|
| 88 | this.tickWaterlogged(state, level, position, random);
|
| 89 | } else {
|
| 90 | int hydrationLevel = this.getHydrationLevel(state);
|
| 91 | if (hydrationLevel > 0) {
|
| 92 | level.setBlock(position, state.setValue(HYDRATION_LEVEL, hydrationLevel - 1), 2);
|
| 93 | level.gameEvent(GameEvent.BLOCK_CHANGE, position, GameEvent.Context.of(state));
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private void tickWaterlogged(final BlockState state, final ServerLevel level, final BlockPos position, final RandomSource random) {
|
| 99 | if (!this.isReadyToSpawn(state)) {
|
| 100 | level.playSound(null, position, SoundEvents.DRIED_GHAST_TRANSITION, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 101 | level.setBlock(position, state.setValue(HYDRATION_LEVEL, this.getHydrationLevel(state) + 1), 2);
|
| 102 | level.gameEvent(GameEvent.BLOCK_CHANGE, position, GameEvent.Context.of(state));
|
| 103 | } else {
|
| 104 | this.spawnGhastling(level, position, state);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private void spawnGhastling(final ServerLevel level, final BlockPos position, final BlockState state) {
|
| 109 | level.removeBlock(position, false);
|
| 110 | HappyGhast ghastling = EntityTypes.HAPPY_GHAST.create(level, EntitySpawnReason.BREEDING);
|
| 111 | if (ghastling != null) {
|
| 112 | Vec3 spawnAt = Vec3.atBottomCenterOf(position);
|
| 113 | ghastling.setBaby(true);
|
| 114 | float blockRotation = Direction.getYRot(state.getValue(FACING));
|
| 115 | ghastling.setYHeadRot(blockRotation);
|
| 116 | ghastling.snapTo(spawnAt.x(), spawnAt.y(), spawnAt.z(), blockRotation, 0.0F);
|
| 117 | level.addFreshEntity(ghastling);
|
| 118 | level.playSound(null, ghastling, SoundEvents.GHASTLING_SPAWN, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 124 | double x = pos.getX() + 0.5;
|
| 125 | double y = pos.getY() + 0.5;
|
| 126 | double z = pos.getZ() + 0.5;
|
| 127 | if (!state.getValue(WATERLOGGED)) {
|
| 128 | if (random.nextInt(40) == 0 && level.getBlockState(pos.below()).is(BlockTags.TRIGGERS_AMBIENT_DRIED_GHAST_BLOCK_SOUNDS)) {
|
| 129 | level.playLocalSound(x, y, z, SoundEvents.DRIED_GHAST_AMBIENT, SoundSource.BLOCKS, 1.0F, 1.0F, false);
|
| 130 | }
|
| 131 |
|
| 132 | if (random.nextInt(6) == 0) {
|
| 133 | level.addParticle(ParticleTypes.WHITE_SMOKE, x, y, z, 0.0, 0.02, 0.0);
|
| 134 | }
|
| 135 | } else {
|
| 136 | if (random.nextInt(40) == 0) {
|
| 137 | level.playLocalSound(x, y, z, SoundEvents.DRIED_GHAST_AMBIENT_WATER, SoundSource.BLOCKS, 1.0F, 1.0F, false);
|
| 138 | }
|
| 139 |
|
| 140 | if (random.nextInt(6) == 0) {
|
| 141 | level.addParticle(
|
| 142 | ParticleTypes.HAPPY_VILLAGER,
|
| 143 | x + (random.nextFloat() * 2.0F - 1.0F) / 3.0F,
|
| 144 | y + 0.4,
|
| 145 | z + (random.nextFloat() * 2.0F - 1.0F) / 3.0F,
|
| 146 | 0.0,
|
| 147 | random.nextFloat(),
|
| 148 | 0.0
|
| 149 | );
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 156 | if ((state.getValue(WATERLOGGED) || state.getValue(HYDRATION_LEVEL) > 0) && !level.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 157 | level.scheduleTick(pos, this, 5000);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 163 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 164 | boolean isWaterSource = replacedFluidState.is(Fluids.WATER);
|
| 165 | return super.getStateForPlacement(context).setValue(WATERLOGGED, isWaterSource).setValue(FACING, context.getHorizontalDirection().getOpposite());
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | protected FluidState getFluidState(final BlockState state) {
|
| 170 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public boolean placeLiquid(final LevelAccessor level, final BlockPos pos, final BlockState state, final FluidState fluidState) {
|
| 175 | if (!state.getValue(BlockStateProperties.WATERLOGGED) && fluidState.is(Fluids.WATER)) {
|
| 176 | if (!level.isClientSide()) {
|
| 177 | level.setBlockAndUpdate(pos, state.setValue(BlockStateProperties.WATERLOGGED, true));
|
| 178 | level.scheduleTick(pos, fluidState.getType(), fluidState.getType().getTickDelay(level));
|
| 179 | level.playSound(null, pos, SoundEvents.DRIED_GHAST_PLACE_IN_WATER, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 180 | }
|
| 181 |
|
| 182 | return true;
|
| 183 | } else {
|
| 184 | return false;
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 190 | super.setPlacedBy(level, pos, state, by, itemStack);
|
| 191 | level.playSound(
|
| 192 | null, pos, state.getValue(WATERLOGGED) ? SoundEvents.DRIED_GHAST_PLACE_IN_WATER : SoundEvents.DRIED_GHAST_PLACE, SoundSource.BLOCKS, 1.0F, 1.0F
|
| 193 | );
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 198 | return false;
|
| 199 | }
|
| 200 | }
|
| 201 |
|