/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.mojang.datafixers.kinds.App * com.mojang.datafixers.kinds.Applicative * com.mojang.serialization.MapCodec * com.mojang.serialization.codecs.RecordCodecBuilder * org.jspecify.annotations.Nullable */ package net.minecraft.world.level.block; import com.mojang.datafixers.kinds.App; import com.mojang.datafixers.kinds.Applicative; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; 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.LevelReader; import net.minecraft.world.level.ScheduledTickAccess; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.SaplingBlock; import net.minecraft.world.level.block.SimpleWaterloggedBlock; import net.minecraft.world.level.block.grower.TreeGrower; 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.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; import org.jspecify.annotations.Nullable; public class MangrovePropaguleBlock extends SaplingBlock implements SimpleWaterloggedBlock { public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(i -> i.group((App)TreeGrower.CODEC.fieldOf("tree").forGetter(b -> b.treeGrower), MangrovePropaguleBlock.propertiesCodec()).apply((Applicative)i, MangrovePropaguleBlock::new)); public static final IntegerProperty AGE = BlockStateProperties.AGE_4; public static final int MAX_AGE = 4; private static final int[] SHAPE_MIN_Y = new int[]{13, 10, 7, 3, 0}; private static final VoxelShape[] SHAPE_PER_AGE = Block.boxes(4, age -> Block.column(2.0, SHAPE_MIN_Y[age], 16.0)); private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final BooleanProperty HANGING = BlockStateProperties.HANGING; public MapCodec codec() { return CODEC; } public MangrovePropaguleBlock(TreeGrower treeGrower, BlockBehaviour.Properties properties) { super(treeGrower, properties); this.registerDefaultState((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)this.stateDefinition.any()).setValue(STAGE, 0)).setValue(AGE, 0)).setValue(WATERLOGGED, false)).setValue(HANGING, false)); } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(STAGE).add(AGE).add(WATERLOGGED).add(HANGING); } @Override protected boolean mayPlaceOn(BlockState state, BlockGetter level, BlockPos pos) { return super.mayPlaceOn(state, level, pos) || state.is(Blocks.CLAY); } @Override public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) { FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos()); boolean isWaterSource = replacedFluidState.getType() == Fluids.WATER; return (BlockState)((BlockState)super.getStateForPlacement(context).setValue(WATERLOGGED, isWaterSource)).setValue(AGE, 4); } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { int age = state.getValue(HANGING) != false ? state.getValue(AGE) : 4; return SHAPE_PER_AGE[age].move(state.getOffset(pos)); } @Override protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { if (MangrovePropaguleBlock.isHanging(state)) { return level.getBlockState(pos.above()).is(Blocks.MANGROVE_LEAVES); } return super.canSurvive(state, level, pos); } @Override protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess ticks, BlockPos pos, Direction directionToNeighbour, BlockPos neighbourPos, BlockState neighbourState, RandomSource random) { if (state.getValue(WATERLOGGED).booleanValue()) { ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } if (directionToNeighbour == Direction.UP && !state.canSurvive(level, pos)) { return Blocks.AIR.defaultBlockState(); } return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random); } @Override protected FluidState getFluidState(BlockState state) { if (state.getValue(WATERLOGGED).booleanValue()) { return Fluids.WATER.getSource(false); } return super.getFluidState(state); } @Override protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { if (!MangrovePropaguleBlock.isHanging(state)) { if (random.nextInt(7) == 0) { this.advanceTree(level, pos, state, random); } return; } if (!MangrovePropaguleBlock.isFullyGrown(state)) { level.setBlock(pos, (BlockState)state.cycle(AGE), 2); } } @Override public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) { return !MangrovePropaguleBlock.isHanging(state) || !MangrovePropaguleBlock.isFullyGrown(state); } @Override public boolean isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) { return MangrovePropaguleBlock.isHanging(state) ? !MangrovePropaguleBlock.isFullyGrown(state) : super.isBonemealSuccess(level, random, pos, state); } @Override public void performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) { if (MangrovePropaguleBlock.isHanging(state) && !MangrovePropaguleBlock.isFullyGrown(state)) { level.setBlock(pos, (BlockState)state.cycle(AGE), 2); } else { super.performBonemeal(level, random, pos, state); } } private static boolean isHanging(BlockState state) { return state.getValue(HANGING); } private static boolean isFullyGrown(BlockState state) { return state.getValue(AGE) == 4; } public static BlockState createNewHangingPropagule() { return MangrovePropaguleBlock.createNewHangingPropagule(0); } public static BlockState createNewHangingPropagule(int age) { return (BlockState)((BlockState)Blocks.MANGROVE_PROPAGULE.defaultBlockState().setValue(HANGING, true)).setValue(AGE, age); } }