package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.particles.DustParticleOptions; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.ScheduledTickAccess; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.phys.BlockHitResult; public class RepeaterBlock extends DiodeBlock { public static final MapCodec CODEC = simpleCodec(RepeaterBlock::new); public static final BooleanProperty LOCKED = BlockStateProperties.LOCKED; public static final IntegerProperty DELAY = BlockStateProperties.DELAY; @Override public MapCodec codec() { return CODEC; } protected RepeaterBlock(BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(DELAY, 1).setValue(LOCKED, false).setValue(POWERED, false)); } @Override protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { if (!player.getAbilities().mayBuild) { return InteractionResult.PASS; } else { level.setBlock(pos, state.cycle(DELAY), 3); return InteractionResult.SUCCESS; } } @Override protected int getDelay(BlockState state) { return (Integer)state.getValue(DELAY) * 2; } @Override public BlockState getStateForPlacement(BlockPlaceContext context) { BlockState blockState = super.getStateForPlacement(context); return blockState.setValue(LOCKED, this.isLocked(context.getLevel(), context.getClickedPos(), blockState)); } @Override protected BlockState updateShape( BlockState blockState, LevelReader levelReader, ScheduledTickAccess scheduledTickAccess, BlockPos blockPos, Direction direction, BlockPos blockPos2, BlockState blockState2, RandomSource randomSource ) { if (direction == Direction.DOWN && !this.canSurviveOn(levelReader, blockPos2, blockState2)) { return Blocks.AIR.defaultBlockState(); } else { return !levelReader.isClientSide() && direction.getAxis() != ((Direction)blockState.getValue(FACING)).getAxis() ? blockState.setValue(LOCKED, this.isLocked(levelReader, blockPos, blockState)) : super.updateShape(blockState, levelReader, scheduledTickAccess, blockPos, direction, blockPos2, blockState2, randomSource); } } @Override public boolean isLocked(LevelReader level, BlockPos pos, BlockState state) { return this.getAlternateSignal(level, pos, state) > 0; } @Override protected boolean sideInputDiodesOnly() { return true; } @Override public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) { if ((Boolean)state.getValue(POWERED)) { Direction direction = state.getValue(FACING); double d = pos.getX() + 0.5 + (random.nextDouble() - 0.5) * 0.2; double e = pos.getY() + 0.4 + (random.nextDouble() - 0.5) * 0.2; double f = pos.getZ() + 0.5 + (random.nextDouble() - 0.5) * 0.2; float g = -5.0F; if (random.nextBoolean()) { g = (Integer)state.getValue(DELAY) * 2 - 1; } g /= 16.0F; double h = g * direction.getStepX(); double i = g * direction.getStepZ(); level.addParticle(DustParticleOptions.REDSTONE, d + h, e, f + i, 0.0, 0.0, 0.0); } } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(FACING, DELAY, LOCKED, POWERED); } }