| 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.core.particles.DustParticleOptions;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.sounds.SoundSource;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Explosion;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.LevelAccessor;
|
| 20 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 23 | import net.minecraft.world.level.block.state.properties.AttachFace;
|
| 24 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 25 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 26 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 27 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 28 | import net.minecraft.world.level.redstone.Orientation;
|
| 29 | import net.minecraft.world.phys.BlockHitResult;
|
| 30 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 31 | import net.minecraft.world.phys.shapes.Shapes;
|
| 32 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 |
|
| 35 | /**
|
| 36 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 37 | */
|
| 38 | public class LeverBlock extends FaceAttachedHorizontalDirectionalBlock {
|
| 39 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 40 | private final Function<BlockState, VoxelShape> shapes;
|
| 41 |
|
| 42 | /**
|
| 43 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 44 | */
|
| 45 | public LeverBlock(final BlockBehaviour.Properties properties) {
|
| 46 | super(properties);
|
| 47 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(POWERED, false).setValue(FACE, AttachFace.WALL));
|
| 48 | this.shapes = this.makeShapes();
|
| 49 | }
|
| 50 |
|
| 51 | private Function<BlockState, VoxelShape> makeShapes() {
|
| 52 | Map<AttachFace, Map<Direction, VoxelShape>> attachFace = Shapes.rotateAttachFace(Block.boxZ(6.0, 8.0, 10.0, 16.0));
|
| 53 | return this.getShapeForEachState(state -> attachFace.get(state.getValue(FACE)).get(state.getValue(FACING)), POWERED);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 58 | return this.shapes.apply(state);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected InteractionResult useWithoutItem(
|
| 63 | final BlockState stateBefore, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult
|
| 64 | ) {
|
| 65 | if (level.isClientSide()) {
|
| 66 | BlockState stateAfter = stateBefore.cycle(POWERED);
|
| 67 | if (stateAfter.getValue(POWERED)) {
|
| 68 | makeParticle(stateAfter, level, pos, 1.0F);
|
| 69 | }
|
| 70 | } else {
|
| 71 | this.pull(stateBefore, level, pos, null);
|
| 72 | }
|
| 73 |
|
| 74 | return InteractionResult.SUCCESS;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void onExplosionHit(
|
| 79 | final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
|
| 80 | ) {
|
| 81 | if (explosion.canTriggerBlocks()) {
|
| 82 | this.pull(state, level, pos, null);
|
| 83 | }
|
| 84 |
|
| 85 | super.onExplosionHit(state, level, pos, explosion, onHit);
|
| 86 | }
|
| 87 |
|
| 88 | public void pull(BlockState state, final Level level, final BlockPos pos, final @Nullable Player player) {
|
| 89 | state = state.cycle(POWERED);
|
| 90 | level.setBlockAndUpdate(pos, state);
|
| 91 | this.updateNeighbours(state, level, pos);
|
| 92 | playSound(player, level, pos, state);
|
| 93 | level.gameEvent(player, state.getValue(POWERED) ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE, pos);
|
| 94 | }
|
| 95 |
|
| 96 | protected static void playSound(final @Nullable Player player, final LevelAccessor level, final BlockPos pos, final BlockState stateAfter) {
|
| 97 | float pitch = stateAfter.getValue(POWERED) ? 0.6F : 0.5F;
|
| 98 | level.playSound(player, pos, SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3F, pitch);
|
| 99 | }
|
| 100 |
|
| 101 | private static void makeParticle(final BlockState state, final LevelAccessor level, final BlockPos pos, final float scale) {
|
| 102 | Direction opposite = state.getValue(FACING).getOpposite();
|
| 103 | Direction oppositeConnect = getConnectedDirection(state).getOpposite();
|
| 104 | double x = pos.getX() + 0.5 + 0.1 * opposite.getStepX() + 0.2 * oppositeConnect.getStepX();
|
| 105 | double y = pos.getY() + 0.5 + 0.1 * opposite.getStepY() + 0.2 * oppositeConnect.getStepY();
|
| 106 | double z = pos.getZ() + 0.5 + 0.1 * opposite.getStepZ() + 0.2 * oppositeConnect.getStepZ();
|
| 107 | level.addParticle(new DustParticleOptions(16711680, scale), x, y, z, 0.0, 0.0, 0.0);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 112 | if (state.getValue(POWERED) && random.nextFloat() < 0.25F) {
|
| 113 | makeParticle(state, level, pos, 0.5F);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 119 | if (!movedByPiston && state.getValue(POWERED)) {
|
| 120 | this.updateNeighbours(state, level, pos);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 126 | return state.getValue(POWERED) && getConnectedDirection(state) == direction ? 15 : 0;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected boolean isSignalSource(final BlockState state) {
|
| 131 | return true;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 136 | return state.getValue(POWERED) ? 15 : 0;
|
| 137 | }
|
| 138 |
|
| 139 | private void updateNeighbours(final BlockState state, final Level level, final BlockPos pos) {
|
| 140 | Direction front = getConnectedDirection(state).getOpposite();
|
| 141 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, front, front.getAxis().isHorizontal() ? Direction.UP : state.getValue(FACING));
|
| 142 | level.updateNeighborsAt(pos, this, orientation);
|
| 143 | level.updateNeighborsAt(pos.relative(front), this, orientation);
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 148 | builder.add(FACE, FACING, POWERED);
|
| 149 | }
|
| 150 | }
|
| 151 |
|