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
Last revisionBoth sides next revision
tutorial:blocks [2024/04/15 01:37] – [Creating a Block] update solidblocktutorial:blocks [2024/07/03 07:27] solidblock
Line 7: Line 7:
 Start by creating an instance of ''Block''. It can be stored at any location, but we will start at the top of your ''ModInitializer''. The ''Block'' constructor requires an ''AbstractBlock.Settings'' instance, which is a builder for configuring block properties. Fabric provides a ''FabricBlockSettings'' builder class with more available options. Start by creating an instance of ''Block''. It can be stored at any location, but we will start at the top of your ''ModInitializer''. The ''Block'' constructor requires an ''AbstractBlock.Settings'' instance, which is a builder for configuring block properties. Fabric provides a ''FabricBlockSettings'' builder class with more available options.
  
-<code java [enable_line_numbers="true"]>+<code java [enable_line_numbers="true"ExampleMod>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
Line 34: Line 34:
 </code> </code>
  
-==== Registering your Block ====+===== Registering your Block =====
  
-Blocks should be registered under the ''Registries.BLOCK'' registry. Call ''Registry.//register//'' and pass in the appropriate arguments. You can either register the block in ''onInitialize'' method or directly when creating the block instance in the static context, as the ''register'' method returns the block instance as well.+Blocks should be registered under the ''Registries.BLOCK'' registry. Similar to registering [[items]], just call ''Registry.//register//'' and pass in the appropriate arguments. You can either register the block in ''onInitialize'' method or directly when creating the block instance in the static context, as the ''register'' method returns the block instance as well.
  
 If you're using version 1.19.2 or below, please replace ''Registries.BLOCK'' with ''Registry.BLOCK'' If you're using version 1.19.2 or below, please replace ''Registries.BLOCK'' with ''Registry.BLOCK''
  
-<code java [enable_line_numbers="true",highlight_lines_extra="11"]>+<code java [enable_line_numbers="true",highlight_lines_extra="11"ExampleMod>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); // fabric api version <= 0.77.0 +    // For versions below 1.20: 
-    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));+    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +    // For versions below 1.20.5: 
 +    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); 
 +    // For versions since 1.20.5: 
 +    public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-        Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);+        // For versions below 1.21: 
 +        // Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
 +        // For versions since 1.21: 
 +        Registry.register(Registries.BLOCK, Identifier.of("tutorial", "example_block"), EXAMPLE_BLOCK);
     }     }
 } }
Line 55: Line 62:
 Your custom block will //not// be accessible as an item yet, but it can be seen in-game by using the command ''/setblock <position> tutorial:example_block''. Your custom block will //not// be accessible as an item yet, but it can be seen in-game by using the command ''/setblock <position> tutorial:example_block''.
  
-==== Registering an Item for your Block ====+===== Registering an Item for your 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 ''Registries.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 ''Registries.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",highlight_lines_extra="12"]>+<code java [enable_line_numbers="true",highlight_lines_extra="12"ExampleMod>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); // fabric api version <= 0.77.0 +    // For versions below 1.20: 
-    public static final Block EXAMPLE_BLOCK  = new Block(FabricBlockSettings.create().strength(4.0f));+    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +    // For versions below 1.20.5: 
 +    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); 
 +    // For versions since 1.20.5: 
 +    public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
         Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);         Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
-        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));+        // For versions below 1.20.5: 
 +        // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings())); 
 +        // For versions below 1.21: 
 +        // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings())); 
 +        // For versions since 1.21: 
 +        Registry.register(Registries.ITEM, Identifier.of("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings())); 
 +    } 
 +
 +</code> 
 + 
 +===== Best practice of registering blocks ===== 
 +Sometimes you have many blocks in the mod. If you register them in such ways, you have to write complex codes for each of them, and the code will be messy. Therefore, similar to registering items, we create a separate class for blocks, and a utility methods to register the block and item. 
 +<code java TutorialBlocks> 
 +public final class TutorialBlocks { 
 +    public static final Block EXAMPLE_BLOCK = register(new Block(Block.Settings.create().strength(4.0f)), "example_block"); 
 +     
 +    private static Block register(Block block, String path) { 
 +        Registry.register(Registries.BLOCK, Identifier.of("tutorial", path), block); 
 +        Registry.register(Registries.ITEM, Identifier.of("tutorial", path), new BlockItem(block, new Item.Settings())); 
 +    } 
 +     
 +    public static void initialize() { 
 +    } 
 +
 +</code> 
 + 
 +Remember to initialize the ''TutorialBlocks'' class in the ''ModInitializer'': 
 +<code java ExampleMod> 
 +public class ExampleMod implements ModInitializer { 
 +    @Override 
 +    public void onInitialize() { 
 +        TutorialBlocks.initialize();
     }     }
 } }
Line 123: Line 165:
 To make your block drop items when broken, you will need a //loot table//. The following file will cause your block to drop its respective item form when broken:  To make your block drop items when broken, you will need a //loot table//. The following file will cause your block to drop its respective item form when broken: 
  
-<code JavaScript src/main/resources/data/tutorial/loot_tables/blocks/example_block.json>+For versions since 1.21, the path is ''src/main/resources/data/tutorial/**loot_table**/blocks/example_block.json''. Before version 1.21, the path was ''src/main/resources/data/tutorial/**loot_tables**/blocks/example_block.json''
 + 
 +<code JavaScript src/main/resources/data/tutorial/loot_table/blocks/example_block.json>
 { {
   "type": "minecraft:block",   "type": "minecraft:block",
Line 149: Line 193:
 In minecraft 1.17, there has been a change for breaking blocks. Now, to define harvest tools and harvest levels, we need to use tags. Read about tags at: [[tutorial:tags|Tags Tutorial]]. The tags that we need to add the block to are: In minecraft 1.17, there has been a change for breaking blocks. Now, to define harvest tools and harvest levels, we need to use tags. Read about tags at: [[tutorial:tags|Tags Tutorial]]. The tags that we need to add the block to are:
  
- * Harvest tool: ''src/main/resources/data/minecraft/tags/blocks/mineable/<tooltype>.json'', where ''<tooltype>'' can be any of: ''axe'', ''pickaxe'', ''shovel'' or ''hoe'' + * Harvest tool: ''src/main/resources/data/minecraft/tags/**block**/mineable/<tooltype>.json'', where ''<tooltype>'' can be any of: ''axe'', ''pickaxe'', ''shovel'' or ''hoe'' (replace "//**block**//" with "//**blocks**//" for versions below 1.21) 
- * Harvest level: ''src/main/resources/data/minecraft/tags/blocks/needs_<tier>_tool.json'', where ''<tier>'' can be any of: ''stone'', ''iron'' or ''diamond'' (//not including// ''netherite'')+ * Harvest level: ''src/main/resources/data/minecraft/tags/**block**/needs_<tier>_tool.json'', where ''<tier>'' can be any of: ''stone'', ''iron'' or ''diamond'' (//not including// ''netherite'') (replace "//**block**//" with "//**blocks**//" for versions below 1.21)
  
-<code JavaScript src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json>+<code JavaScript src/main/resources/data/minecraft/tags/block/mineable/pickaxe.json>
 { {
   "replace": false,   "replace": false,
Line 161: Line 205:
 </code> </code>
  
-<code JavaScript src/main/resources/data/minecraft/tags/blocks/needs_stone_tool.json>+<code JavaScript src/main/resources/data/minecraft/tags/block/needs_stone_tool.json>
 { {
   "replace": false,   "replace": false,
Line 170: Line 214:
 </code> </code>
  
-For the harvest level tags (''needs_stone_tool'', ''needs_iron_tool'' and ''needs_diamond_tool'') to take effect, add ''requiresTool()'' to the FabricBlockSettings in the block declaration:+For the harvest level tags (''needs_stone_tool'', ''needs_iron_tool'' and ''needs_diamond_tool'') to take effect, add ''requiresTool()'' to the ''Block.Settings'' in the block declaration (example of versions above 1.20.5):
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-    // public static final Block EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.METAL).strength(4.0f).requiresTool()); // fabric api version <= 0.77.0 +    public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool());
-    public static final Block EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.create().strength(4.0f).requiresTool());+
 </code> </code>
  
Line 183: Line 226:
 <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 199: Line 241:
     }     }
  
 +    // For versions below 1.20.5, the parameters should be "BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit"
     @Override     @Override
-    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {+    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
         if (!world.isClient) {         if (!world.isClient) {
             player.sendMessage(Text.literal("Hello, world!"), false);             player.sendMessage(Text.literal("Hello, world!"), false);
Line 213: Line 256:
  
 <code java [enable_line_numbers="true",highlight_lines_extra="3"]> <code java [enable_line_numbers="true",highlight_lines_extra="3"]>
-public class ExampleMod implements ModInitializer {+public final class TutorialBlocks {
  
-    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); // fabric api version <= 0.77.0 +    public static final Block EXAMPLE_BLOCK = register(new ExampleBLock(Block.Settings.create().strength(4.0f)), "example_mod);
-    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));+
          
-    @Override +    // ...
-    public void onInitialize() { +
-        Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); +
-        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings())); +
-    }+
 } }
 </code> </code>
Line 248: Line 286:
 You can also define other types of shapes for the block. The type of shapes of blocks include: You can also define other types of shapes for the block. The type of shapes of blocks include:
   * **outline shape**: the shape used as default value for most type of shapes. In the worlds, when you points to the shape, the translucent black outline is displayed according to this shape. Most times it should not be empty.   * **outline shape**: the shape used as default value for most type of shapes. In the worlds, when you points to the shape, the translucent black outline is displayed according to this shape. Most times it should not be empty.
-  * **collision shape**: the shape used to calculate collisions. When entities (including players) are moving, their collision box usually cannot intersect the collision shape of blocks. Some blocks, such as fences and walls, may have a collision shape higher than one block. Some blocks, such as flowers, have an empty collision shape. Apart from modifying ''getCollisionShape'' method, you can also call ''noCollision'' in the ''FabricBlockSettings'' when creating the block.+  * **collision shape**: the shape used to calculate collisions. When entities (including players) are moving, their collision box usually cannot intersect the collision shape of blocks. Some blocks, such as fences and walls, may have a collision shape higher than one block. Some blocks, such as flowers, have an empty collision shape. Apart from modifying ''getCollisionShape'' method, you can also call ''noCollision'' in the ''Block.Settings'' when creating the block.
   * **raycasting shape**: the shape used to calculate raycasting (the process judging which block you are pointing to). You usually do not need to specify it.   * **raycasting shape**: the shape used to calculate raycasting (the process judging which block you are pointing to). You usually do not need to specify it.
-  * **camera collision shape**: the shape used to calculate the position of camera in third-party view. Glass and powder snow have an empty camera collision shape.+  * **camera collision shape**: the shape used to calculate the position of camera in third-person view. Glass and powder snow have an empty camera collision shape.
  
 ===== Next Steps ===== ===== Next Steps =====
tutorial/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11