How to make a block that faces the player in a Minecraft mod
A block that turns to face you has a FACING block state property: extend HorizontalDirectionalBlock (HorizontalBlock on 1.16.5, BlockHorizontal on 1.12.2), add FACING in createBlockStateDefinition (createBlockState on 1.12.2), and set it in getStateForPlacement from context.getHorizontalDirection().getOpposite(). Its blockstate file has a variant per direction that turns the model with “y”, and the orientable model gives it a front.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Extend HorizontalDirectionalBlock (with a codec on 1.20.3 to 26.2).
- Add FACING to the state definition and pick it when placed.
- Write a blockstate variant for each direction with a y rotation.
- Give it a model with a front, minecraft:block/orientable.
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric and NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20, Forge 1.17.1 to 1.20.1”Uses:
HorizontalDirectionalBlock.FACINGBlockBehaviour.getStateForPlacementBlock.createBlockStateDefinitionBlockPlaceContext.getHorizontalDirection
src/main/java/com/example/examplemod/block/CarvedCrateBlock.java
package com.example.examplemod.block;
import net.minecraft.core.Direction;import net.minecraft.world.item.context.BlockPlaceContext;import net.minecraft.world.level.block.Block;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;
// Carved Crate: placed facing the player; FACING (north, east, south, west) is a block state property, turned in the blockstate file.public class CarvedCrateBlock extends HorizontalDirectionalBlock { public CarvedCrateBlock(BlockBehaviour.Properties properties) { super(properties); registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH)); }
@Override public void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { builder.add(FACING); }
@Override public BlockState getStateForPlacement(BlockPlaceContext context) { return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()); }}src/main/resources/assets/examplemod/blockstates/carved_crate.json
{ "variants": { "facing=north": {"model": "examplemod:block/carved_crate"}, "facing=east": {"model": "examplemod:block/carved_crate", "y": 90}, "facing=south": {"model": "examplemod:block/carved_crate", "y": 180}, "facing=west": {"model": "examplemod:block/carved_crate", "y": 270} }}src/main/resources/assets/examplemod/models/block/carved_crate.json
{ "parent": "minecraft:block/orientable", "textures": { "front": "examplemod:block/carved_crate_front", "side": "examplemod:block/carved_crate", "top": "examplemod:block/carved_crate" }}It’s registered like any other block, as CarvedCrateBlock: see a new block.
Fabric and NeoForge 1.20.3 to 1.21.1, Fabric and NeoForge 1.21.2 to 26.2
Section titled “Fabric and NeoForge 1.20.3 to 1.21.1, Fabric and NeoForge 1.21.2 to 26.2”Uses:
HorizontalDirectionalBlock.FACINGBlockBehaviour.getStateForPlacementBlock.createBlockStateDefinitionBlockPlaceContext.getHorizontalDirection
src/main/java/com/example/examplemod/block/CarvedCrateBlock.java
package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;import net.minecraft.core.Direction;import net.minecraft.world.item.context.BlockPlaceContext;import net.minecraft.world.level.block.Block;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;
// Carved Crate: placed facing the player; FACING (north, east, south, west) is a block state property, turned in the blockstate file.public class CarvedCrateBlock extends HorizontalDirectionalBlock { public static final MapCodec<CarvedCrateBlock> CODEC = simpleCodec(CarvedCrateBlock::new);
public CarvedCrateBlock(BlockBehaviour.Properties properties) { super(properties); registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH)); }
@Override protected MapCodec<? extends HorizontalDirectionalBlock> codec() { return CODEC; }
@Override protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { builder.add(FACING); }
@Override public BlockState getStateForPlacement(BlockPlaceContext context) { return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()); }}src/main/resources/assets/examplemod/blockstates/carved_crate.json
{ "variants": { "facing=north": {"model": "examplemod:block/carved_crate"}, "facing=east": {"model": "examplemod:block/carved_crate", "y": 90}, "facing=south": {"model": "examplemod:block/carved_crate", "y": 180}, "facing=west": {"model": "examplemod:block/carved_crate", "y": 270} }}src/main/resources/assets/examplemod/models/block/carved_crate.json
{ "parent": "minecraft:block/orientable", "textures": { "front": "examplemod:block/carved_crate_front", "side": "examplemod:block/carved_crate", "top": "examplemod:block/carved_crate" }}It’s registered like any other block, as CarvedCrateBlock: see a new block.
Fabric and NeoForge 26.3
Section titled “Fabric and NeoForge 26.3”Uses:
HorizontalDirectionalBlock.FACINGBlockBehaviour.getStateForPlacementBlock.createBlockStateDefinitionBlockPlaceContext.getHorizontalDirection
Since 26.3 a Block no longer has a codec: there is no CODEC field and no codec() method to write.
src/main/java/com/example/examplemod/block/CarvedCrateBlock.java
package com.example.examplemod.block;
import net.minecraft.core.Direction;import net.minecraft.world.item.context.BlockPlaceContext;import net.minecraft.world.level.block.Block;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;
// Carved Crate: placed facing the player; FACING (north, east, south, west) is a block state property, turned in the blockstate file.public class CarvedCrateBlock extends HorizontalDirectionalBlock { public CarvedCrateBlock(BlockBehaviour.Properties properties) { super(properties); registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH)); }
@Override protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { builder.add(FACING); }
@Override public BlockState getStateForPlacement(BlockPlaceContext context) { return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()); }}src/main/resources/assets/examplemod/blockstates/carved_crate.json
{ "variants": { "facing=north": {"model": "examplemod:block/carved_crate"}, "facing=east": {"model": "examplemod:block/carved_crate", "y": 90}, "facing=south": {"model": "examplemod:block/carved_crate", "y": 180}, "facing=west": {"model": "examplemod:block/carved_crate", "y": 270} }}src/main/resources/assets/examplemod/models/block/carved_crate.json
{ "parent": "minecraft:block/orientable", "textures": { "front": "examplemod:block/carved_crate_front", "side": "examplemod:block/carved_crate", "top": "examplemod:block/carved_crate" }}It’s registered like any other block, as CarvedCrateBlock: see a new block.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
HorizontalBlock.FACINGBlock.getStateForPlacementBlock.createBlockStateDefinitionBlockItemUseContext.getHorizontalDirection
src/main/java/com/example/examplemod/block/CarvedCrateBlock.java
package com.example.examplemod.block;
import net.minecraft.util.Direction;import net.minecraft.item.BlockItemUseContext;import net.minecraft.block.Block;import net.minecraft.block.HorizontalBlock;import net.minecraft.block.AbstractBlock;import net.minecraft.block.BlockState;import net.minecraft.state.StateContainer;
// Carved Crate: placed facing the player; FACING (north, east, south, west) is a block state property, turned in the blockstate file.public class CarvedCrateBlock extends HorizontalBlock { public CarvedCrateBlock(AbstractBlock.Properties properties) { super(properties); registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH)); }
@Override public void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> builder) { builder.add(FACING); }
@Override public BlockState getStateForPlacement(BlockItemUseContext context) { return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()); }}src/main/resources/assets/examplemod/blockstates/carved_crate.json
{ "variants": { "facing=north": {"model": "examplemod:block/carved_crate"}, "facing=east": {"model": "examplemod:block/carved_crate", "y": 90}, "facing=south": {"model": "examplemod:block/carved_crate", "y": 180}, "facing=west": {"model": "examplemod:block/carved_crate", "y": 270} }}src/main/resources/assets/examplemod/models/block/carved_crate.json
{ "parent": "minecraft:block/orientable", "textures": { "front": "examplemod:block/carved_crate_front", "side": "examplemod:block/carved_crate", "top": "examplemod:block/carved_crate" }}It’s registered like any other block, as CarvedCrateBlock: see a new block.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
BlockHorizontal.FACINGBlock.createBlockStateBlock.getStateForPlacementBlock.getMetaFromState
In 1.12.2 the facing is saved as block metadata through getMetaFromState and getStateFromMeta.
src/main/java/com/example/examplemod/block/CarvedCrateBlock.java
package com.example.examplemod.block;
import net.minecraft.block.BlockHorizontal;import net.minecraft.block.material.Material;import net.minecraft.block.state.BlockStateContainer;import net.minecraft.block.state.IBlockState;import net.minecraft.entity.EntityLivingBase;import net.minecraft.util.EnumFacing;import net.minecraft.util.EnumHand;import net.minecraft.util.math.BlockPos;import net.minecraft.world.World;
// Carved Crate: placed facing the player; FACING (north, east, south, west) is a block state property, turned in the blockstate file. A 1.12.2 state is saved as a metadata number.public class CarvedCrateBlock extends BlockHorizontal { public CarvedCrateBlock() { super(Material.ROCK); setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH)); }
@Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, FACING); }
@Override public IBlockState getStateFromMeta(int meta) { return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)); }
@Override public int getMetaFromState(IBlockState state) { return state.getValue(FACING).getHorizontalIndex(); }
@Override public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) { return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()); }}src/main/resources/assets/examplemod/blockstates/carved_crate.json
{ "variants": { "facing=north": {"model": "examplemod:carved_crate"}, "facing=east": {"model": "examplemod:carved_crate", "y": 90}, "facing=south": {"model": "examplemod:carved_crate", "y": 180}, "facing=west": {"model": "examplemod:carved_crate", "y": 270} }}src/main/resources/assets/examplemod/models/block/carved_crate.json
{ "parent": "minecraft:block/orientable", "textures": { "front": "examplemod:blocks/carved_crate_front", "side": "examplemod:blocks/carved_crate", "top": "examplemod:blocks/carved_crate" }}It’s registered like any other block, as CarvedCrateBlock: see a new block.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/blocks/states
- https://docs.neoforged.net/docs/1.21.1/resources/client/models/
- https://docs.fabricmc.net/1.21.1/develop/blocks/blockstates
Related how-tos
Section titled “Related how-tos”- A block that can hold water
- A block that changes over time with random ticks
- A block that does something when clicked
- A block that gives off light
- A block with the properties you choose
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.