minecraft-src/net/minecraft/world/level/block/SculkSensorBlock.java
2025-07-04 01:41:11 +03:00

283 lines
11 KiB
Java

package net.minecraft.world.level.block;
import com.google.common.annotations.VisibleForTesting;
import com.mojang.serialization.MapCodec;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.DustColorTransitionOptions;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.RandomSource;
import net.minecraft.util.valueproviders.ConstantInt;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.entity.SculkSensorBlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.SculkSensorPhase;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.gameevent.vibrations.VibrationSystem;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class SculkSensorBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
public static final MapCodec<SculkSensorBlock> CODEC = simpleCodec(SculkSensorBlock::new);
public static final int ACTIVE_TICKS = 30;
public static final int COOLDOWN_TICKS = 10;
public static final EnumProperty<SculkSensorPhase> PHASE = BlockStateProperties.SCULK_SENSOR_PHASE;
public static final IntegerProperty POWER = BlockStateProperties.POWER;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
protected static final VoxelShape SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0);
private static final float[] RESONANCE_PITCH_BEND = Util.make(new float[16], fs -> {
int[] is = new int[]{0, 0, 2, 4, 6, 7, 9, 10, 12, 14, 15, 18, 19, 21, 22, 24};
for (int i = 0; i < 16; i++) {
fs[i] = NoteBlock.getPitchFromNote(is[i]);
}
});
@Override
public MapCodec<? extends SculkSensorBlock> codec() {
return CODEC;
}
public SculkSensorBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(PHASE, SculkSensorPhase.INACTIVE).setValue(POWER, 0).setValue(WATERLOGGED, false));
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockPos blockPos = context.getClickedPos();
FluidState fluidState = context.getLevel().getFluidState(blockPos);
return this.defaultBlockState().setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
@Override
protected FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (getPhase(state) != SculkSensorPhase.ACTIVE) {
if (getPhase(state) == SculkSensorPhase.COOLDOWN) {
level.setBlock(pos, state.setValue(PHASE, SculkSensorPhase.INACTIVE), 3);
if (!(Boolean)state.getValue(WATERLOGGED)) {
level.playSound(null, pos, SoundEvents.SCULK_CLICKING_STOP, SoundSource.BLOCKS, 1.0F, level.random.nextFloat() * 0.2F + 0.8F);
}
}
} else {
deactivate(level, pos, state);
}
}
@Override
public void stepOn(Level level, BlockPos pos, BlockState state, Entity entity) {
if (!level.isClientSide()
&& canActivate(state)
&& entity.getType() != EntityType.WARDEN
&& level.getBlockEntity(pos) instanceof SculkSensorBlockEntity sculkSensorBlockEntity
&& level instanceof ServerLevel serverLevel
&& sculkSensorBlockEntity.getVibrationUser().canReceiveVibration(serverLevel, pos, GameEvent.STEP, GameEvent.Context.of(state))) {
sculkSensorBlockEntity.getListener().forceScheduleVibration(serverLevel, GameEvent.STEP, GameEvent.Context.of(entity), entity.position());
}
super.stepOn(level, pos, state, entity);
}
@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
if (!level.isClientSide() && !state.is(oldState.getBlock())) {
if ((Integer)state.getValue(POWER) > 0 && !level.getBlockTicks().hasScheduledTick(pos, this)) {
level.setBlock(pos, state.setValue(POWER, 0), 18);
}
}
}
@Override
protected void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) {
if (!state.is(newState.getBlock())) {
super.onRemove(state, level, pos, newState, movedByPiston);
if (getPhase(state) == SculkSensorPhase.ACTIVE) {
updateNeighbours(level, pos, state);
}
}
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if ((Boolean)state.getValue(WATERLOGGED)) {
level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
private static void updateNeighbours(Level level, BlockPos pos, BlockState state) {
Block block = state.getBlock();
level.updateNeighborsAt(pos, block);
level.updateNeighborsAt(pos.below(), block);
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new SculkSensorBlockEntity(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> blockEntityType) {
return !level.isClientSide
? createTickerHelper(
blockEntityType,
BlockEntityType.SCULK_SENSOR,
(levelx, blockPos, blockState, sculkSensorBlockEntity) -> VibrationSystem.Ticker.tick(
levelx, sculkSensorBlockEntity.getVibrationData(), sculkSensorBlockEntity.getVibrationUser()
)
)
: null;
}
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPE;
}
@Override
protected boolean isSignalSource(BlockState state) {
return true;
}
@Override
protected int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
return (Integer)state.getValue(POWER);
}
@Override
public int getDirectSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
return direction == Direction.UP ? state.getSignal(level, pos, direction) : 0;
}
public static SculkSensorPhase getPhase(BlockState state) {
return state.getValue(PHASE);
}
public static boolean canActivate(BlockState state) {
return getPhase(state) == SculkSensorPhase.INACTIVE;
}
public static void deactivate(Level level, BlockPos pos, BlockState state) {
level.setBlock(pos, state.setValue(PHASE, SculkSensorPhase.COOLDOWN).setValue(POWER, 0), 3);
level.scheduleTick(pos, state.getBlock(), 10);
updateNeighbours(level, pos, state);
}
@VisibleForTesting
public int getActiveTicks() {
return 30;
}
public void activate(@Nullable Entity entity, Level level, BlockPos pos, BlockState state, int power, int frequency) {
level.setBlock(pos, state.setValue(PHASE, SculkSensorPhase.ACTIVE).setValue(POWER, power), 3);
level.scheduleTick(pos, state.getBlock(), this.getActiveTicks());
updateNeighbours(level, pos, state);
tryResonateVibration(entity, level, pos, frequency);
level.gameEvent(entity, GameEvent.SCULK_SENSOR_TENDRILS_CLICKING, pos);
if (!(Boolean)state.getValue(WATERLOGGED)) {
level.playSound(
null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.SCULK_CLICKING, SoundSource.BLOCKS, 1.0F, level.random.nextFloat() * 0.2F + 0.8F
);
}
}
public static void tryResonateVibration(@Nullable Entity entity, Level level, BlockPos pos, int frequency) {
for (Direction direction : Direction.values()) {
BlockPos blockPos = pos.relative(direction);
BlockState blockState = level.getBlockState(blockPos);
if (blockState.is(BlockTags.VIBRATION_RESONATORS)) {
level.gameEvent(VibrationSystem.getResonanceEventByFrequency(frequency), blockPos, GameEvent.Context.of(entity, blockState));
float f = RESONANCE_PITCH_BEND[frequency];
level.playSound(null, blockPos, SoundEvents.AMETHYST_BLOCK_RESONATE, SoundSource.BLOCKS, 1.0F, f);
}
}
}
@Override
public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) {
if (getPhase(state) == SculkSensorPhase.ACTIVE) {
Direction direction = Direction.getRandom(random);
if (direction != Direction.UP && direction != Direction.DOWN) {
double d = pos.getX() + 0.5 + (direction.getStepX() == 0 ? 0.5 - random.nextDouble() : direction.getStepX() * 0.6);
double e = pos.getY() + 0.25;
double f = pos.getZ() + 0.5 + (direction.getStepZ() == 0 ? 0.5 - random.nextDouble() : direction.getStepZ() * 0.6);
double g = random.nextFloat() * 0.04;
level.addParticle(DustColorTransitionOptions.SCULK_TO_REDSTONE, d, e, f, 0.0, g, 0.0);
}
}
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(PHASE, POWER, WATERLOGGED);
}
@Override
protected boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos) {
if (level.getBlockEntity(pos) instanceof SculkSensorBlockEntity sculkSensorBlockEntity) {
return getPhase(state) == SculkSensorPhase.ACTIVE ? sculkSensorBlockEntity.getLastVibrationFrequency() : 0;
} else {
return 0;
}
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
@Override
protected boolean useShapeForLightOcclusion(BlockState state) {
return true;
}
@Override
protected void spawnAfterBreak(BlockState state, ServerLevel level, BlockPos pos, ItemStack stack, boolean dropExperience) {
super.spawnAfterBreak(state, level, pos, stack, dropExperience);
if (dropExperience) {
this.tryDropExperience(level, pos, stack, ConstantInt.of(5));
}
}
}