| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.OptionalInt;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.tags.BlockTags;
|
| 9 | import net.minecraft.util.ParticleUtils;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.LevelAccessor;
|
| 15 | import net.minecraft.world.level.LevelReader;
|
| 16 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 17 | import net.minecraft.world.level.block.sounds.AmbientLeavesBlockSoundPlayer;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 21 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 22 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 23 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 24 | import net.minecraft.world.level.material.FluidState;
|
| 25 | import net.minecraft.world.level.material.Fluids;
|
| 26 | import net.minecraft.world.phys.shapes.Shapes;
|
| 27 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 28 |
|
| 29 | public class LeavesBlock extends Block implements SimpleWaterloggedBlock {
|
| 30 | public static final int DECAY_DISTANCE = 7;
|
| 31 | public static final IntegerProperty DISTANCE = BlockStateProperties.DISTANCE;
|
| 32 | public static final BooleanProperty PERSISTENT = BlockStateProperties.PERSISTENT;
|
| 33 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 34 | protected final AmbientLeavesBlockSoundPlayer ambientLeavesBlockSoundPlayer;
|
| 35 | private static final int TICK_DELAY = 1;
|
| 36 | private static volatile boolean cutoutLeaves = true;
|
| 37 |
|
| 38 | public LeavesBlock(final AmbientLeavesBlockSoundPlayer ambientLeavesBlockSoundPlayer, final BlockBehaviour.Properties properties) {
|
| 39 | super(properties);
|
| 40 | this.ambientLeavesBlockSoundPlayer = ambientLeavesBlockSoundPlayer;
|
| 41 | this.registerDefaultState(this.stateDefinition.any().setValue(DISTANCE, 7).setValue(PERSISTENT, false).setValue(WATERLOGGED, false));
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected boolean skipRendering(final BlockState state, final BlockState neighborState, final Direction direction) {
|
| 46 | return !cutoutLeaves && neighborState.getBlock() instanceof LeavesBlock ? true : super.skipRendering(state, neighborState, direction);
|
| 47 | }
|
| 48 |
|
| 49 | public static void setCutoutLeaves(final boolean cutoutLeaves) {
|
| 50 | LeavesBlock.cutoutLeaves = cutoutLeaves;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 55 | return Shapes.empty();
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 60 | return state.getValue(DISTANCE) == 7 && !state.getValue(PERSISTENT);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 65 | if (this.decaying(state)) {
|
| 66 | dropResources(state, level, pos);
|
| 67 | level.removeBlock(pos, false);
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | protected boolean decaying(final BlockState state) {
|
| 72 | return !state.getValue(PERSISTENT) && state.getValue(DISTANCE) == 7;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 77 | level.setBlockAndUpdate(pos, updateDistance(state, level, pos));
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | protected int getLightDampening(final BlockState state) {
|
| 82 | return 1;
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected BlockState updateShape(
|
| 87 | final BlockState state,
|
| 88 | final LevelReader level,
|
| 89 | final ScheduledTickAccess ticks,
|
| 90 | final BlockPos pos,
|
| 91 | final Direction directionToNeighbour,
|
| 92 | final BlockPos neighbourPos,
|
| 93 | final BlockState neighbourState,
|
| 94 | final RandomSource random
|
| 95 | ) {
|
| 96 | if (state.getValue(WATERLOGGED)) {
|
| 97 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 98 | }
|
| 99 |
|
| 100 | int distanceFromNeighbor = getDistanceAt(neighbourState) + 1;
|
| 101 | if (distanceFromNeighbor != 1 || state.getValue(DISTANCE) != distanceFromNeighbor) {
|
| 102 | ticks.scheduleTick(pos, this, 1);
|
| 103 | }
|
| 104 |
|
| 105 | return state;
|
| 106 | }
|
| 107 |
|
| 108 | private static BlockState updateDistance(final BlockState state, final LevelAccessor level, final BlockPos pos) {
|
| 109 | int newDistance = 7;
|
| 110 | BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
|
| 111 |
|
| 112 | for (Direction direction : Direction.values()) {
|
| 113 | neighborPos.setWithOffset(pos, direction);
|
| 114 | newDistance = Math.min(newDistance, getDistanceAt(level.getBlockState(neighborPos)) + 1);
|
| 115 | if (newDistance == 1) {
|
| 116 | break;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | return state.setValue(DISTANCE, newDistance);
|
| 121 | }
|
| 122 |
|
| 123 | private static int getDistanceAt(final BlockState state) {
|
| 124 | return getOptionalDistanceAt(state).orElse(7);
|
| 125 | }
|
| 126 |
|
| 127 | public static OptionalInt getOptionalDistanceAt(final BlockState state) {
|
| 128 | if (state.is(BlockTags.PREVENTS_NEARBY_LEAF_DECAY)) {
|
| 129 | return OptionalInt.of(0);
|
| 130 | } else {
|
| 131 | return state.hasProperty(DISTANCE) ? OptionalInt.of(state.getValue(DISTANCE)) : OptionalInt.empty();
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected FluidState getFluidState(final BlockState state) {
|
| 137 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 142 | super.animateTick(state, level, pos, random);
|
| 143 | BlockPos below = pos.below();
|
| 144 | BlockState belowState = level.getBlockState(below);
|
| 145 | makeDrippingWaterParticles(level, pos, random, belowState, below);
|
| 146 | this.ambientLeavesBlockSoundPlayer.playAmbientLeavesSounds(level, pos, this, random);
|
| 147 | }
|
| 148 |
|
| 149 | private static void makeDrippingWaterParticles(
|
| 150 | final Level level, final BlockPos pos, final RandomSource random, final BlockState belowState, final BlockPos below
|
| 151 | ) {
|
| 152 | if (level.isRainingAt(pos.above())) {
|
| 153 | if (random.nextInt(15) == 1) {
|
| 154 | if (!belowState.canOcclude() || !belowState.isFaceSturdy(level, below, Direction.UP)) {
|
| 155 | ParticleUtils.spawnParticleBelow(level, pos, random, ParticleTypes.DRIPPING_WATER);
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 163 | builder.add(DISTANCE, PERSISTENT, WATERLOGGED);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 168 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 169 | BlockState state = this.defaultBlockState().setValue(PERSISTENT, true).setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 170 | return updateDistance(state, context.getLevel(), context.getClickedPos());
|
| 171 | }
|
| 172 | }
|
| 173 |
|