minecraft-src/net/minecraft/world/level/block/RepeaterBlock.java
2025-07-04 01:41:11 +03:00

102 lines
3.9 KiB
Java

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.LevelAccessor;
import net.minecraft.world.level.LevelReader;
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<RepeaterBlock> CODEC = simpleCodec(RepeaterBlock::new);
public static final BooleanProperty LOCKED = BlockStateProperties.LOCKED;
public static final IntegerProperty DELAY = BlockStateProperties.DELAY;
@Override
public MapCodec<RepeaterBlock> 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.sidedSuccess(level.isClientSide);
}
}
@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 state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if (direction == Direction.DOWN && !this.canSurviveOn(level, neighborPos, neighborState)) {
return Blocks.AIR.defaultBlockState();
} else {
return !level.isClientSide() && direction.getAxis() != ((Direction)state.getValue(FACING)).getAxis()
? state.setValue(LOCKED, this.isLocked(level, pos, state))
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
}
@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<Block, BlockState> builder) {
builder.add(FACING, DELAY, LOCKED, POWERED);
}
}