| 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.entity.LivingEntity;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 10 | import net.minecraft.world.level.BlockGetter;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.SignalGetter;
|
| 14 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 19 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 20 | import net.minecraft.world.level.redstone.Orientation;
|
| 21 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 22 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 23 | import net.minecraft.world.ticks.TickPriority;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public abstract class DiodeBlock extends HorizontalDirectionalBlock {
|
| 27 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 28 | private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 2.0);
|
| 29 |
|
| 30 | protected DiodeBlock(final BlockBehaviour.Properties properties) {
|
| 31 | super(properties);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 36 | return SHAPE;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 41 | BlockPos belowPos = pos.below();
|
| 42 | return this.canSurviveOn(level, belowPos, level.getBlockState(belowPos));
|
| 43 | }
|
| 44 |
|
| 45 | protected boolean canSurviveOn(final LevelReader level, final BlockPos neightborPos, final BlockState neighborState) {
|
| 46 | return neighborState.isFaceSturdy(level, neightborPos, Direction.UP, SupportType.RIGID);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 51 | if (!this.isLocked(level, pos, state)) {
|
| 52 | boolean on = state.getValue(POWERED);
|
| 53 | boolean shouldTurnOn = this.shouldTurnOn(level, pos, state);
|
| 54 | if (on && !shouldTurnOn) {
|
| 55 | level.setBlock(pos, state.setValue(POWERED, false), 2);
|
| 56 | } else if (!on) {
|
| 57 | level.setBlock(pos, state.setValue(POWERED, true), 2);
|
| 58 | if (!shouldTurnOn) {
|
| 59 | level.scheduleTick(pos, this, this.getDelay(state), TickPriority.VERY_HIGH);
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void neighborChanged(
|
| 67 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 68 | ) {
|
| 69 | if (level.getBlockState(pos).is(this)) {
|
| 70 | if (state.canSurvive(level, pos)) {
|
| 71 | this.checkTickOnNeighbor(level, pos, state);
|
| 72 | } else {
|
| 73 | BlockEntity blockEntity = state.hasBlockEntity() ? level.getBlockEntity(pos) : null;
|
| 74 | dropResources(state, level, pos, blockEntity);
|
| 75 | level.removeBlock(pos, false);
|
| 76 |
|
| 77 | for (Direction direction : Direction.values()) {
|
| 78 | level.updateNeighborsAt(pos.relative(direction), this);
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | protected void checkTickOnNeighbor(final Level level, final BlockPos pos, final BlockState state) {
|
| 85 | if (!this.isLocked(level, pos, state)) {
|
| 86 | boolean on = state.getValue(POWERED);
|
| 87 | boolean shouldTurnOn = this.shouldTurnOn(level, pos, state);
|
| 88 | if (on != shouldTurnOn && !level.getBlockTicks().willTickThisTick(pos, this)) {
|
| 89 | TickPriority priority = TickPriority.HIGH;
|
| 90 | if (this.shouldPrioritize(level, pos, state)) {
|
| 91 | priority = TickPriority.EXTREMELY_HIGH;
|
| 92 | } else if (on) {
|
| 93 | priority = TickPriority.VERY_HIGH;
|
| 94 | }
|
| 95 |
|
| 96 | level.scheduleTick(pos, this, this.getDelay(state), priority);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | public boolean isLocked(final LevelReader level, final BlockPos pos, final BlockState state) {
|
| 102 | return false;
|
| 103 | }
|
| 104 |
|
| 105 | protected boolean shouldTurnOn(final Level level, final BlockPos pos, final BlockState state) {
|
| 106 | return this.getInputSignal(level, pos, state) > 0;
|
| 107 | }
|
| 108 |
|
| 109 | protected int getInputSignal(final Level level, final BlockPos pos, final BlockState state) {
|
| 110 | Direction direction = state.getValue(FACING);
|
| 111 | BlockPos targetPos = pos.relative(direction);
|
| 112 | int input = level.getSignal(targetPos, direction);
|
| 113 | if (input >= 15) {
|
| 114 | return input;
|
| 115 | }
|
| 116 |
|
| 117 | BlockState targetBlockState = level.getBlockState(targetPos);
|
| 118 | return Math.max(input, targetBlockState.is(Blocks.REDSTONE_WIRE) ? targetBlockState.getValue(RedstoneWireBlock.POWER) : 0);
|
| 119 | }
|
| 120 |
|
| 121 | protected int getAlternateSignal(final SignalGetter level, final BlockPos pos, final BlockState state) {
|
| 122 | Direction direction = state.getValue(FACING);
|
| 123 | Direction clockWise = direction.getClockWise();
|
| 124 | Direction counterClockWise = direction.getCounterClockWise();
|
| 125 | boolean sideInputDiodesOnly = this.sideInputDiodesOnly();
|
| 126 | return Math.max(
|
| 127 | level.getControlInputSignal(pos.relative(clockWise), clockWise, sideInputDiodesOnly),
|
| 128 | level.getControlInputSignal(pos.relative(counterClockWise), counterClockWise, sideInputDiodesOnly)
|
| 129 | );
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | protected boolean isSignalSource(final BlockState state) {
|
| 134 | return true;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 139 | return state.getValue(POWERED) ? this.getOutputSignal(level, pos, state) : 0;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 144 | return state.getSignal(level, pos, direction);
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | protected int getSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 149 | return state.getValue(FACING) == direction ? this.ownSignal(state, level, pos) : 0;
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 154 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 159 | if (this.shouldTurnOn(level, pos, state)) {
|
| 160 | level.scheduleTick(pos, this, 1);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 166 | this.updateNeighborsInFront(level, pos, state);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 171 | if (!movedByPiston) {
|
| 172 | this.updateNeighborsInFront(level, pos, state);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | protected void updateNeighborsInFront(final Level level, final BlockPos pos, final BlockState state) {
|
| 177 | Direction direction = state.getValue(FACING);
|
| 178 | BlockPos oppositePos = pos.relative(direction.getOpposite());
|
| 179 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, direction.getOpposite(), Direction.UP);
|
| 180 | level.neighborChanged(oppositePos, this, orientation);
|
| 181 | level.updateNeighborsAtExceptFromFacing(oppositePos, this, direction, orientation);
|
| 182 | }
|
| 183 |
|
| 184 | protected boolean sideInputDiodesOnly() {
|
| 185 | return false;
|
| 186 | }
|
| 187 |
|
| 188 | protected int getOutputSignal(final BlockGetter level, final BlockPos pos, final BlockState state) {
|
| 189 | return 15;
|
| 190 | }
|
| 191 |
|
| 192 | public static boolean isDiode(final BlockState state) {
|
| 193 | return state.getBlock() instanceof DiodeBlock;
|
| 194 | }
|
| 195 |
|
| 196 | public boolean shouldPrioritize(final BlockGetter level, final BlockPos pos, final BlockState state) {
|
| 197 | Direction direction = state.getValue(FACING).getOpposite();
|
| 198 | BlockState oppositeState = level.getBlockState(pos.relative(direction));
|
| 199 | return isDiode(oppositeState) && oppositeState.getValue(FACING) != direction;
|
| 200 | }
|
| 201 |
|
| 202 | protected abstract int getDelay(BlockState state);
|
| 203 | }
|
| 204 |
|