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 [2023/03/15 21:21] haykamtutorial:directionalblock [2024/12/08 14:04] (current) – [Influence path finding] typo solidblock
Line 6: Line 6:
 {{:tutorial:vertslab.png?200|}} {{:tutorial:vertslab.png?200|}}
  
-<code java>+<code java VerticalSlabBlock.java>
 public class VerticalSlabBlock 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 VerticalSlabBlock(Settings settings) {  public VerticalSlabBlock(Settings settings) {
  super(settings);  super(settings);
  setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));  setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
 + }
 +
 + @Override
 + protected MapCodec<? extends VerticalSlabBlock> getCodec() {
 + return CODEC;
  }  }
  
Line 22: Line 29:
  public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {  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(); +
- }+
  }  }
  
Line 43: Line 45:
 } }
 </code> </code>
-<code java> + 
-public class ExampleMod implements ModInitialzer +and then register the block according to the method covered in [[blocks]]: 
-    public static final VerticalSlabBlock POLISHED_ANDESITE_VERTICAL_SLAB = Registry.register( +<code java TutorialBlocks.java> 
-        Registries.BLOCK, +public final class TutorialBlocks implements ModInitializer { 
-        new Identifier("tutorial", "polished_andesite_vertical_slab")+    [...] 
-        new VerticalSlabBlock(FabricBlockSettings.copyOf(Blocks.POLISHED_ANDESITE)));+     
 +    public static final VerticalSlabBlock POLISHED_ANDESITE_VERTICAL_SLAB = register("polished_andesite_vertical_slab",  
 +        new VerticalSlabBlock(Block.Settings.copy(Blocks.POLISHED_ANDESITE)));
 } }
 </code> </code>
Line 101: Line 105:
 ===== Defining rotation and mirroring of blocks ===== ===== 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. 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 ===== ===== Next =====
 Try to make it [[waterloggable]]. Try to make it [[waterloggable]].
tutorial/directionalblock.1678915301.txt.gz · Last modified: 2023/03/15 21:21 by haykam