package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; import net.minecraft.util.RandomSource; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.SignalGetter; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils; import net.minecraft.world.level.redstone.Orientation; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.ticks.TickPriority; import org.jetbrains.annotations.Nullable; public abstract class DiodeBlock extends HorizontalDirectionalBlock { public static final BooleanProperty POWERED = BlockStateProperties.POWERED; private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 2.0); protected DiodeBlock(BlockBehaviour.Properties properties) { super(properties); } @Override protected abstract MapCodec codec(); @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return SHAPE; } @Override protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { BlockPos blockPos = pos.below(); return this.canSurviveOn(level, blockPos, level.getBlockState(blockPos)); } protected boolean canSurviveOn(LevelReader level, BlockPos pos, BlockState state) { return state.isFaceSturdy(level, pos, Direction.UP, SupportType.RIGID); } @Override protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { if (!this.isLocked(level, pos, state)) { boolean bl = (Boolean)state.getValue(POWERED); boolean bl2 = this.shouldTurnOn(level, pos, state); if (bl && !bl2) { level.setBlock(pos, state.setValue(POWERED, false), 2); } else if (!bl) { level.setBlock(pos, state.setValue(POWERED, true), 2); if (!bl2) { level.scheduleTick(pos, this, this.getDelay(state), TickPriority.VERY_HIGH); } } } } @Override protected int getDirectSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) { return state.getSignal(level, pos, direction); } @Override protected int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) { if (!(Boolean)state.getValue(POWERED)) { return 0; } else { return state.getValue(FACING) == direction ? this.getOutputSignal(level, pos, state) : 0; } } @Override protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @Nullable Orientation orientation, boolean movedByPiston) { if (state.canSurvive(level, pos)) { this.checkTickOnNeighbor(level, pos, state); } else { BlockEntity blockEntity = state.hasBlockEntity() ? level.getBlockEntity(pos) : null; dropResources(state, level, pos, blockEntity); level.removeBlock(pos, false); for (Direction direction : Direction.values()) { level.updateNeighborsAt(pos.relative(direction), this); } } } /** * Check the output signal of this diode and schedule a new block tick if it should change. */ protected void checkTickOnNeighbor(Level level, BlockPos pos, BlockState state) { if (!this.isLocked(level, pos, state)) { boolean bl = (Boolean)state.getValue(POWERED); boolean bl2 = this.shouldTurnOn(level, pos, state); if (bl != bl2 && !level.getBlockTicks().willTickThisTick(pos, this)) { TickPriority tickPriority = TickPriority.HIGH; if (this.shouldPrioritize(level, pos, state)) { tickPriority = TickPriority.EXTREMELY_HIGH; } else if (bl) { tickPriority = TickPriority.VERY_HIGH; } level.scheduleTick(pos, this, this.getDelay(state), tickPriority); } } } /** * Check if neighboring blocks are locking this diode. */ public boolean isLocked(LevelReader level, BlockPos pos, BlockState state) { return false; } protected boolean shouldTurnOn(Level level, BlockPos pos, BlockState state) { return this.getInputSignal(level, pos, state) > 0; } protected int getInputSignal(Level level, BlockPos pos, BlockState state) { Direction direction = state.getValue(FACING); BlockPos blockPos = pos.relative(direction); int i = level.getSignal(blockPos, direction); if (i >= 15) { return i; } else { BlockState blockState = level.getBlockState(blockPos); return Math.max(i, blockState.is(Blocks.REDSTONE_WIRE) ? (Integer)blockState.getValue(RedStoneWireBlock.POWER) : 0); } } protected int getAlternateSignal(SignalGetter level, BlockPos pos, BlockState state) { Direction direction = state.getValue(FACING); Direction direction2 = direction.getClockWise(); Direction direction3 = direction.getCounterClockWise(); boolean bl = this.sideInputDiodesOnly(); return Math.max(level.getControlInputSignal(pos.relative(direction2), direction2, bl), level.getControlInputSignal(pos.relative(direction3), direction3, bl)); } @Override protected boolean isSignalSource(BlockState state) { return true; } @Override public BlockState getStateForPlacement(BlockPlaceContext context) { return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()); } @Override public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) { if (this.shouldTurnOn(level, pos, state)) { level.scheduleTick(pos, this, 1); } } @Override protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) { this.updateNeighborsInFront(level, pos, state); } @Override protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) { if (!movedByPiston) { this.updateNeighborsInFront(level, pos, state); } } protected void updateNeighborsInFront(Level level, BlockPos pos, BlockState state) { Direction direction = state.getValue(FACING); BlockPos blockPos = pos.relative(direction.getOpposite()); Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, direction.getOpposite(), Direction.UP); level.neighborChanged(blockPos, this, orientation); level.updateNeighborsAtExceptFromFacing(blockPos, this, direction, orientation); } protected boolean sideInputDiodesOnly() { return false; } protected int getOutputSignal(BlockGetter level, BlockPos pos, BlockState state) { return 15; } public static boolean isDiode(BlockState state) { return state.getBlock() instanceof DiodeBlock; } /** * Check if this diode should have a higher tick priority than default. * *

* Both repeaters and comparators use this method to increase their tick priorities * when facing other diodes. This makes certain monostable circuits based on the repeater locking * mechanic more reliable. */ public boolean shouldPrioritize(BlockGetter level, BlockPos pos, BlockState state) { Direction direction = ((Direction)state.getValue(FACING)).getOpposite(); BlockState blockState = level.getBlockState(pos.relative(direction)); return isDiode(blockState) && blockState.getValue(FACING) != direction; } protected abstract int getDelay(BlockState state); }