User Tools

Site Tools


tutorial:blocks

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
Next revisionBoth sides next revision
tutorial:blocks [2020/04/16 01:09] – Added required .INSTANCE to BlockRenderLayerMap example and added the example block name. darthjaketutorial:blocks [2020/06/14 00:17] – more formatting fixes + highlights draylar
Line 4: Line 4:
  
 To add a block to your mod, you will need to register a new instance of the Block class. For more control over your block, you can create a custom block class. We'll also look at adding a block model.  To add a block to your mod, you will need to register a new instance of the Block class. For more control over your block, you can create a custom block class. We'll also look at adding a block model. 
 +
 ==== Creating a Block ==== ==== Creating a Block ====
  
 To start, create an instance of Block in your main mod class. Block's constructor uses the FabricBlockSettings builder to set up basic properties of the block, such as hardness and resistance: To start, create an instance of Block in your main mod class. Block's constructor uses the FabricBlockSettings builder to set up basic properties of the block, such as hardness and resistance:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-{+
     // an instance of our new block     // an instance of our new block
-    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build()); +    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL)); 
-    [...]+     
 +    @Override 
 +    public void onInitialize() { 
 +         
 +    }
 } }
 </code> </code>
Line 20: Line 25:
 Registering blocks is the same as registering items. Call //Registry.register// and pass in the appropriate arguments. Registering blocks is the same as registering items. Call //Registry.register// and pass in the appropriate arguments.
  
-<code java [enable_line_numbers="true"]> +<code java [enable_line_numbers="true",highlight_lines_extra="8"]> 
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-+ 
-    // block creation +    // an instance of our new block 
-    [...]+    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL));
          
     @Override     @Override
-    public void onInitialize() +    public void onInitialize() {
-    {+
         Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);         Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
     }     }
Line 40: Line 44:
 In most cases, you want to be able to place your block using an item. To do this, you need to register a corresponding BlockItem in the item registry. You can do this by registering an instance of BlockItem under Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block. In most cases, you want to be able to place your block using an item. To do this, you need to register a corresponding BlockItem in the item registry. You can do this by registering an instance of BlockItem under Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block.
  
-<code java [enable_line_numbers="true"]> +<code java [enable_line_numbers="true",highlight_lines_extra="9"]> 
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-+ 
-    // block creation +    // an instance of our new block 
-    […]+    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL));
          
     @Override     @Override
-    public void onInitialize() +    public void onInitialize() { 
-    +        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
-        // block registration +
-        [...] +
-        +
         Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC)));         Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC)));
     }     }
Line 125: Line 126:
 When broken in survival mode, the block will now drop an item. When broken in survival mode, the block will now drop an item.
  
-==== Creating a Block class ====+==== Creating a Custom Block Class ==== 
 When creating a simple block the above approach works well, but sometimes you want a //special// block with unique mechanics. We'll create a separate class that extends Block to do this. The class needs a constructor that takes in a BlockSettings argument. When creating a simple block the above approach works well, but sometimes you want a //special// block with unique mechanics. We'll create a separate class that extends Block to do this. The class needs a constructor that takes in a BlockSettings argument.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class ExampleBlock extends Block +public class ExampleBlock extends Block { 
-+ 
-    public ExampleBlock(Settings settings) +    public ExampleBlock(Settings settings) {
-    {+
         super(settings);         super(settings);
     }     }
Line 158: Line 159:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-+ 
-    // an instance of our new item+    // an instance of our new block
     public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE));     public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE));
-    [...]+     
 +    @Override 
 +    public void onInitialize() { 
 +        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
 +        Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC))); 
 +    }
 } }
 </code> </code>
Line 171: Line 177:
 ==== Custom VoxelShape ==== ==== Custom VoxelShape ====
  
-When making custom blocks which do not entirely fill the block, the adjacent blocks might hide their faces. In this case of a custom vertical slab it would look like this:+When making custom blocks that do not entirely fill the block, the adjacent blocks might hide their faces. In this case of a custom vertical slab it would look like this:
  
 {{:tutorial:voxelshape_wrong.png?200|}} {{:tutorial:voxelshape_wrong.png?200|}}
tutorial/blocks.txt · Last modified: 2024/04/15 01:52 by solidblock