| 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.core.particles.DustColorTransitionOptions;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.tags.BlockTags;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import net.minecraft.util.valueproviders.ConstantInt;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.EntityTypes;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 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.entity.BlockEntity;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 25 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 26 | import net.minecraft.world.level.block.entity.SculkSensorBlockEntity;
|
| 27 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 32 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 33 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 34 | import net.minecraft.world.level.block.state.properties.SculkSensorPhase;
|
| 35 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 36 | import net.minecraft.world.level.gameevent.vibrations.VibrationSystem;
|
| 37 | import net.minecraft.world.level.material.FluidState;
|
| 38 | import net.minecraft.world.level.material.Fluids;
|
| 39 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 40 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 41 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 42 | import org.jspecify.annotations.Nullable;
|
| 43 |
|
| 44 | public class SculkSensorBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
|
| 45 | public static final int ACTIVE_TICKS = 30;
|
| 46 | public static final int COOLDOWN_TICKS = 10;
|
| 47 | public static final EnumProperty<SculkSensorPhase> PHASE = BlockStateProperties.SCULK_SENSOR_PHASE;
|
| 48 | public static final IntegerProperty POWER = BlockStateProperties.POWER;
|
| 49 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 50 | private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 8.0);
|
| 51 | private static final float[] RESONANCE_PITCH_BEND = Util.make(new float[16], arr -> {
|
| 52 | int[] toneMap = new int[]{0, 0, 2, 4, 6, 7, 9, 10, 12, 14, 15, 18, 19, 21, 22, 24};
|
| 53 |
|
| 54 | for (int i = 0; i < 16; i++) {
|
| 55 | arr[i] = NoteBlock.getPitchFromNote(toneMap[i]);
|
| 56 | }
|
| 57 | });
|
| 58 |
|
| 59 | public SculkSensorBlock(final BlockBehaviour.Properties properties) {
|
| 60 | super(properties);
|
| 61 | this.registerDefaultState(this.stateDefinition.any().setValue(PHASE, SculkSensorPhase.INACTIVE).setValue(POWER, 0).setValue(WATERLOGGED, false));
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 66 | BlockPos pos = context.getClickedPos();
|
| 67 | FluidState replacedFluidState = context.getLevel().getFluidState(pos);
|
| 68 | return this.defaultBlockState().setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected FluidState getFluidState(final BlockState state) {
|
| 73 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 78 | if (getPhase(state) != SculkSensorPhase.ACTIVE) {
|
| 79 | if (getPhase(state) == SculkSensorPhase.COOLDOWN) {
|
| 80 | level.setBlockAndUpdate(pos, state.setValue(PHASE, SculkSensorPhase.INACTIVE));
|
| 81 | if (!state.getValue(WATERLOGGED)) {
|
| 82 | level.playSound(null, pos, SoundEvents.SCULK_CLICKING_STOP, SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.2F + 0.8F);
|
| 83 | }
|
| 84 | }
|
| 85 | } else {
|
| 86 | deactivate(level, pos, state);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void stepOn(final Level level, final BlockPos pos, final BlockState onState, final Entity entity) {
|
| 92 | if (!level.isClientSide()
|
| 93 | && canActivate(onState)
|
| 94 | && !entity.is(EntityTypes.WARDEN)
|
| 95 | && level.getBlockEntity(pos) instanceof SculkSensorBlockEntity sculkSensor
|
| 96 | && level instanceof ServerLevel serverLevel
|
| 97 | && sculkSensor.getVibrationUser().canReceiveVibration(serverLevel, pos, GameEvent.STEP, GameEvent.Context.of(onState))) {
|
| 98 | sculkSensor.getListener().forceScheduleVibration(serverLevel, GameEvent.STEP, GameEvent.Context.of(entity), entity.position());
|
| 99 | }
|
| 100 |
|
| 101 | super.stepOn(level, pos, onState, entity);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 106 | if (!level.isClientSide() && !state.is(oldState.getBlock())) {
|
| 107 | if (state.getValue(POWER) > 0 && !level.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 108 | level.setBlock(pos, state.setValue(POWER, 0), 18);
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 115 | if (getPhase(state) == SculkSensorPhase.ACTIVE) {
|
| 116 | updateNeighbours(level, pos, state);
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | protected BlockState updateShape(
|
| 122 | final BlockState state,
|
| 123 | final LevelReader level,
|
| 124 | final ScheduledTickAccess ticks,
|
| 125 | final BlockPos pos,
|
| 126 | final Direction directionToNeighbour,
|
| 127 | final BlockPos neighbourPos,
|
| 128 | final BlockState neighbourState,
|
| 129 | final RandomSource random
|
| 130 | ) {
|
| 131 | if (state.getValue(WATERLOGGED)) {
|
| 132 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 133 | }
|
| 134 |
|
| 135 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 136 | }
|
| 137 |
|
| 138 | private static void updateNeighbours(final Level level, final BlockPos pos, final BlockState state) {
|
| 139 | Block block = state.getBlock();
|
| 140 | level.updateNeighborsAt(pos, block);
|
| 141 | level.updateNeighborsAt(pos.below(), block);
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 146 | return new SculkSensorBlockEntity(worldPosition, blockState);
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 151 | return !level.isClientSide()
|
| 152 | ? createTickerHelper(
|
| 153 | type,
|
| 154 | BlockEntityTypes.SCULK_SENSOR,
|
| 155 | (innerLevel, pos, state, entity) -> VibrationSystem.Ticker.tick(innerLevel, entity.getVibrationData(), entity.getVibrationUser())
|
| 156 | )
|
| 157 | : null;
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 162 | return SHAPE;
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | protected boolean isSignalSource(final BlockState state) {
|
| 167 | return true;
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 172 | return state.getValue(POWER);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 177 | return direction == Direction.UP ? state.getSignal(level, pos, direction) : 0;
|
| 178 | }
|
| 179 |
|
| 180 | public static SculkSensorPhase getPhase(final BlockState state) {
|
| 181 | return state.getValue(PHASE);
|
| 182 | }
|
| 183 |
|
| 184 | public static boolean canActivate(final BlockState state) {
|
| 185 | return getPhase(state) == SculkSensorPhase.INACTIVE;
|
| 186 | }
|
| 187 |
|
| 188 | public static void deactivate(final Level level, final BlockPos pos, final BlockState state) {
|
| 189 | level.setBlockAndUpdate(pos, state.setValue(PHASE, SculkSensorPhase.COOLDOWN).setValue(POWER, 0));
|
| 190 | level.scheduleTick(pos, state.getBlock(), 10);
|
| 191 | updateNeighbours(level, pos, state);
|
| 192 | }
|
| 193 |
|
| 194 | @VisibleForTesting
|
| 195 | public int getActiveTicks() {
|
| 196 | return 30;
|
| 197 | }
|
| 198 |
|
| 199 | public void activate(
|
| 200 | final @Nullable Entity sourceEntity, final Level level, final BlockPos pos, final BlockState state, final int calculatedPower, final int vibrationFrequency
|
| 201 | ) {
|
| 202 | level.setBlockAndUpdate(pos, state.setValue(PHASE, SculkSensorPhase.ACTIVE).setValue(POWER, calculatedPower));
|
| 203 | level.scheduleTick(pos, state.getBlock(), this.getActiveTicks());
|
| 204 | updateNeighbours(level, pos, state);
|
| 205 | tryResonateVibration(sourceEntity, level, pos, vibrationFrequency);
|
| 206 | level.gameEvent(sourceEntity, GameEvent.SCULK_SENSOR_TENDRILS_CLICKING, pos);
|
| 207 | if (!state.getValue(WATERLOGGED)) {
|
| 208 | level.playSound(
|
| 209 | null,
|
| 210 | pos.getX() + 0.5,
|
| 211 | pos.getY() + 0.5,
|
| 212 | pos.getZ() + 0.5,
|
| 213 | SoundEvents.SCULK_CLICKING,
|
| 214 | SoundSource.BLOCKS,
|
| 215 | 1.0F,
|
| 216 | level.getRandom().nextFloat() * 0.2F + 0.8F
|
| 217 | );
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | public static void tryResonateVibration(final @Nullable Entity sourceEntity, final Level level, final BlockPos pos, final int vibrationFrequency) {
|
| 222 | for (Direction direction : Direction.values()) {
|
| 223 | BlockPos relativePos = pos.relative(direction);
|
| 224 | BlockState blockState = level.getBlockState(relativePos);
|
| 225 | if (blockState.is(BlockTags.VIBRATION_RESONATORS)) {
|
| 226 | level.gameEvent(VibrationSystem.getResonanceEventByFrequency(vibrationFrequency), relativePos, GameEvent.Context.of(sourceEntity, blockState));
|
| 227 | float pitch = RESONANCE_PITCH_BEND[vibrationFrequency];
|
| 228 | level.playSound(null, relativePos, SoundEvents.AMETHYST_BLOCK_RESONATE, SoundSource.BLOCKS, 1.0F, pitch);
|
| 229 | }
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 235 | if (getPhase(state) == SculkSensorPhase.ACTIVE) {
|
| 236 | Direction dir = Direction.getRandom(random);
|
| 237 | if (dir != Direction.UP && dir != Direction.DOWN) {
|
| 238 | double x = pos.getX() + 0.5 + (dir.getStepX() == 0 ? 0.5 - random.nextDouble() : dir.getStepX() * 0.6);
|
| 239 | double y = pos.getY() + 0.25;
|
| 240 | double z = pos.getZ() + 0.5 + (dir.getStepZ() == 0 ? 0.5 - random.nextDouble() : dir.getStepZ() * 0.6);
|
| 241 | double ya = random.nextFloat() * 0.04;
|
| 242 | level.addParticle(DustColorTransitionOptions.SCULK_TO_REDSTONE, x, y, z, 0.0, ya, 0.0);
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 249 | builder.add(PHASE, POWER, WATERLOGGED);
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 254 | return true;
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 259 | if (level.getBlockEntity(pos) instanceof SculkSensorBlockEntity sculk) {
|
| 260 | return getPhase(state) == SculkSensorPhase.ACTIVE ? sculk.getLastVibrationFrequency() : 0;
|
| 261 | } else {
|
| 262 | return 0;
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | @Override
|
| 267 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 268 | return false;
|
| 269 | }
|
| 270 |
|
| 271 | @Override
|
| 272 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 273 | return true;
|
| 274 | }
|
| 275 |
|
| 276 | @Override
|
| 277 | protected void spawnAfterBreak(final BlockState state, final ServerLevel level, final BlockPos pos, final ItemStack tool, final boolean dropExperience) {
|
| 278 | super.spawnAfterBreak(state, level, pos, tool, dropExperience);
|
| 279 | if (dropExperience) {
|
| 280 | this.tryDropExperience(level, pos, tool, ConstantInt.of(5));
|
| 281 | }
|
| 282 | }
|
| 283 | }
|
| 284 |
|