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.ParticleTypes; import net.minecraft.server.level.ServerLevel; import net.minecraft.util.ParticleUtils; import net.minecraft.util.RandomSource; import net.minecraft.util.valueproviders.UniformInt; 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.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.levelgen.Heightmap; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils; public class LightningRodBlock extends RodBlock implements SimpleWaterloggedBlock { public static final MapCodec CODEC = simpleCodec(LightningRodBlock::new); public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final BooleanProperty POWERED = BlockStateProperties.POWERED; private static final int ACTIVATION_TICKS = 8; public static final int RANGE = 128; private static final int SPARK_CYCLE = 200; @Override public MapCodec codec() { return CODEC; } public LightningRodBlock(final BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.UP).setValue(WATERLOGGED, false).setValue(POWERED, false)); } @Override public BlockState getStateForPlacement(final BlockPlaceContext context) { FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos()); boolean isWaterSource = replacedFluidState.is(Fluids.WATER); return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(WATERLOGGED, isWaterSource); } @Override protected BlockState updateShape( final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random ) { if ((Boolean)state.getValue(WATERLOGGED)) { ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random); } @Override protected FluidState getFluidState(final BlockState state) { return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state); } @Override protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) { return state.getValue(POWERED) ? 15 : 0; } @Override protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) { return state.getValue(POWERED) && state.getValue(FACING) == direction ? 15 : 0; } public void onLightningStrike(final BlockState state, final Level level, final BlockPos pos) { level.setBlock(pos, state.setValue(POWERED, true), 3); this.updateNeighbours(state, level, pos); level.scheduleTick(pos, this, 8); level.levelEvent(3002, pos, ((Direction)state.getValue(FACING)).getAxis().ordinal()); } private void updateNeighbours(final BlockState state, final Level level, final BlockPos pos) { Direction front = ((Direction)state.getValue(FACING)).getOpposite(); level.updateNeighborsAt(pos.relative(front), this, ExperimentalRedstoneUtils.initialOrientation(level, front, null)); } @Override protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) { level.setBlock(pos, state.setValue(POWERED, false), 3); this.updateNeighbours(state, level, pos); } @Override public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) { if (level.isThundering() && level.getRandom().nextInt(200) <= level.getGameTime() % 200L && pos.getY() == level.getHeight(Heightmap.Types.WORLD_SURFACE, pos.getX(), pos.getZ()) - 1) { ParticleUtils.spawnParticlesAlongAxis(((Direction)state.getValue(FACING)).getAxis(), level, pos, 0.125, ParticleTypes.ELECTRIC_SPARK, UniformInt.of(1, 2)); } } @Override protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) { if ((Boolean)state.getValue(POWERED)) { this.updateNeighbours(state, level, pos); } } @Override protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) { if (!state.is(oldState.getBlock())) { if ((Boolean)state.getValue(POWERED) && !level.getBlockTicks().hasScheduledTick(pos, this)) { level.scheduleTick(pos, this, 8); } } } @Override protected void createBlockStateDefinition(final StateDefinition.Builder builder) { builder.add(FACING, POWERED, WATERLOGGED); } @Override protected boolean isSignalSource(final BlockState state) { return true; } }