minecraft-source/src/net/minecraft/world/level/block/BasePressurePlateBlock.java

152 lines
5.3 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.shapes.VoxelShape;
public abstract class BasePressurePlateBlock extends Block {
protected static final VoxelShape PRESSED_AABB;
protected static final VoxelShape AABB;
protected static final AABB TOUCH_AABB;
protected BasePressurePlateBlock(final Properties c) {
super(c);
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
return (this.getSignalForState(byg) > 0) ? BasePressurePlateBlock.PRESSED_AABB : BasePressurePlateBlock.AABB;
}
@Override
public int getTickDelay(final LevelReader bjw) {
return 20;
}
@Override
public boolean isPossibleToRespawnInThis() {
return true;
}
@Override
public BlockState updateShape(final BlockState byg1, final Direction fp, final BlockState byg3, final LevelAccessor bju, final BlockPos fk5, final BlockPos fk6) {
if (fp == Direction.DOWN && !byg1.canSurvive(bju, fk5)) {
return Blocks.AIR.defaultBlockState();
}
return super.updateShape(byg1, fp, byg3, bju, fk5, fk6);
}
@Override
public boolean canSurvive(final BlockState byg, final LevelReader bjw, final BlockPos fk) {
final BlockPos fk2 = fk.below();
return Block.canSupportRigidBlock(bjw, fk2) || Block.canSupportCenter(bjw, fk2, Direction.UP);
}
@Override
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
final int integer6 = this.getSignalForState(byg);
if (integer6 > 0) {
this.checkPressed(xd, fk, byg, integer6);
}
}
@Override
public void entityInside(final BlockState byg, final Level bjt, final BlockPos fk, final Entity akn) {
if (bjt.isClientSide) {
return;
}
final int integer6 = this.getSignalForState(byg);
if (integer6 == 0) {
this.checkPressed(bjt, fk, byg, integer6);
}
}
protected void checkPressed(final Level bjt, final BlockPos fk, final BlockState byg, final int integer) {
final int integer2 = this.getSignalStrength(bjt, fk);
final boolean boolean7 = integer > 0;
final boolean boolean8 = integer2 > 0;
if (integer != integer2) {
final BlockState byg2 = this.setSignalForState(byg, integer2);
bjt.setBlock(fk, byg2, 2);
this.updateNeighbours(bjt, fk);
bjt.setBlocksDirty(fk, byg, byg2);
}
if (!boolean8 && boolean7) {
this.playOffSound(bjt, fk);
}
else if (boolean8 && !boolean7) {
this.playOnSound(bjt, fk);
}
if (boolean8) {
bjt.getBlockTicks().scheduleTick(new BlockPos(fk), this, this.getTickDelay(bjt));
}
}
protected abstract void playOnSound(final LevelAccessor bju, final BlockPos fk);
protected abstract void playOffSound(final LevelAccessor bju, final BlockPos fk);
@Override
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
if (boolean5 || byg1.getBlock() == byg4.getBlock()) {
return;
}
if (this.getSignalForState(byg1) > 0) {
this.updateNeighbours(bjt, fk);
}
super.onRemove(byg1, bjt, fk, byg4, boolean5);
}
protected void updateNeighbours(final Level bjt, final BlockPos fk) {
bjt.updateNeighborsAt(fk, this);
bjt.updateNeighborsAt(fk.below(), this);
}
@Override
public int getSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
return this.getSignalForState(byg);
}
@Override
public int getDirectSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
if (fp == Direction.UP) {
return this.getSignalForState(byg);
}
return 0;
}
@Override
public boolean isSignalSource(final BlockState byg) {
return true;
}
@Override
public PushReaction getPistonPushReaction(final BlockState byg) {
return PushReaction.DESTROY;
}
protected abstract int getSignalStrength(final Level bjt, final BlockPos fk);
protected abstract int getSignalForState(final BlockState byg);
protected abstract BlockState setSignalForState(final BlockState byg, final int integer);
static {
PRESSED_AABB = Block.box(1.0, 0.0, 1.0, 15.0, 0.5, 15.0);
AABB = Block.box(1.0, 0.0, 1.0, 15.0, 1.0, 15.0);
TOUCH_AABB = new AABB(0.125, 0.0, 0.125, 0.875, 0.25, 0.875);
}
}