218 lines
10 KiB
Java
218 lines
10 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import net.minecraft.world.level.block.state.AbstractStateHolder;
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import java.util.List;
|
|
import net.minecraft.core.Vec3i;
|
|
import net.minecraft.world.entity.projectile.AbstractArrow;
|
|
import net.minecraft.world.entity.Entity;
|
|
import java.util.Random;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.sounds.SoundEvent;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.block.state.properties.AttachFace;
|
|
import net.minecraft.world.level.block.state.properties.Property;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
|
|
|
public abstract class ButtonBlock extends FaceAttachedHorizontalDirectionalBlock {
|
|
public static final BooleanProperty POWERED;
|
|
protected static final VoxelShape CEILING_AABB_X;
|
|
protected static final VoxelShape CEILING_AABB_Z;
|
|
protected static final VoxelShape FLOOR_AABB_X;
|
|
protected static final VoxelShape FLOOR_AABB_Z;
|
|
protected static final VoxelShape NORTH_AABB;
|
|
protected static final VoxelShape SOUTH_AABB;
|
|
protected static final VoxelShape WEST_AABB;
|
|
protected static final VoxelShape EAST_AABB;
|
|
protected static final VoxelShape PRESSED_CEILING_AABB_X;
|
|
protected static final VoxelShape PRESSED_CEILING_AABB_Z;
|
|
protected static final VoxelShape PRESSED_FLOOR_AABB_X;
|
|
protected static final VoxelShape PRESSED_FLOOR_AABB_Z;
|
|
protected static final VoxelShape PRESSED_NORTH_AABB;
|
|
protected static final VoxelShape PRESSED_SOUTH_AABB;
|
|
protected static final VoxelShape PRESSED_WEST_AABB;
|
|
protected static final VoxelShape PRESSED_EAST_AABB;
|
|
private final boolean sensitive;
|
|
|
|
protected ButtonBlock(final boolean boolean1, final Properties c) {
|
|
super(c);
|
|
this.registerDefaultState(((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)ButtonBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)ButtonBlock.POWERED, false)).<AttachFace, AttachFace>setValue(ButtonBlock.FACE, AttachFace.WALL));
|
|
this.sensitive = boolean1;
|
|
}
|
|
|
|
@Override
|
|
public int getTickDelay(final LevelReader bjw) {
|
|
return this.sensitive ? 30 : 20;
|
|
}
|
|
|
|
@Override
|
|
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
|
|
final Direction fp6 = byg.<Direction>getValue((Property<Direction>)ButtonBlock.FACING);
|
|
final boolean boolean7 = byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED);
|
|
switch (byg.<AttachFace>getValue(ButtonBlock.FACE)) {
|
|
case FLOOR: {
|
|
if (fp6.getAxis() == Direction.Axis.X) {
|
|
return boolean7 ? ButtonBlock.PRESSED_FLOOR_AABB_X : ButtonBlock.FLOOR_AABB_X;
|
|
}
|
|
return boolean7 ? ButtonBlock.PRESSED_FLOOR_AABB_Z : ButtonBlock.FLOOR_AABB_Z;
|
|
}
|
|
case WALL: {
|
|
switch (fp6) {
|
|
case EAST: {
|
|
return boolean7 ? ButtonBlock.PRESSED_EAST_AABB : ButtonBlock.EAST_AABB;
|
|
}
|
|
case WEST: {
|
|
return boolean7 ? ButtonBlock.PRESSED_WEST_AABB : ButtonBlock.WEST_AABB;
|
|
}
|
|
case SOUTH: {
|
|
return boolean7 ? ButtonBlock.PRESSED_SOUTH_AABB : ButtonBlock.SOUTH_AABB;
|
|
}
|
|
default: {
|
|
return boolean7 ? ButtonBlock.PRESSED_NORTH_AABB : ButtonBlock.NORTH_AABB;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
default: {
|
|
if (fp6.getAxis() == Direction.Axis.X) {
|
|
return boolean7 ? ButtonBlock.PRESSED_CEILING_AABB_X : ButtonBlock.CEILING_AABB_X;
|
|
}
|
|
return boolean7 ? ButtonBlock.PRESSED_CEILING_AABB_Z : ButtonBlock.CEILING_AABB_Z;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
|
|
if (byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
|
|
return InteractionResult.CONSUME;
|
|
}
|
|
this.press(byg, bjt, fk);
|
|
this.playSound(ayg, bjt, fk, true);
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
|
|
public void press(final BlockState byg, final Level bjt, final BlockPos fk) {
|
|
bjt.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)ButtonBlock.POWERED, true), 3);
|
|
this.updateNeighbours(byg, bjt, fk);
|
|
bjt.getBlockTicks().scheduleTick(fk, this, this.getTickDelay(bjt));
|
|
}
|
|
|
|
protected void playSound(@Nullable final Player ayg, final LevelAccessor bju, final BlockPos fk, final boolean boolean4) {
|
|
bju.playSound(boolean4 ? ayg : null, fk, this.getSound(boolean4), SoundSource.BLOCKS, 0.3f, boolean4 ? 0.6f : 0.5f);
|
|
}
|
|
|
|
protected abstract SoundEvent getSound(final boolean boolean1);
|
|
|
|
@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 (byg1.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
|
|
this.updateNeighbours(byg1, bjt, fk);
|
|
}
|
|
super.onRemove(byg1, bjt, fk, byg4, boolean5);
|
|
}
|
|
|
|
@Override
|
|
public int getSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
return byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED) ? 15 : 0;
|
|
}
|
|
|
|
@Override
|
|
public int getDirectSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
if (byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED) && FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byg) == fp) {
|
|
return 15;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean isSignalSource(final BlockState byg) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
|
|
if (!byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
|
|
return;
|
|
}
|
|
if (this.sensitive) {
|
|
this.checkPressed(byg, xd, fk);
|
|
}
|
|
else {
|
|
xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)ButtonBlock.POWERED, false), 3);
|
|
this.updateNeighbours(byg, xd, fk);
|
|
this.playSound(null, xd, fk, false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void entityInside(final BlockState byg, final Level bjt, final BlockPos fk, final Entity akn) {
|
|
if (bjt.isClientSide || !this.sensitive || byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
|
|
return;
|
|
}
|
|
this.checkPressed(byg, bjt, fk);
|
|
}
|
|
|
|
private void checkPressed(final BlockState byg, final Level bjt, final BlockPos fk) {
|
|
final List<? extends Entity> list5 = bjt.getEntitiesOfClass(AbstractArrow.class, byg.getShape(bjt, fk).bounds().move(fk));
|
|
final boolean boolean6 = !list5.isEmpty();
|
|
final boolean boolean7 = byg.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED);
|
|
if (boolean6 != boolean7) {
|
|
bjt.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)ButtonBlock.POWERED, boolean6), 3);
|
|
this.updateNeighbours(byg, bjt, fk);
|
|
this.playSound(null, bjt, fk, boolean6);
|
|
}
|
|
if (boolean6) {
|
|
bjt.getBlockTicks().scheduleTick(new BlockPos(fk), this, this.getTickDelay(bjt));
|
|
}
|
|
}
|
|
|
|
private void updateNeighbours(final BlockState byg, final Level bjt, final BlockPos fk) {
|
|
bjt.updateNeighborsAt(fk, this);
|
|
bjt.updateNeighborsAt(fk.relative(FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byg).getOpposite()), this);
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
|
|
a.add(ButtonBlock.FACING, ButtonBlock.POWERED, ButtonBlock.FACE);
|
|
}
|
|
|
|
static {
|
|
POWERED = BlockStateProperties.POWERED;
|
|
CEILING_AABB_X = Block.box(6.0, 14.0, 5.0, 10.0, 16.0, 11.0);
|
|
CEILING_AABB_Z = Block.box(5.0, 14.0, 6.0, 11.0, 16.0, 10.0);
|
|
FLOOR_AABB_X = Block.box(6.0, 0.0, 5.0, 10.0, 2.0, 11.0);
|
|
FLOOR_AABB_Z = Block.box(5.0, 0.0, 6.0, 11.0, 2.0, 10.0);
|
|
NORTH_AABB = Block.box(5.0, 6.0, 14.0, 11.0, 10.0, 16.0);
|
|
SOUTH_AABB = Block.box(5.0, 6.0, 0.0, 11.0, 10.0, 2.0);
|
|
WEST_AABB = Block.box(14.0, 6.0, 5.0, 16.0, 10.0, 11.0);
|
|
EAST_AABB = Block.box(0.0, 6.0, 5.0, 2.0, 10.0, 11.0);
|
|
PRESSED_CEILING_AABB_X = Block.box(6.0, 15.0, 5.0, 10.0, 16.0, 11.0);
|
|
PRESSED_CEILING_AABB_Z = Block.box(5.0, 15.0, 6.0, 11.0, 16.0, 10.0);
|
|
PRESSED_FLOOR_AABB_X = Block.box(6.0, 0.0, 5.0, 10.0, 1.0, 11.0);
|
|
PRESSED_FLOOR_AABB_Z = Block.box(5.0, 0.0, 6.0, 11.0, 1.0, 10.0);
|
|
PRESSED_NORTH_AABB = Block.box(5.0, 6.0, 15.0, 11.0, 10.0, 16.0);
|
|
PRESSED_SOUTH_AABB = Block.box(5.0, 6.0, 0.0, 11.0, 10.0, 1.0);
|
|
PRESSED_WEST_AABB = Block.box(15.0, 6.0, 5.0, 16.0, 10.0, 11.0);
|
|
PRESSED_EAST_AABB = Block.box(0.0, 6.0, 5.0, 1.0, 10.0, 11.0);
|
|
}
|
|
}
|