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

Both sides previous revisionPrevious revision
Next revision
Previous revision
zh_cn:tutorial:directionalblock [2022/02/28 00:56] solidblockzh_cn:tutorial:directionalblock [2022/12/16 02:09] (current) – title size solidblock
Line 1: Line 1:
-==== 带有方向的方块 ====+====== 制作向的方块 ======
  
-使方块带有方向(朝向特定的方向)也是通过方块状态完成的。 +使方块带有方向(朝向特定的方向)也是通过方块状态完成的。这个例子介绍了垂直版的安山岩台阶。
-这个例子介绍了垂直版的安山岩台阶。+
  
 {{:tutorial:vertslab.png?200|}} {{:tutorial:vertslab.png?200|}}
  
 <code java> <code java>
-public class PolishedAndesiteSideBlock extends HorizontalFacingBlock {+public class VerticalSlabBlock extends HorizontalFacingBlock {
  
- 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, BlockState> stateManager) { + protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { 
- stateManager.add(Properties.HORIZONTAL_FACING);+ builder.add(Properties.HORIZONTAL_FACING);
  }  }
  
  @Override  @Override
- public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, EntityContext ctx) {+ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
  Direction dir = state.get(FACING);  Direction dir = state.get(FACING);
  switch(dir) {  switch(dir) {
Line 36: Line 35:
  }  }
  
 +        @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.getPlayerFacing().getOpposite());
  }  }
  
 +}
 +</code>
 +<code java>
 +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)));
 } }
 </code> </code>
  
 +==== 定义方块状态 JSON ====
  
-==== 定义方块状态 ==== +<code javascript src/main/resources/assets/tutorial/blockstates/polished_andesite_vertical_slab.json>
- +
- +
-<code javascript src/main/resources/assets/tutorial/blockstates/polished_andesite_side_block.json>+
 { {
   "variants": {   "variants": {
-    "facing=north": { "model": "bitmod:block/polished_andesite_side_block", "uvlock": true }, +    "facing=north": { "model": "tutorial:block/polished_andesite_vertical_slab", "uvlock": true }, 
-    "facing=east":  { "model": "bitmod:block/polished_andesite_side_block", "y":  90, "uvlock": true }, +    "facing=east":  { "model": "tutorial:block/polished_andesite_vertical_slab", "y":  90, "uvlock": true }, 
-    "facing=south": { "model": "bitmod:block/polished_andesite_side_block", "y": 180, "uvlock": true }, +    "facing=south": { "model": "tutorial:block/polished_andesite_vertical_slab", "y": 180, "uvlock": true }, 
-    "facing=west":  { "model": "bitmod:block/polished_andesite_side_block", "y": 270, "uvlock": true }+    "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>
-<code javascript src/main/resources/assets/tutorial/models/block/side.json>+
 {   "parent": "block/block", {   "parent": "block/block",
     "textures": {     "textures": {
Line 83: Line 87:
 </code> </code>
  
-<code javascript src/main/resources/assets/tutorial/models/block/polished_andesite_side_block.json>+<code javascript src/main/resources/assets/tutorial/models/block/polished_andesite_vertical_slab.json>
 { {
-    "parent": "tutorial:block/side",+    "parent": "tutorial:block/vertical_slab",
     "textures": {     "textures": {
         "bottom": "block/polished_andesite",         "bottom": "block/polished_andesite",
Line 93: Line 97:
 } }
 </code> </code>
 +
 +===== 定义方块的旋转和翻转 =====
 +对于带有朝向的方块,你需要覆盖 ''rotate'' 和 ''mirror'' 方法,这样在结构方块中可以正确地被旋转或者翻转。不过,在这个例子中,''HorizontalFacingBlock'' 类已经帮你做好了。
 +
 +===== 下一步 =====
 +尝试让它[[waterloggable|可含水]]
 +
zh_cn/tutorial/directionalblock.1646009763.txt.gz · Last modified: 2022/02/28 00:56 by solidblock