User Tools

Site Tools


Sidebar

← 返回首页

Fabric 教程

安装

基础

物品

方块和方块实体

数据生成

世界生成

命令

事件

实体

流体

Mixin 和 ASM

杂项

Yarn

贡献 Fabric

zh_cn:tutorial:directionalblock

制作带朝向的方块

使方块带有方向(朝向特定的方向)也是通过方块状态完成的。这个例子介绍了垂直版的安山岩台阶。

public class VerticalSlabBlock extends HorizontalFacingBlock {
 
	public VerticalSlabBlock(Settings settings) {
		super(settings);
		setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
	}
 
	@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);
		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 super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getPlayerFacing().getOpposite());
	}
 
}
public class ExampleMod implements ModInitialzer {
    public static final VerticalSlabBlock POLISHED_ANDESITE_VERTICAL_SLAB = Registry.register(
        Registries.BLOCK,
        new Identifier("tutorial", "polished_andesite_vertical_slab"),
        new VerticalSlabBlock(FabricBlockSettings.copyOf(Blocks.POLISHED_ANDESITE)));
}

定义方块状态 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 }
  }
}

定义方块模型

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"
    }
}

定义方块的旋转和翻转

对于带有朝向的方块,你需要覆盖 rotatemirror 方法,这样在结构方块中可以正确地被旋转或者翻转。不过,在这个例子中,HorizontalFacingBlock 类已经帮你做好了。

下一步

尝试让它可含水

zh_cn/tutorial/directionalblock.txt · Last modified: 2022/12/16 02:09 by solidblock