131 lines
4.5 KiB
Java
131 lines
4.5 KiB
Java
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.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition.Builder;
|
|
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.jetbrains.annotations.Nullable;
|
|
|
|
public class CocoaBlock extends HorizontalDirectionalBlock implements BonemealableBlock {
|
|
public static final MapCodec<CocoaBlock> CODEC = 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, (i - 5) / 16.0).optimize()))
|
|
.toList();
|
|
|
|
@Override
|
|
public MapCodec<CocoaBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public CocoaBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(AGE, 0));
|
|
}
|
|
|
|
@Override
|
|
protected boolean isRandomlyTicking(BlockState state) {
|
|
return (Integer)state.getValue(AGE) < 2;
|
|
}
|
|
|
|
@Override
|
|
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
if (level.random.nextInt(5) == 0) {
|
|
int i = (Integer)state.getValue(AGE);
|
|
if (i < 2) {
|
|
level.setBlock(pos, state.setValue(AGE, i + 1), 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
|
|
BlockState blockState = level.getBlockState(pos.relative(state.getValue(FACING)));
|
|
return blockState.is(BlockTags.JUNGLE_LOGS);
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return (VoxelShape)((Map)SHAPES.get((Integer)state.getValue(AGE))).get(state.getValue(FACING));
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
BlockState blockState = this.defaultBlockState();
|
|
LevelReader levelReader = context.getLevel();
|
|
BlockPos blockPos = context.getClickedPos();
|
|
|
|
for (Direction direction : context.getNearestLookingDirections()) {
|
|
if (direction.getAxis().isHorizontal()) {
|
|
blockState = blockState.setValue(FACING, direction);
|
|
if (blockState.canSurvive(levelReader, blockPos)) {
|
|
return blockState;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(
|
|
BlockState state,
|
|
LevelReader level,
|
|
ScheduledTickAccess scheduledTickAccess,
|
|
BlockPos pos,
|
|
Direction direction,
|
|
BlockPos neighborPos,
|
|
BlockState neighborState,
|
|
RandomSource random
|
|
) {
|
|
return direction == state.getValue(FACING) && !state.canSurvive(level, pos)
|
|
? Blocks.AIR.defaultBlockState()
|
|
: super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) {
|
|
return (Integer)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, state.setValue(AGE, (Integer)state.getValue(AGE) + 1), 2);
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(Builder<Block, BlockState> builder) {
|
|
builder.add(FACING, AGE);
|
|
}
|
|
|
|
@Override
|
|
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
|
|
return false;
|
|
}
|
|
}
|