minecraft-src/net/minecraft/world/level/block/AmethystClusterBlock.java
2025-07-04 03:45:38 +03:00

117 lines
4.5 KiB
Java

package net.minecraft.world.level.block;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Map;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
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.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
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.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class AmethystClusterBlock extends AmethystBlock implements SimpleWaterloggedBlock {
public static final MapCodec<AmethystClusterBlock> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
Codec.FLOAT.fieldOf("height").forGetter(amethystClusterBlock -> amethystClusterBlock.height),
Codec.FLOAT.fieldOf("width").forGetter(amethystClusterBlock -> amethystClusterBlock.width),
propertiesCodec()
)
.apply(instance, AmethystClusterBlock::new)
);
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final EnumProperty<Direction> FACING = BlockStateProperties.FACING;
private final float height;
private final float width;
private final Map<Direction, VoxelShape> shapes;
@Override
public MapCodec<AmethystClusterBlock> codec() {
return CODEC;
}
public AmethystClusterBlock(float height, float aabbOffset, BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.defaultBlockState().setValue(WATERLOGGED, false).setValue(FACING, Direction.UP));
this.shapes = Shapes.rotateAll(Block.boxZ(aabbOffset, 16.0F - height, 16.0));
this.height = height;
this.width = aabbOffset;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return (VoxelShape)this.shapes.get(state.getValue(FACING));
}
@Override
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
Direction direction = state.getValue(FACING);
BlockPos blockPos = pos.relative(direction.getOpposite());
return level.getBlockState(blockPos).isFaceSturdy(level, blockPos, direction);
}
@Override
protected BlockState updateShape(
BlockState state,
LevelReader level,
ScheduledTickAccess scheduledTickAccess,
BlockPos pos,
Direction direction,
BlockPos neighborPos,
BlockState neighborState,
RandomSource random
) {
if ((Boolean)state.getValue(WATERLOGGED)) {
scheduledTickAccess.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return direction == ((Direction)state.getValue(FACING)).getOpposite() && !state.canSurvive(level, pos)
? Blocks.AIR.defaultBlockState()
: super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
LevelAccessor levelAccessor = context.getLevel();
BlockPos blockPos = context.getClickedPos();
return this.defaultBlockState()
.setValue(WATERLOGGED, levelAccessor.getFluidState(blockPos).getType() == Fluids.WATER)
.setValue(FACING, context.getClickedFace());
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}
@Override
protected FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> builder) {
builder.add(WATERLOGGED, FACING);
}
}