User Tools

Site Tools


tutorial:directionalblock

Making a Directional Block

Making blocks directional (facing into certain directions) is also done using block states. This example describes a vertical version of the andesite slab.

VerticalSlabBlock.java
public class VerticalSlabBlock extends HorizontalFacingBlock {
	// the codec is required since 1.20.5 however not actually used in Minecraft yet.
	public static final MapCodec<VerticalSlabBlock> CODEC = Block.createCodec(VerticalSlabBlock::new);
 
	public VerticalSlabBlock(Settings settings) {
		super(settings);
		setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
	}
 
	@Override
	protected MapCodec<? extends VerticalSlabBlock> getCodec() {
		return CODEC;
	}
 
	@Override
	protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
		builder.add(Properties.HORIZONTAL_FACING);
	}
 
	@Override
	public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
		Direction dir = state.get(FACING);
		return switch (dir) {
			case NORTH -> VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f);
			case SOUTH -> VoxelShapes.cuboid(0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f);
			case EAST -> VoxelShapes.cuboid(0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
			case WEST -> VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f);
			default -> VoxelShapes.fullCube();
		};
	}
 
        @Override
	public BlockState getPlacementState(ItemPlacementContext ctx) {
		return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite());
	}
 
}

and then register the block according to the method covered in blocks:

TutorialBlocks.java
public final class TutorialBlocks implements ModInitializer {
    [...]
 
    public static final VerticalSlabBlock POLISHED_ANDESITE_VERTICAL_SLAB = register("polished_andesite_vertical_slab", 
        new VerticalSlabBlock(Block.Settings.copy(Blocks.POLISHED_ANDESITE)));
}

Defining Blockstate JSON

src/main/resources/assets/tutorial/blockstates/polished_andesite_vertical_slab.json
{
  "variants": {
    "facing=north": { "model": "tutorial:block/polished_andesite_vertical_slab", "uvlock": true },
    "facing=east":  { "model": "tutorial:block/polished_andesite_vertical_slab", "y":  90, "uvlock": true },
    "facing=south": { "model": "tutorial:block/polished_andesite_vertical_slab", "y": 180, "uvlock": true },
    "facing=west":  { "model": "tutorial:block/polished_andesite_vertical_slab", "y": 270, "uvlock": true }
  }
}

Defining Block Models

src/main/resources/assets/tutorial/models/block/vertical_slab.json
{   "parent": "block/block",
    "textures": {
        "particle": "#side"
    },
    "elements": [
        {   "from": [ 0, 0, 0 ],
            "to": [  16, 16, 8 ],
            "faces": {
                "down":  { "texture": "#bottom", "cullface": "down" },
                "up":    { "texture": "#top",    "cullface": "up" },
                "north": { "texture": "#side",   "cullface": "north" },
                "south": { "texture": "#side",   "cullface": "south" },
                "west":  { "texture": "#side",   "cullface": "west" },
                "east":  { "texture": "#side",   "cullface": "east" }
            }
        }
    ]
}
src/main/resources/assets/tutorial/models/block/polished_andesite_vertical_slab.json
{
    "parent": "tutorial:block/vertical_slab",
    "textures": {
        "bottom": "block/polished_andesite",
        "top": "block/polished_andesite",
        "side": "block/polished_andesite"
    }
}

Defining rotation and mirroring of blocks

For directional blocks, you may have to override rotate and mirror methods, so that in structure blocks, they can be correctly rotated or mirrored. However, in this case, the HorizontalFacingBlock class has already done it for you.

Next

Try to make it waterloggable.

tutorial/directionalblock.txt · Last modified: 2024/08/26 01:28 by solidblock