User Tools

Site Tools


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
tutorial:directionalblock [2021/02/15 06:48] – [Directional Blocks] Updated getOutlineShpe to fit current syntax arbeetutorial:directionalblock [2024/12/08 14:04] (current) – [Influence path finding] typo solidblock
Line 1: Line 1:
-==== Directional Blocks ==== +====== Making a Directional Block ======
  
 Making blocks directional (facing into certain directions) is also done using block states. Making blocks directional (facing into certain directions) is also done using block states.
Line 7: Line 6:
 {{:tutorial:vertslab.png?200|}} {{:tutorial:vertslab.png?200|}}
  
-<code java> +<code java VerticalSlabBlock.java> 
-public class PolishedAndesiteSideBlock extends HorizontalFacingBlock {+public class VerticalSlabBlock extends HorizontalFacingBlock { 
 + // the codec is required since 1.20.5 however not actually used in Minecraft yet. 
 + 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, ShapeContext 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 44: Line 46:
 </code> </code>
  
 +and then register the block according to the method covered in [[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>
  
-==== Defining Blockstate ====+===== Defining Blockstate 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>
  
 +===== Defining Block Models =====
  
-==== Defining Block Models ==== +<code javascript src/main/resources/assets/tutorial/models/block/vertical_slab.json>
- +
- +
-side.json +
-<code>+
 {   "parent": "block/block", {   "parent": "block/block",
     "textures": {     "textures": {
Line 73: Line 80:
             "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 85: Line 92:
 </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 96: Line 102:
 } }
 </code> </code>
 +
 +===== Defining rotation and mirroring of blocks =====
 +For directional blocks, you may have to override ''rotate'' and ''mirror'' methods, so that in structure blocks, they can be correctly rotated or mirrored. However, in this case, the ''HorizontalFacingBlock'' class has already done it for you.
 +
 +===== Influence path finding =====
 +If you place these blocks in game, you may find the issue that, the mobs when trying to find paths, will try to cross the blocks, as if the blocks did not exist, ending up being blocked by the blocks. That's because mobs treat such non-full-cube blocks as those not blocking their path. To modify this path-finding behavior, you need to override ''canPathfindThrough'' method:
 +<code java>
 +  @Override
 +  protected boolean canPathfindThrough(BlockState state, NavigationType type) {
 +    return false;
 +  }
 +</code>
 +
 +In vanilla, some blocks have different path node types. For example, mobs will avoid some dangerous blocks, such as magma block, wither rose and cactus. You can also make mobs treat your blocks differently, by modifying path node types, via ''LandPathNodeTypesRegistry'' of Fabric API.
 +
 +===== Next =====
 +Try to make it [[waterloggable]].
tutorial/directionalblock.1613371730.txt.gz · Last modified: 2021/02/15 06:48 by arbee