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
tutorial:blocks [2021/11/02 13:04] – add requiresTool() to FabricBlockSettings to make harvest level tags work suicuiunetutorial:blocks [2023/11/18 08:34] (current) solidblock
Line 1: Line 1:
 ====== Adding a Block ====== ====== Adding a Block ======
  
-Adding blocks to your mod follows a similar process to [[tutorial:items|adding an item]]. You can create an instance of ''Block'' or a custom class, and then register it under ''Registry.BLOCK''. You also need to provide a texture and blockstate/model file to give your block visuals. For more information on the block model format, view the [[https://minecraft.gamepedia.com/Model|Minecraft Wiki Model page]].+Adding blocks to your mod follows a similar process to [[tutorial:items|adding an item]]. You can create an instance of ''Block'' or a custom class, and then register it under ''Registries.BLOCK'' (for 1.19.3 and above) or ''Registry.BLOCK'' (for 1.19.2 and below). You also need to provide a texture and blockstate/model file to give your block visuals. For more information on the block model format, view the [[https://minecraft.wiki/Model|Minecraft Wiki Model page]].
  
 ===== Creating a Block ===== ===== Creating a Block =====
Line 11: Line 11:
  
     /* Declare and initialize our custom block instance.     /* Declare and initialize our custom block instance.
-       We set our block material to `METAL`, which requires a pickaxe to efficiently break.+       We set our block material to `METAL`.
                
        `strength` sets both the hardness and the resistance of a block to the same value.        `strength` sets both the hardness and the resistance of a block to the same value.
Line 19: Line 19:
        You can find the stats of all vanilla blocks in the class `Blocks`, where you can also reference other blocks.        You can find the stats of all vanilla blocks in the class `Blocks`, where you can also reference other blocks.
     */     */
-    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); +    // 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  = new Block(FabricBlockSettings.create().strength(4.0f));
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
Line 30: Line 30:
 ==== Registering your Block ==== ==== Registering your Block ====
  
-Blocks should be registered under the ''Registry.BLOCK'' registry. Call ''Registry.//register//'' and pass in the appropriate arguments. You can register the block in ''onInitialize'' method or directly when creating the block instance in the static context.+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. 
 + 
 +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"]>
 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));+    // 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 = new Block(FabricBlockSettings.create().strength(4.0f));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);+        Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
     }     }
 } }
 </code> </code>
  
-Your custom block will //not// be accessible as an item yet, but it can be seen in-game by using the command ''/setblock 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 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 ''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"]>
 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));+    // 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  = new Block(FabricBlockSettings.create().strength(4.0f));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); +        Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
-        Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings().group(ItemGroup.MISC)));+        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));
     }     }
 } }
Line 73: Line 77:
 The files should be located here: The files should be located here:
  
-  Blockstate: src/main/resources/assets/tutorial/blockstates/example_block.json +  Blockstate: ''src/main/resources/assets/tutorial/blockstates/example_block.json'' 
-  Block Model: src/main/resources/assets/tutorial/models/block/example_block.json +  Block Model: ''src/main/resources/assets/tutorial/models/block/example_block.json'' 
-  Item Model: src/main/resources/assets/tutorial/models/item/example_block.json +  Item Model: ''src/main/resources/assets/tutorial/models/item/example_block.json'' 
-  Block Texture: src/main/resources/assets/tutorial/textures/block/example_block.png+  Block Texture: ''src/main/resources/assets/tutorial/textures/block/example_block.png''
  
 The blockstate file determines which model a block should use depending on its blockstate. Our block doesn't have any potential states, so we cover everything with ''""'' The blockstate file determines which model a block should use depending on its blockstate. Our block doesn't have any potential states, so we cover everything with ''""''
Line 134: Line 138:
 } }
 </code> </code>
 +
 +The condition ''minecraft:survives_explosion'' means that, if the block is destroyed in an explosion with decay (such as creeper explosion, not TNT explosion), it //may not// drop. If there is no this condition, the block always drops in explosions with decay.
  
 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' ore 'hoe' + Harvest tool: ''src/main/resources/data/minecraft/tags/blocks/mineable/<tooltype>.json''where ''<tooltype>'' can be any of: ''axe'', ''pickaxe'', ''shovel'' or ''hoe'
-  Harvest level: src/main/resources/data/minecraft/tags/blocks/needs_<tier>_tool.json, Where 'tier' can be any of: 'stone', 'iron' or 'diamond'+ 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'')
  
 <code JavaScript src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json> <code JavaScript src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json>
Line 144: Line 150:
   "replace": false,   "replace": false,
   "values": [   "values": [
-    "example:example_block"+    "tutorial:example_block"
   ]   ]
 } }
Line 153: Line 159:
   "replace": false,   "replace": false,
   "values": [   "values": [
-    "example:example_block"+    "tutorial:example_block"
   ]   ]
 } }
 </code> </code>
  
-For the harvest level tags (needs_stone_tool, needs_iron_tool and needs_diamond_tool) to take effect, add requiresTool() to the FabricToolSettings 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 FabricBlockSettings in the block declaration:
  
 <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());+    // 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(FabricBlockSettings.create().strength(4.0f).requiresTool());
 </code> </code>
  
Line 190: Line 196:
     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, Hand hand, BlockHitResult hit) {
         if (!world.isClient) {         if (!world.isClient) {
-            player.sendMessage(new LiteralText("Hello, world!"), false);+            player.sendMessage(Text.literal("Hello, world!"), false);
         }         }
  
Line 198: Line 204:
 </code> </code>
  
-To use your custom block class, replace //new Block// with //new ExampleBlock//:+To use your custom block class, replace ''new Block'' with ''new ExampleBlock'':
  
 <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 class ExampleMod implements ModInitializer {
  
-    public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.STONE).hardness(4.0f));+    // 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 = new Block(FabricBlockSettings.create().strength(4.0f));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); +        Registry.register(Registries.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)));+        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
     }     }
 } }
 </code> </code>
  
-==== Custom VoxelShape ====+==== Custom Shape====
  
-When using block models that do not //entirely// fill the block (eg. Anvil, Slab, Stairs), adjacent blocks hide their faces:+When using block models that do not //entirely// fill the block (eg. Anvil, Slab, Stairs), while its voxel shape is full, the hidden faces of the adjacent blocks will be exposed:
  
 {{:tutorial:voxelshape_wrong.png?200|}} {{:tutorial:voxelshape_wrong.png?200|}}
Line 221: Line 228:
 To fix this, we have to define the ''VoxelShape'' of the new block: To fix this, we have to define the ''VoxelShape'' of the new block:
  
-<code> +<code java
- @Override +public class ExampleBlock extends Block { 
- public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { +    [...] 
-     return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f); +    @Override 
- }+    public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { 
 +        return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f); 
 +    } 
 +}
 </code> </code>
- 
-Note that the //collision shape// of the block defaults to the outline shape if it is not specified. 
  
 {{:tutorial:voxelshape_fixed.png?200|}} {{:tutorial:voxelshape_fixed.png?200|}}
 +
 +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.
 +  * **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.
 +  * **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.
  
 ===== Next Steps ===== ===== Next Steps =====
Line 236: Line 250:
  
 [[tutorial:blockentity|Giving blocks a block entity so they can have advanced state like inventories]]. Also needed for many things like GUI and custom block rendering. [[tutorial:blockentity|Giving blocks a block entity so they can have advanced state like inventories]]. Also needed for many things like GUI and custom block rendering.
 +
 +To make your block flammable (that is, can be burned in fire), you may use ''FlammableBlockRegistry''.
tutorial/blocks.1635858256.txt.gz · Last modified: 2021/11/02 13:04 by suicuiune