| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.function.BiConsumer;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.InteractionResult;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.level.BlockGetter;
|
| 19 | import net.minecraft.world.level.Explosion;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.LevelAccessor;
|
| 22 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 25 | import net.minecraft.world.level.block.state.properties.AttachFace;
|
| 26 | import net.minecraft.world.level.block.state.properties.BlockSetType;
|
| 27 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 28 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 29 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 30 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 31 | import net.minecraft.world.level.redstone.Orientation;
|
| 32 | import net.minecraft.world.phys.BlockHitResult;
|
| 33 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 34 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 35 | import net.minecraft.world.phys.shapes.Shapes;
|
| 36 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 37 | import org.jspecify.annotations.Nullable;
|
| 38 |
|
| 39 | /**
|
| 40 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 41 | */
|
| 42 | public class ButtonBlock extends FaceAttachedHorizontalDirectionalBlock {
|
| 43 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 44 | private final BlockSetType type;
|
| 45 | private final int ticksToStayPressed;
|
| 46 | private final Function<BlockState, VoxelShape> shapes;
|
| 47 |
|
| 48 | /**
|
| 49 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 50 | */
|
| 51 | public ButtonBlock(final BlockSetType type, final int ticksToStayPressed, final BlockBehaviour.Properties properties) {
|
| 52 | super(properties.sound(type.soundType()));
|
| 53 | this.type = type;
|
| 54 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(POWERED, false).setValue(FACE, AttachFace.WALL));
|
| 55 | this.ticksToStayPressed = ticksToStayPressed;
|
| 56 | this.shapes = this.makeShapes();
|
| 57 | }
|
| 58 |
|
| 59 | private Function<BlockState, VoxelShape> makeShapes() {
|
| 60 | VoxelShape pressedShaper = Block.cube(14.0);
|
| 61 | VoxelShape unpressedShaper = Block.cube(12.0);
|
| 62 | Map<AttachFace, Map<Direction, VoxelShape>> attachFace = Shapes.rotateAttachFace(Block.boxZ(6.0, 4.0, 8.0, 16.0));
|
| 63 | return this.getShapeForEachState(
|
| 64 | state -> Shapes.join(
|
| 65 | attachFace.get(state.getValue(FACE)).get(state.getValue(FACING)), state.getValue(POWERED) ? pressedShaper : unpressedShaper, BooleanOp.ONLY_FIRST
|
| 66 | )
|
| 67 | );
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 72 | return this.shapes.apply(state);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 77 | if (state.getValue(POWERED)) {
|
| 78 | return InteractionResult.CONSUME;
|
| 79 | }
|
| 80 |
|
| 81 | this.press(state, level, pos, player);
|
| 82 | return InteractionResult.SUCCESS;
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected void onExplosionHit(
|
| 87 | final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
|
| 88 | ) {
|
| 89 | if (explosion.canTriggerBlocks() && !state.getValue(POWERED)) {
|
| 90 | this.press(state, level, pos, null);
|
| 91 | }
|
| 92 |
|
| 93 | super.onExplosionHit(state, level, pos, explosion, onHit);
|
| 94 | }
|
| 95 |
|
| 96 | public void press(final BlockState state, final Level level, final BlockPos pos, final @Nullable Player player) {
|
| 97 | level.setBlockAndUpdate(pos, state.setValue(POWERED, true));
|
| 98 | this.updateNeighbours(state, level, pos);
|
| 99 | level.scheduleTick(pos, this, this.ticksToStayPressed);
|
| 100 | this.playSound(player, level, pos, true);
|
| 101 | level.gameEvent(player, GameEvent.BLOCK_ACTIVATE, pos);
|
| 102 | }
|
| 103 |
|
| 104 | protected void playSound(final @Nullable Player player, final LevelAccessor level, final BlockPos pos, final boolean pressed) {
|
| 105 | level.playSound(pressed ? player : null, pos, this.getSound(pressed), SoundSource.BLOCKS);
|
| 106 | }
|
| 107 |
|
| 108 | protected SoundEvent getSound(final boolean pressed) {
|
| 109 | return pressed ? this.type.buttonClickOn() : this.type.buttonClickOff();
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 114 | if (!movedByPiston && state.getValue(POWERED)) {
|
| 115 | this.updateNeighbours(state, level, pos);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 121 | return state.getValue(POWERED) && getConnectedDirection(state) == direction ? 15 : 0;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected boolean isSignalSource(final BlockState state) {
|
| 126 | return true;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 131 | return state.getValue(POWERED) ? 15 : 0;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 136 | if (state.getValue(POWERED)) {
|
| 137 | this.checkPressed(state, level, pos);
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | protected void entityInside(
|
| 143 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 144 | ) {
|
| 145 | if (!level.isClientSide() && this.type.canButtonBeActivatedByArrows() && !state.getValue(POWERED)) {
|
| 146 | this.checkPressed(state, level, pos);
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | protected void checkPressed(final BlockState state, final Level level, final BlockPos pos) {
|
| 151 | AbstractArrow firstArrow = this.type.canButtonBeActivatedByArrows()
|
| 152 | ? level.getEntitiesOfClass(AbstractArrow.class, state.getShape(level, pos).bounds().move(pos)).stream().findFirst().orElse(null)
|
| 153 | : null;
|
| 154 | boolean shouldBePressed = firstArrow != null;
|
| 155 | boolean wasPressed = state.getValue(POWERED);
|
| 156 | if (shouldBePressed != wasPressed) {
|
| 157 | level.setBlockAndUpdate(pos, state.setValue(POWERED, shouldBePressed));
|
| 158 | this.updateNeighbours(state, level, pos);
|
| 159 | this.playSound(null, level, pos, shouldBePressed);
|
| 160 | level.gameEvent(firstArrow, shouldBePressed ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE, pos);
|
| 161 | }
|
| 162 |
|
| 163 | if (shouldBePressed) {
|
| 164 | level.scheduleTick(pos, this, this.ticksToStayPressed);
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | private void updateNeighbours(final BlockState state, final Level level, final BlockPos pos) {
|
| 169 | Direction front = getConnectedDirection(state).getOpposite();
|
| 170 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, front, front.getAxis().isHorizontal() ? Direction.UP : state.getValue(FACING));
|
| 171 | level.updateNeighborsAt(pos, this, orientation);
|
| 172 | level.updateNeighborsAt(pos.relative(front), this, orientation);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 177 | builder.add(FACING, POWERED, FACE);
|
| 178 | }
|
| 179 | }
|
| 180 |
|