180 lines
7.3 KiB
Java
180 lines
7.3 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.serialization.MapCodec
|
|
*/
|
|
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import java.util.OptionalInt;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.core.Vec3i;
|
|
import net.minecraft.core.particles.ParticleTypes;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.tags.BlockTags;
|
|
import net.minecraft.util.ParticleUtils;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.ScheduledTickAccess;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
|
|
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.level.material.FluidState;
|
|
import net.minecraft.world.level.material.Fluids;
|
|
import net.minecraft.world.phys.shapes.Shapes;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
public abstract class LeavesBlock
|
|
extends Block
|
|
implements SimpleWaterloggedBlock {
|
|
public static final int DECAY_DISTANCE = 7;
|
|
public static final IntegerProperty DISTANCE = BlockStateProperties.DISTANCE;
|
|
public static final BooleanProperty PERSISTENT = BlockStateProperties.PERSISTENT;
|
|
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
|
protected final float leafParticleChance;
|
|
private static final int TICK_DELAY = 1;
|
|
|
|
public abstract MapCodec<? extends LeavesBlock> codec();
|
|
|
|
public LeavesBlock(float leafParticleChance, BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.leafParticleChance = leafParticleChance;
|
|
this.registerDefaultState((BlockState)((BlockState)((BlockState)((BlockState)this.stateDefinition.any()).setValue(DISTANCE, 7)).setValue(PERSISTENT, false)).setValue(WATERLOGGED, false));
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) {
|
|
return Shapes.empty();
|
|
}
|
|
|
|
@Override
|
|
protected boolean isRandomlyTicking(BlockState state) {
|
|
return state.getValue(DISTANCE) == 7 && state.getValue(PERSISTENT) == false;
|
|
}
|
|
|
|
@Override
|
|
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
if (this.decaying(state)) {
|
|
LeavesBlock.dropResources(state, level, pos);
|
|
level.removeBlock(pos, false);
|
|
}
|
|
}
|
|
|
|
protected boolean decaying(BlockState state) {
|
|
return state.getValue(PERSISTENT) == false && state.getValue(DISTANCE) == 7;
|
|
}
|
|
|
|
@Override
|
|
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
level.setBlock(pos, LeavesBlock.updateDistance(state, level, pos), 3);
|
|
}
|
|
|
|
@Override
|
|
protected int getLightBlock(BlockState state) {
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess ticks, BlockPos pos, Direction directionToNeighbour, BlockPos neighbourPos, BlockState neighbourState, RandomSource random) {
|
|
int distanceFromNeighbor;
|
|
if (state.getValue(WATERLOGGED).booleanValue()) {
|
|
ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
|
}
|
|
if ((distanceFromNeighbor = LeavesBlock.getDistanceAt(neighbourState) + 1) != 1 || state.getValue(DISTANCE) != distanceFromNeighbor) {
|
|
ticks.scheduleTick(pos, this, 1);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
private static BlockState updateDistance(BlockState state, LevelAccessor level, BlockPos pos) {
|
|
int newDistance = 7;
|
|
BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
|
|
for (Direction direction : Direction.values()) {
|
|
neighborPos.setWithOffset((Vec3i)pos, direction);
|
|
newDistance = Math.min(newDistance, LeavesBlock.getDistanceAt(level.getBlockState(neighborPos)) + 1);
|
|
if (newDistance == 1) break;
|
|
}
|
|
return (BlockState)state.setValue(DISTANCE, newDistance);
|
|
}
|
|
|
|
private static int getDistanceAt(BlockState state) {
|
|
return LeavesBlock.getOptionalDistanceAt(state).orElse(7);
|
|
}
|
|
|
|
public static OptionalInt getOptionalDistanceAt(BlockState state) {
|
|
if (state.is(BlockTags.LOGS)) {
|
|
return OptionalInt.of(0);
|
|
}
|
|
if (state.hasProperty(DISTANCE)) {
|
|
return OptionalInt.of(state.getValue(DISTANCE));
|
|
}
|
|
return OptionalInt.empty();
|
|
}
|
|
|
|
@Override
|
|
protected FluidState getFluidState(BlockState state) {
|
|
if (state.getValue(WATERLOGGED).booleanValue()) {
|
|
return Fluids.WATER.getSource(false);
|
|
}
|
|
return super.getFluidState(state);
|
|
}
|
|
|
|
@Override
|
|
public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) {
|
|
super.animateTick(state, level, pos, random);
|
|
BlockPos below = pos.below();
|
|
BlockState belowState = level.getBlockState(below);
|
|
LeavesBlock.makeDrippingWaterParticles(level, pos, random, belowState, below);
|
|
this.makeFallingLeavesParticles(level, pos, random, belowState, below);
|
|
}
|
|
|
|
private static void makeDrippingWaterParticles(Level level, BlockPos pos, RandomSource random, BlockState belowState, BlockPos below) {
|
|
if (!level.isRainingAt(pos.above())) {
|
|
return;
|
|
}
|
|
if (random.nextInt(15) != 1) {
|
|
return;
|
|
}
|
|
if (belowState.canOcclude() && belowState.isFaceSturdy(level, below, Direction.UP)) {
|
|
return;
|
|
}
|
|
ParticleUtils.spawnParticleBelow(level, pos, random, ParticleTypes.DRIPPING_WATER);
|
|
}
|
|
|
|
private void makeFallingLeavesParticles(Level level, BlockPos pos, RandomSource random, BlockState belowState, BlockPos below) {
|
|
if (random.nextFloat() >= this.leafParticleChance) {
|
|
return;
|
|
}
|
|
if (LeavesBlock.isFaceFull(belowState.getCollisionShape(level, below), Direction.UP)) {
|
|
return;
|
|
}
|
|
this.spawnFallingLeavesParticle(level, pos, random);
|
|
}
|
|
|
|
protected abstract void spawnFallingLeavesParticle(Level var1, BlockPos var2, RandomSource var3);
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(DISTANCE, PERSISTENT, WATERLOGGED);
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
|
BlockState state = (BlockState)((BlockState)this.defaultBlockState().setValue(PERSISTENT, true)).setValue(WATERLOGGED, replacedFluidState.getType() == Fluids.WATER);
|
|
return LeavesBlock.updateDistance(state, context.getLevel(), context.getClickedPos());
|
|
}
|
|
}
|
|
|