package net.minecraft.world.level.block; import com.mojang.serialization.Codec; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import java.util.Map; import net.minecraft.core.BlockPos; import net.minecraft.util.StringRepresentable; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; 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.block.state.properties.RotationSegment; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; public class SkullBlock extends AbstractSkullBlock { public static final MapCodec CODEC = RecordCodecBuilder.mapCodec( instance -> instance.group(SkullBlock.Type.CODEC.fieldOf("kind").forGetter(AbstractSkullBlock::getType), propertiesCodec()).apply(instance, SkullBlock::new) ); public static final int MAX = RotationSegment.getMaxSegmentIndex(); private static final int ROTATIONS = MAX + 1; public static final IntegerProperty ROTATION = BlockStateProperties.ROTATION_16; protected static final VoxelShape SHAPE = Block.box(4.0, 0.0, 4.0, 12.0, 8.0, 12.0); protected static final VoxelShape PIGLIN_SHAPE = Block.box(3.0, 0.0, 3.0, 13.0, 8.0, 13.0); @Override public MapCodec codec() { return CODEC; } protected SkullBlock(SkullBlock.Type type, BlockBehaviour.Properties properties) { super(type, properties); this.registerDefaultState(this.defaultBlockState().setValue(ROTATION, 0)); } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return this.getType() == SkullBlock.Types.PIGLIN ? PIGLIN_SHAPE : SHAPE; } @Override protected VoxelShape getOcclusionShape(BlockState blockState) { return Shapes.empty(); } @Override public BlockState getStateForPlacement(BlockPlaceContext context) { return super.getStateForPlacement(context).setValue(ROTATION, RotationSegment.convertToSegment(context.getRotation())); } @Override protected BlockState rotate(BlockState state, Rotation rotation) { return state.setValue(ROTATION, rotation.rotate((Integer)state.getValue(ROTATION), ROTATIONS)); } @Override protected BlockState mirror(BlockState state, Mirror mirror) { return state.setValue(ROTATION, mirror.mirror((Integer)state.getValue(ROTATION), ROTATIONS)); } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { super.createBlockStateDefinition(builder); builder.add(ROTATION); } public interface Type extends StringRepresentable { Map TYPES = new Object2ObjectArrayMap<>(); Codec CODEC = Codec.stringResolver(StringRepresentable::getSerializedName, TYPES::get); } public static enum Types implements SkullBlock.Type { SKELETON("skeleton"), WITHER_SKELETON("wither_skeleton"), PLAYER("player"), ZOMBIE("zombie"), CREEPER("creeper"), PIGLIN("piglin"), DRAGON("dragon"); private final String name; private Types(final String name) { this.name = name; TYPES.put(name, this); } @Override public String getSerializedName() { return this.name; } } }