| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 8 | import net.minecraft.world.level.BlockGetter;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import net.minecraft.world.level.LevelReader;
|
| 11 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 12 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 15 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 16 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 17 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 18 | import net.minecraft.world.level.redstone.Orientation;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class ObserverBlock extends DirectionalBlock {
|
| 22 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 23 |
|
| 24 | public ObserverBlock(final BlockBehaviour.Properties properties) {
|
| 25 | super(properties);
|
| 26 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.SOUTH).setValue(POWERED, false));
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 31 | builder.add(FACING, POWERED);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 36 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 41 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 46 | if (state.getValue(POWERED)) {
|
| 47 | level.setBlock(pos, state.setValue(POWERED, false), 2);
|
| 48 | } else {
|
| 49 | level.setBlock(pos, state.setValue(POWERED, true), 2);
|
| 50 | level.scheduleTick(pos, this, 2);
|
| 51 | }
|
| 52 |
|
| 53 | this.updateNeighborsInFront(level, pos, state);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected BlockState updateShape(
|
| 58 | final BlockState state,
|
| 59 | final LevelReader level,
|
| 60 | final ScheduledTickAccess ticks,
|
| 61 | final BlockPos pos,
|
| 62 | final Direction directionToNeighbour,
|
| 63 | final BlockPos neighbourPos,
|
| 64 | final BlockState neighbourState,
|
| 65 | final RandomSource random
|
| 66 | ) {
|
| 67 | if (state.getValue(FACING) == directionToNeighbour && !state.getValue(POWERED)) {
|
| 68 | this.startSignal(level, ticks, pos);
|
| 69 | }
|
| 70 |
|
| 71 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 72 | }
|
| 73 |
|
| 74 | private void startSignal(final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos) {
|
| 75 | if (!level.isClientSide() && !ticks.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 76 | ticks.scheduleTick(pos, this, 2);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | protected void updateNeighborsInFront(final Level level, final BlockPos pos, final BlockState state) {
|
| 81 | Direction direction = state.getValue(FACING);
|
| 82 | BlockPos oppositePos = pos.relative(direction.getOpposite());
|
| 83 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, direction.getOpposite(), null);
|
| 84 | level.neighborChanged(oppositePos, this, orientation);
|
| 85 | level.updateNeighborsAtExceptFromFacing(oppositePos, this, direction, orientation);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected boolean isSignalSource(final BlockState state) {
|
| 90 | return true;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected boolean shouldRedstoneWireConnectTo(final BlockState state, final BlockGetter level, final BlockPos pos, final @Nullable Direction direction) {
|
| 95 | return direction == state.getValue(FACING);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 100 | return state.getValue(POWERED) ? 15 : 0;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 105 | return state.getSignal(level, pos, direction);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected int getSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 110 | return state.getValue(FACING) == direction ? this.ownSignal(state, level, pos) : 0;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 115 | if (!state.is(oldState.getBlock())) {
|
| 116 | if (!level.isClientSide() && state.getValue(POWERED) && !level.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 117 | BlockState newState = state.setValue(POWERED, false);
|
| 118 | level.setBlock(pos, newState, 18);
|
| 119 | this.updateNeighborsInFront(level, pos, newState);
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 126 | if (state.getValue(POWERED) && level.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 127 | this.updateNeighborsInFront(level, pos, state.setValue(POWERED, false));
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 133 | return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite().getOpposite());
|
| 134 | }
|
| 135 | }
|
| 136 |
|