User Tools

Site Tools


zh_cn:tutorial:directionalblock

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
zh_cn:tutorial:directionalblock [2020/11/12 13:41] – created solidblockzh_cn:tutorial:directionalblock [2024/12/08 14:06] (current) – [下一步] solidblock
Line 1: Line 1:
-==== 带有方向的方块 ====+====== 制作向的方块 ======
  
-使方块带有方向(朝向特定的方向)也是通过方块状态完成的。 +使方块带有方向(朝向特定的方向)也是通过方块状态完成的。这个例子介绍了垂直版的安山岩台阶。
-这个例子介绍了垂直版的安山岩台阶。+
  
 {{:tutorial:vertslab.png?200|}} {{:tutorial:vertslab.png?200|}}
  
-<code java> +<code java VerticalSlabBlock.java> 
-public class PolishedAndesiteSideBlock extends HorizontalFacingBlock {+public class VerticalSlabBlock extends HorizontalFacingBlock { 
 + // codec 从 1.20.5 开始是必需的,但是还没有在 Minecraft 中实际使用。 
 + public static final MapCodec<VerticalSlabBlock> CODEC = Block.createCodec(VerticalSlabBlock::new);
  
- public PolishedAndesiteSideBlock(Settings settings) {+ public VerticalSlabBlock(Settings settings) {
  super(settings);  super(settings);
- setDefaultState(this.stateManager.getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));+ setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
  }  }
  
  @Override  @Override
- protected void appendProperties(StateManager.Builder<Block, BlockStatestateManager) { + protected MapCodec<? extends VerticalSlabBlockgetCodec() { 
- stateManager.add(Properties.HORIZONTAL_FACING);+ return CODEC;
  }  }
  
  @Override  @Override
- public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, EntityContext ctx) {+ 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);  Direction dir = state.get(FACING);
- switch(dir) { + return switch (dir) { 
- case NORTH+ case NORTH -> VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f); 
- return 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 SOUTH+ case EAST -> VoxelShapes.cuboid(0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f); 
- return VoxelShapes.cuboid(0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f); + case WEST -> VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f); 
- case EAST+ default -> VoxelShapes.fullCube(); 
- 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) {  public BlockState getPlacementState(ItemPlacementContext ctx) {
- return (BlockState)this.getDefaultState().with(FACING, ctx.getPlayerFacing());+ return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite());
  }  }
  
Line 43: Line 45:
 </code> </code>
  
 +然后按照在 [[blocks]] 中提到的方法,注册方块:
 +<code java 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)));
 +}
 +</code>
  
-==== 定义方块状态 ====+==== 定义方块状态 JSON ====
  
-polished_andesite_side_block.json +<code javascript src/main/resources/assets/tutorial/blockstates/polished_andesite_vertical_slab.json>
-<code>+
 { {
   "variants": {   "variants": {
-    "facing=north": { "model": "bitmod:block/polished_andesite_side_block" }, +    "facing=north": { "model": "tutorial:block/polished_andesite_vertical_slab", "uvlock": true }, 
-    "facing=east":  { "model": "bitmod:block/polished_andesite_side_block", "y":  90}, +    "facing=east":  { "model": "tutorial:block/polished_andesite_vertical_slab", "y":  90, "uvlock": true }, 
-    "facing=south": { "model": "bitmod:block/polished_andesite_side_block", "y": 180 }, +    "facing=south": { "model": "tutorial:block/polished_andesite_vertical_slab", "y": 180, "uvlock": true }, 
-    "facing=west":  { "model": "bitmod:block/polished_andesite_side_block", "y": 270 }+    "facing=west":  { "model": "tutorial:block/polished_andesite_vertical_slab", "y": 270, "uvlock": true }
   }   }
 } }
 </code> </code>
- 
  
 ==== 定义方块模型 ==== ==== 定义方块模型 ====
  
- +<code javascript src/main/resources/assets/tutorial/models/block/vertical_slab.json>
-side.json +
-<code>+
 {   "parent": "block/block", {   "parent": "block/block",
     "textures": {     "textures": {
Line 72: Line 79:
             "to": [  16, 16, 8 ],             "to": [  16, 16, 8 ],
             "faces": {             "faces": {
-                "down": "uv": [ 0, 8, 16, 16 ], "texture": "#bottom", "cullface": "down" }, +                "down":  { "texture": "#bottom", "cullface": "down" }, 
-                "up":    { "uv": [ 0, 8, 16, 16 ], "texture": "#top",    "cullface": "up" }, +                "up":    { "texture": "#top",    "cullface": "up" }, 
-                "north":"uv": [ 0, 0, 16, 16 ], "texture": "#side",   "cullface": "north" }, +                "north": { "texture": "#side",   "cullface": "north" }, 
-                "south": { "uv": [ 0, 0, 16, 16 ], "texture": "#side },+                "south": { "texture": "#side"  "cullface": "south" },
                 "west":  { "texture": "#side",   "cullface": "west" },                 "west":  { "texture": "#side",   "cullface": "west" },
                 "east":  { "texture": "#side",   "cullface": "east" }                 "east":  { "texture": "#side",   "cullface": "east" }
Line 84: Line 91:
 </code> </code>
  
-polished_andesite_side_block.json +<code javascript src/main/resources/assets/tutorial/models/block/polished_andesite_vertical_slab.json>
-<code>+
 { {
-    "parent": "bitmod:block/side",+    "parent": "tutorial:block/vertical_slab",
     "textures": {     "textures": {
         "bottom": "block/polished_andesite",         "bottom": "block/polished_andesite",
Line 95: Line 101:
 } }
 </code> </code>
 +
 +===== 定义方块的旋转和翻转 =====
 +对于带有朝向的方块,你需要覆盖 ''rotate'' 和 ''mirror'' 方法,这样在结构方块中可以正确地被旋转或者翻转。不过,在这个例子中,''HorizontalFacingBlock'' 类已经帮你做好了。
 +
 +
 +===== 影响寻路 =====
 +如果在游戏内放置这些方块,可能会发现个问题,生物寻路时,会尝试这些方块,似乎这些方块不存在,结果被这些方块阻挡。这是因为,生物会将这些非完整的方块视为不挡路的方块。要修改这一寻路行为,需要修改 ''canPathfindThrough'' 方法:
 +<code java>
 +  @Override
 +  protected boolean canPathfindThrough(BlockState state, NavigationType type) {
 +    return false;
 +  }
 +</code>
 +
 +在原版中,有些方块有不同的路径节点类型(path node type)。例如,生物会避免危险的方块,比如岩浆块、凋零玫瑰和仙人掌。你也可以让你的生物以不同方式对待这些方块,方法就是利用 Fabric API 中的 ''LandPathNodeTypesRegistry'' 修改路径节点类型。
 +
 +===== 下一步 =====
 +尝试让它[[waterloggable|可含水]]
 +
zh_cn/tutorial/directionalblock.1605188501.txt.gz · Last modified: 2020/11/12 13:41 by solidblock