Skip to content

Filling out the Block Family File

The Ghost in the Machine edited this page Dec 13, 2017 · 5 revisions

We'll need to fill out the methods in the skeleton code from the last tutorial. Here's how we do it:

Annotations

To make this block family work, you must register you block family and it's sections in the block family file. To do this, place these annotations above your class header like this:

@RegisterBlockFamily("arrow")
@BlockSections({"no_connections", "left_end", "right_end", "front_end", "back_end", "line_connection", "2d_corner", "2d_t", "cross"})
public class ArrowBlockFamily extends MultiConnectFamily {

Constructor

Here's what our constructor will look like, I'll walk through the code below:

public ArrowBlockFamily(BlockFamilyDefinition definition, BlockBuilderHelper blockBuilder) {
    super(definition, blockBuilder);

    BlockUri blockUri = new BlockUri(definition.getUrn());
        
    this.registerBlock(blockUri, definition, blockBuilder, "no_connections", (byte) 0, Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "left_end", SideBitFlag.getSide(Side.RIGHT), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "right_end", SideBitFlag.getSide(Side.LEFT), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "front_end", SideBitFlag.getSide(Side.BACK), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "back_end", SideBitFlag.getSide(Side.FRONT), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "line_connection", SideBitFlag.getSides(Side.LEFT, Side.RIGHT), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "2d_corner", SideBitFlag.getSides(Side.LEFT, Side.FRONT), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "2d_corner", SideBitFlag.getSides(Side.LEFT, Side.FRONT, Side.RIGHT), Rotation.horizontalRotations());
    this.registerBlock(blockUri, definition, blockBuilder, "cross", SideBitFlag.getSides(Side.LEFT, Side.FRONT, Side.RIGHT, Side.BACK), Rotation.horizontalRotations());
}
  1. Call the super constructor, as is required by java
  2. Get our block URI
  3. Register all of our blocks.

Just a note about registerBlock, when you register a block, the registerBlock method finds all the rotations possible for that shape. So you only have to worry about the base shapes and registerBlock will take care of the rest for you.

Connection Condition

The connectionCondition method is called when MultiConnectFamily needs to find out if a given block should connect on a given side. The simplest way to do this is to check if the neighboring block is of our block family:

@Override
protected boolean connectionCondition(Vector3i blockLocation, Side connectSide) {
    Vector3i neighborLocation = new Vector3i(blockLocation);
    neighborLocation.add(connectSide.getVector3i());
    if (worldProvider.isBlockRelevant(neighborLocation)) {
        Block neighborBlock = worldProvider.getBlock(neighborLocation);
        final BlockFamily blockFamily = neighborBlock.getBlockFamily();
        if (blockFamily instanceof ArrowBlockFamily)
            return true;
    }
    return false;
}

Connection Sides

getConnectionSides should return what sides it is possible for our block to connect on. Our block is going to be horizontal only, so we have this:

@Override
public byte getConnectionSides() {
    return SideBitFlag.getSides(Side.FRONT, Side.BACK, Side.LEFT, Side.RIGHT);
}

Archetype Block

The archetype is the block that is shown in the player's inventory. Ours will be the "no_connections" block:

@Override
public Block getArchetypeBlock() {
    return blocks.get((byte) 0);
}

Clone this wiki locally