zh_cn:tutorial:directionalblock
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:directionalblock [2022/02/19 01:53] – [定义方块状态] style: code formatting xtexchooser | zh_cn:tutorial:directionalblock [2024/12/08 14:06] (current) – [下一步] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 带有方向的方块 ==== | + | ====== 制作带朝向的方块 |
- | 使方块带有方向(朝向特定的方向)也是通过方块状态完成的。 | + | 使方块带有方向(朝向特定的方向)也是通过方块状态完成的。这个例子介绍了垂直版的安山岩台阶。 |
- | 这个例子介绍了垂直版的安山岩台阶。 | + | |
{{: | {{: | ||
- | <code java> | + | < |
- | public class PolishedAndesiteSideBlock | + | public class VerticalSlabBlock |
+ | // codec 从 1.20.5 开始是必需的,但是还没有在 Minecraft 中实际使用。 | ||
+ | public static final MapCodec< | ||
- | public | + | public |
super(settings); | super(settings); | ||
- | setDefaultState(this.stateManager.getDefaultState().with(Properties.HORIZONTAL_FACING, | + | setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, |
} | } | ||
@Override | @Override | ||
- | protected | + | protected |
- | stateManager.add(Properties.HORIZONTAL_FACING); | + | return CODEC; |
} | } | ||
@Override | @Override | ||
- | public VoxelShape getOutlineShape(BlockState state, BlockView | + | protected void appendProperties(StateManager.Builder< |
+ | builder.add(Properties.HORIZONTAL_FACING); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public VoxelShape getOutlineShape(BlockState state, BlockView | ||
Direction dir = state.get(FACING); | Direction dir = state.get(FACING); | ||
- | switch(dir) { | + | return |
- | case NORTH: | + | case NORTH -> VoxelShapes.cuboid(0.0f, |
- | return | + | case SOUTH -> VoxelShapes.cuboid(0.0f, |
- | case SOUTH: | + | case EAST -> VoxelShapes.cuboid(0.5f, |
- | return | + | case WEST -> VoxelShapes.cuboid(0.0f, |
- | case EAST: | + | default |
- | return | + | }; |
- | case WEST: | + | |
- | return | + | |
- | default: | + | |
- | return | + | |
- | } | + | |
} | } | ||
+ | @Override | ||
public BlockState getPlacementState(ItemPlacementContext ctx) { | public BlockState getPlacementState(ItemPlacementContext ctx) { | ||
- | return | + | return |
} | } | ||
Line 43: | Line 45: | ||
</ | </ | ||
+ | 然后按照在 [[blocks]] 中提到的方法,注册方块: | ||
+ | <code java TutorialBlocks.java> | ||
+ | public final class TutorialBlocks implements ModInitializer { | ||
+ | [...] | ||
+ | | ||
+ | public static final VerticalSlabBlock POLISHED_ANDESITE_VERTICAL_SLAB = register(" | ||
+ | new VerticalSlabBlock(Block.Settings.copy(Blocks.POLISHED_ANDESITE))); | ||
+ | } | ||
+ | </ | ||
- | ==== 定义方块状态 ==== | + | ==== 定义方块状态 |
- | polished_andesite_side_block.json | + | < |
- | < | + | |
{ | { | ||
" | " | ||
- | " | + | " |
- | " | + | " |
- | " | + | " |
- | " | + | " |
} | } | ||
} | } | ||
</ | </ | ||
- | |||
==== 定义方块模型 ==== | ==== 定义方块模型 ==== | ||
- | + | < | |
- | side.json | + | |
- | < | + | |
{ " | { " | ||
" | " | ||
Line 72: | Line 79: | ||
" | " | ||
" | " | ||
- | " | + | " |
- | " | + | " |
- | " | + | " |
- | " | + | " |
" | " | ||
" | " | ||
Line 84: | Line 91: | ||
</ | </ | ||
- | polished_andesite_side_block.json | + | < |
- | < | + | |
{ | { | ||
- | " | + | " |
" | " | ||
" | " | ||
Line 95: | Line 101: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== 定义方块的旋转和翻转 ===== | ||
+ | 对于带有朝向的方块,你需要覆盖 '' | ||
+ | |||
+ | |||
+ | ===== 影响寻路 ===== | ||
+ | 如果在游戏内放置这些方块,可能会发现个问题,生物寻路时,会尝试这些方块,似乎这些方块不存在,结果被这些方块阻挡。这是因为,生物会将这些非完整的方块视为不挡路的方块。要修改这一寻路行为,需要修改 '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | protected boolean canPathfindThrough(BlockState state, NavigationType type) { | ||
+ | return false; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 在原版中,有些方块有不同的路径节点类型(path node type)。例如,生物会避免危险的方块,比如岩浆块、凋零玫瑰和仙人掌。你也可以让你的生物以不同方式对待这些方块,方法就是利用 Fabric API 中的 '' | ||
+ | |||
+ | ===== 下一步 ===== | ||
+ | 尝试让它[[waterloggable|可含水]] | ||
+ |
zh_cn/tutorial/directionalblock.1645235634.txt.gz · Last modified: 2022/02/19 01:53 by xtexchooser