| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.ParticleUtils;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.valueproviders.UniformInt;
|
| 10 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 11 | import net.minecraft.world.level.BlockGetter;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.LevelReader;
|
| 14 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 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.StateDefinition;
|
| 18 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 19 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 20 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 21 | import net.minecraft.world.level.material.FluidState;
|
| 22 | import net.minecraft.world.level.material.Fluids;
|
| 23 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 24 |
|
| 25 | public class LightningRodBlock extends RodBlock implements SimpleWaterloggedBlock {
|
| 26 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 27 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 28 | private static final int ACTIVATION_TICKS = 8;
|
| 29 | public static final int RANGE = 128;
|
| 30 | private static final int SPARK_CYCLE = 200;
|
| 31 |
|
| 32 | public LightningRodBlock(final BlockBehaviour.Properties properties) {
|
| 33 | super(properties);
|
| 34 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.UP).setValue(WATERLOGGED, false).setValue(POWERED, false));
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 39 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 40 | boolean isWaterSource = replacedFluidState.is(Fluids.WATER);
|
| 41 | return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(WATERLOGGED, isWaterSource);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected BlockState updateShape(
|
| 46 | final BlockState state,
|
| 47 | final LevelReader level,
|
| 48 | final ScheduledTickAccess ticks,
|
| 49 | final BlockPos pos,
|
| 50 | final Direction directionToNeighbour,
|
| 51 | final BlockPos neighbourPos,
|
| 52 | final BlockState neighbourState,
|
| 53 | final RandomSource random
|
| 54 | ) {
|
| 55 | if (state.getValue(WATERLOGGED)) {
|
| 56 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 57 | }
|
| 58 |
|
| 59 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected FluidState getFluidState(final BlockState state) {
|
| 64 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 69 | return state.getValue(POWERED) ? 15 : 0;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 74 | return state.getValue(POWERED) && state.getValue(FACING) == direction ? 15 : 0;
|
| 75 | }
|
| 76 |
|
| 77 | public void onLightningStrike(final BlockState state, final Level level, final BlockPos pos) {
|
| 78 | level.setBlockAndUpdate(pos, state.setValue(POWERED, true));
|
| 79 | this.updateNeighbours(state, level, pos);
|
| 80 | level.scheduleTick(pos, this, 8);
|
| 81 | level.levelEvent(3002, pos, state.getValue(FACING).getAxis().ordinal());
|
| 82 | }
|
| 83 |
|
| 84 | private void updateNeighbours(final BlockState state, final Level level, final BlockPos pos) {
|
| 85 | Direction front = state.getValue(FACING).getOpposite();
|
| 86 | level.updateNeighborsAt(pos.relative(front), this, ExperimentalRedstoneUtils.initialOrientation(level, front, null));
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 91 | level.setBlockAndUpdate(pos, state.setValue(POWERED, false));
|
| 92 | this.updateNeighbours(state, level, pos);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 97 | if (level.isThundering()
|
| 98 | && level.getRandom().nextInt(200) <= level.getGameTime() % 200L
|
| 99 | && pos.getY() == level.getHeight(Heightmap.Types.WORLD_SURFACE, pos.getX(), pos.getZ()) - 1) {
|
| 100 | ParticleUtils.spawnParticlesAlongAxis(state.getValue(FACING).getAxis(), level, pos, 0.125, ParticleTypes.ELECTRIC_SPARK, UniformInt.of(1, 2));
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 106 | if (state.getValue(POWERED)) {
|
| 107 | this.updateNeighbours(state, level, pos);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 113 | if (!state.is(oldState.getBlock())) {
|
| 114 | if (state.getValue(POWERED) && !level.getBlockTicks().hasScheduledTick(pos, this)) {
|
| 115 | level.scheduleTick(pos, this, 8);
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 122 | builder.add(FACING, POWERED, WATERLOGGED);
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | protected boolean isSignalSource(final BlockState state) {
|
| 127 | return true;
|
| 128 | }
|
| 129 | }
|
| 130 |
|