159 lines
5.4 KiB
Java
159 lines
5.4 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.EntitySelector;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.properties.BlockSetType;
|
|
import net.minecraft.world.level.gameevent.GameEvent;
|
|
import net.minecraft.world.phys.AABB;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public abstract class BasePressurePlateBlock extends Block {
|
|
protected static final VoxelShape PRESSED_AABB = Block.box(1.0, 0.0, 1.0, 15.0, 0.5, 15.0);
|
|
protected static final VoxelShape AABB = Block.box(1.0, 0.0, 1.0, 15.0, 1.0, 15.0);
|
|
protected static final AABB TOUCH_AABB = new AABB(0.0625, 0.0, 0.0625, 0.9375, 0.25, 0.9375);
|
|
protected final BlockSetType type;
|
|
|
|
protected BasePressurePlateBlock(BlockBehaviour.Properties properties, BlockSetType type) {
|
|
super(properties.sound(type.soundType()));
|
|
this.type = type;
|
|
}
|
|
|
|
@Override
|
|
protected abstract MapCodec<? extends BasePressurePlateBlock> codec();
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return this.getSignalForState(state) > 0 ? PRESSED_AABB : AABB;
|
|
}
|
|
|
|
protected int getPressedTime() {
|
|
return 20;
|
|
}
|
|
|
|
@Override
|
|
public boolean isPossibleToRespawnInThis(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
|
|
return direction == Direction.DOWN && !state.canSurvive(level, pos)
|
|
? Blocks.AIR.defaultBlockState()
|
|
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
|
|
}
|
|
|
|
@Override
|
|
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
|
|
BlockPos blockPos = pos.below();
|
|
return canSupportRigidBlock(level, blockPos) || canSupportCenter(level, blockPos, Direction.UP);
|
|
}
|
|
|
|
@Override
|
|
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
int i = this.getSignalForState(state);
|
|
if (i > 0) {
|
|
this.checkPressed(null, level, pos, state, i);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void entityInside(BlockState state, Level level, BlockPos pos, Entity entity) {
|
|
if (!level.isClientSide) {
|
|
int i = this.getSignalForState(state);
|
|
if (i == 0) {
|
|
this.checkPressed(entity, level, pos, state, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void checkPressed(@Nullable Entity entity, Level level, BlockPos pos, BlockState state, int currentSignal) {
|
|
int i = this.getSignalStrength(level, pos);
|
|
boolean bl = currentSignal > 0;
|
|
boolean bl2 = i > 0;
|
|
if (currentSignal != i) {
|
|
BlockState blockState = this.setSignalForState(state, i);
|
|
level.setBlock(pos, blockState, 2);
|
|
this.updateNeighbours(level, pos);
|
|
level.setBlocksDirty(pos, state, blockState);
|
|
}
|
|
|
|
if (!bl2 && bl) {
|
|
level.playSound(null, pos, this.type.pressurePlateClickOff(), SoundSource.BLOCKS);
|
|
level.gameEvent(entity, GameEvent.BLOCK_DEACTIVATE, pos);
|
|
} else if (bl2 && !bl) {
|
|
level.playSound(null, pos, this.type.pressurePlateClickOn(), SoundSource.BLOCKS);
|
|
level.gameEvent(entity, GameEvent.BLOCK_ACTIVATE, pos);
|
|
}
|
|
|
|
if (bl2) {
|
|
level.scheduleTick(new BlockPos(pos), this, this.getPressedTime());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) {
|
|
if (!movedByPiston && !state.is(newState.getBlock())) {
|
|
if (this.getSignalForState(state) > 0) {
|
|
this.updateNeighbours(level, pos);
|
|
}
|
|
|
|
super.onRemove(state, level, pos, newState, movedByPiston);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notify block and block below of changes
|
|
*/
|
|
protected void updateNeighbours(Level level, BlockPos pos) {
|
|
level.updateNeighborsAt(pos, this);
|
|
level.updateNeighborsAt(pos.below(), this);
|
|
}
|
|
|
|
@Override
|
|
protected int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
|
|
return this.getSignalForState(state);
|
|
}
|
|
|
|
@Override
|
|
protected int getDirectSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
|
|
return direction == Direction.UP ? this.getSignalForState(state) : 0;
|
|
}
|
|
|
|
@Override
|
|
protected boolean isSignalSource(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
protected static int getEntityCount(Level level, AABB box, Class<? extends Entity> entityClass) {
|
|
return level.getEntitiesOfClass(entityClass, box, EntitySelector.NO_SPECTATORS.and(entity -> !entity.isIgnoringBlockTriggers())).size();
|
|
}
|
|
|
|
/**
|
|
* Calculates what the signal strength of a pressure plate at the given location should be.
|
|
*/
|
|
protected abstract int getSignalStrength(Level level, BlockPos pos);
|
|
|
|
/**
|
|
* Returns the signal encoded in the given block state.
|
|
*/
|
|
protected abstract int getSignalForState(BlockState state);
|
|
|
|
/**
|
|
* Returns the block state that encodes the given signal.
|
|
*/
|
|
protected abstract BlockState setSignalForState(BlockState state, int signal);
|
|
}
|