2025-11-24 22:52:51 +03:00

126 lines
5.1 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.serialization.MapCodec
* org.jspecify.annotations.Nullable
*/
package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
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.BonemealableBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
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.IntegerProperty;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jspecify.annotations.Nullable;
public class CocoaBlock
extends HorizontalDirectionalBlock
implements BonemealableBlock {
public static final MapCodec<CocoaBlock> CODEC = CocoaBlock.simpleCodec(CocoaBlock::new);
public static final int MAX_AGE = 2;
public static final IntegerProperty AGE = BlockStateProperties.AGE_2;
private static final List<Map<Direction, VoxelShape>> SHAPES = IntStream.rangeClosed(0, 2).mapToObj(i -> Shapes.rotateHorizontal(Block.column(4 + i * 2, 7 - i * 2, 12.0).move(0.0, 0.0, (double)(i - 5) / 16.0).optimize())).toList();
public MapCodec<CocoaBlock> codec() {
return CODEC;
}
public CocoaBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState((BlockState)((BlockState)((BlockState)this.stateDefinition.any()).setValue(FACING, Direction.NORTH)).setValue(AGE, 0));
}
@Override
protected boolean isRandomlyTicking(BlockState state) {
return state.getValue(AGE) < 2;
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
int age;
if (level.random.nextInt(5) == 0 && (age = state.getValue(AGE).intValue()) < 2) {
level.setBlock(pos, (BlockState)state.setValue(AGE, age + 1), 2);
}
}
@Override
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
BlockState relativeState = level.getBlockState(pos.relative((Direction)state.getValue(FACING)));
return relativeState.is(BlockTags.JUNGLE_LOGS);
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPES.get(state.getValue(AGE)).get(state.getValue(FACING));
}
@Override
public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) {
BlockState state = this.defaultBlockState();
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
for (Direction direction : context.getNearestLookingDirections()) {
if (!direction.getAxis().isHorizontal() || !(state = (BlockState)state.setValue(FACING, direction)).canSurvive(level, pos)) continue;
return state;
}
return null;
}
@Override
protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess ticks, BlockPos pos, Direction directionToNeighbour, BlockPos neighbourPos, BlockState neighbourState, RandomSource random) {
if (directionToNeighbour == state.getValue(FACING) && !state.canSurvive(level, pos)) {
return Blocks.AIR.defaultBlockState();
}
return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
}
@Override
public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) {
return state.getValue(AGE) < 2;
}
@Override
public boolean isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) {
return true;
}
@Override
public void performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) {
level.setBlock(pos, (BlockState)state.setValue(AGE, state.getValue(AGE) + 1), 2);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, AGE);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType type) {
return false;
}
}