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 [2019/12/24 09:35] bitmantutorial:directionalblock [2023/09/26 05:43] (current) – Fix typo [Making a Directional Block] poopooracoocoo
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 8: Line 7:
  
 <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 37: Line 36:
  }  }
  
 +        @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());
  }  }
  
 +}
 +</code>
 +<code java>
 +public class ExampleMod implements ModInitializer {
 +    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>
  
 +===== Defining Blockstate JSON =====
  
-==== Defining Blockstate ==== +<code javascript src/main/resources/assets/tutorial/blockstates/polished_andesite_vertical_slab.json>
- +
-polished_andesite_side_block.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 76:
             "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 88:
 </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 98:
 } }
 </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.
 +
 +===== Next =====
 +Try to make it [[waterloggable]].
tutorial/directionalblock.1577180129.txt.gz · Last modified: 2019/12/24 09:35 by bitman