User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

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.

public class VerticalSlabBlock extends HorizontalFacingBlock {
 
	public VerticalSlabBlock(Settings settings) {
		super(settings);
		setDefaultState(this.stateManager.getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
	}
 
	@Override
	protected void appendProperties(StateManager.Builder<Block, BlockState> stateManager) {
		stateManager.add(Properties.HORIZONTAL_FACING);
	}
 
	@Override
	public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ctx) {
		Direction dir = state.get(FACING);
		switch(dir) {
			case NORTH:
				return VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f);
			case SOUTH:
				return VoxelShapes.cuboid(0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f);
			case EAST:
				return VoxelShapes.cuboid(0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
			case WEST:
				return VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f);
			default:
				return VoxelShapes.fullCube();
		}
	}
 
        @Override
	public BlockState getPlacementState(ItemPlacementContext ctx) {
		return (BlockState)this.getDefaultState().with(Properties.HORIZONTAL_FACING, ctx.getPlayerFacing().getOpposite());
	}
 
}

Defining Blockstate

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

Defining Block Models

src/main/resources/assets/tutorial/models/block/side.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_side_block.json
{
    "parent": "tutorial:block/side",
    "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 (and structure-related mods like WorldEdit), they can be correctly rotated or mirrored. However, in this case, the HorizontalFacingBlock class has already done it for you.

tutorial/directionalblock.txt · Last modified: 2022/01/31 01:23 (external edit)