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 [2024/10/27 14:09] – [Creating a Custom Block Class] fix typo solidblocktutorial:blocks [2025/04/02 01:13] (current) – [Giving your Block Visuals] solidblock
Line 5: Line 5:
 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]]. 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 (before 1.21.2) ===== 
 +:!: If you are using 1.21.2 or later versions, please directly see [[#Registering blocks in 1.21.2+]].
  
 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.
Line 26: Line 27:
     // For versions below 1.20.5:     // For versions below 1.20.5:
     // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));     // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
-    // For versions since 1.20.5:+    // For versions since 1.20.5 below 1.21.2:
     public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));     public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
          
Line 36: Line 37:
 </code> </code>
  
-===== Registering your Block =====+===== Registering your Block (before 1.21.2) =====
  
 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. 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.
Line 49: Line 50:
     // For versions below 1.20.5:     // For versions below 1.20.5:
     // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));     // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
-    // For versions since 1.20.5:+    // For versions since 1.20.5 below 1.21.2:
     public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));     public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
          
Line 64: Line 65:
 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 (before 1.21.2) =====
  
 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.
Line 75: Line 76:
     // For versions below 1.20.5:     // For versions below 1.20.5:
     // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));     // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
-    // For versions since 1.20.5:+    // For versions since 1.20.5 below 1.21.2:
     public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));     public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
          
Line 83: Line 84:
         // For versions below 1.20.5:         // For versions below 1.20.5:
         // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));         // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));
 +        
         // For versions below 1.21:         // For versions below 1.21:
         // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));         // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
 +        
         // For versions since 1.21:         // For versions since 1.21:
         Registry.register(Registries.ITEM, Identifier.of("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));         Registry.register(Registries.ITEM, Identifier.of("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
Line 91: Line 94:
 </code> </code>
  
-===== Best practice of registering blocks =====+===== Best practice of registering blocks befor 1.21.2 ===== 
 +:!: This section does not apply to versions 1.21.2 and later. 
 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. 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.java> <code java TutorialBlocks.java>
Line 116: Line 121:
     }     }
 } }
 +</code>
 +
 +===== Registering blocks in 1.21.2+ =====
 +In 1.21.2+, ''RegistryKey'' should be added into the ''AbstractBlock.Settings'' for the block, as well as ''Item.Settings'' for the item. It looks troublesome, but luckily, Minecraft's ''Blocks.//register//'' and ''Items.//register//'' helps you do that.
 +
 +<code java TutorialBlocks.java>
 +public class TutorialBlocks {
 +  public static final Block EXAMPLE_BLOCK = register("example_block", Block::new, Block.Settings.create().strength(4.0f));
 +
 +  private static Block register(String path, Function<AbstractBlock.Settings, Block> factory, AbstractBlock.Settings settings) {
 +    final Identifier identifier = Identifier.of("tutorial", path);
 +    final RegistryKey<Block> registryKey = RegistryKey.of(RegistryKeys.BLOCK, identifier);
 +
 +    final Block block = Blocks.register(registryKey, factory, settings);
 +    Items.register(block);
 +    return block;
 +  }
 +}
 +</code>
 +
 +In the code above, ''Blocks.//register//'' helps you to write the registry key into the ''AbstractBlock.Settings'' at first, and then create the block instance and register. ''Items.register'' directly creates a simple ''BlockItem'' instance, use the id same to the block, and then register it. If you need more complex operations, such as creating subclasses of ''BlockItem'', you may call other methods with different signatures which are also named ''Items.//register//''.
 +
 +Do not forget to static-load the class in the mod initializer:
 +<code java>
 +    public class ExampleMod implements ModInitializer {
 +     
 +      @Override
 +      public void onInitialize() {
 +        // ...
 +     
 +        TutorialBlocks.init();
 +      }
 +    }
 </code> </code>
  
Line 121: Line 159:
  
 At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft's way of showing you that something went wrong while loading the block's assets (or visuals). A full list of issues will be printed to your log when you run your client. You will need these files to give your block visuals: At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft's way of showing you that something went wrong while loading the block's assets (or visuals). A full list of issues will be printed to your log when you run your client. You will need these files to give your block visuals:
-  * A blockstate file +  * A [[https://minecraft.wiki/w/Blockstates_definition|blockstates definition]] 
-  * A block model file +  * A [[https://minecraft.wiki/w/Model#Uses_of_models|baked block model]] 
-  * A texture +  * A texture for the block 
-  * An item model file (if the block has an item associated with it).+  * //For version 1.21.3 and below:// An item model file (if the block has an item associated with it). 
 +  * //For version 1.21.4 and above:// An item model definition for the item (if the block has an item associated with it).
  
 The files should be located here: The files should be located here:
  
-  * Blockstate: ''src/main/resources/assets/tutorial/blockstates/example_block.json'' +  * Blockstates definition: ''src/main/resources/assets/tutorial/blockstates/example_block.json'' 
-  * Block Model: ''src/main/resources/assets/tutorial/models/block/example_block.json'' +  * Baked Block Model: ''src/main/resources/assets/tutorial/models/block/example_block.json'' 
-  * Item Model: ''src/main/resources/assets/tutorial/models/item/example_block.json'' +  * Texture for the block: ''src/main/resources/assets/tutorial/textures/block/example_block.png'' 
-  * Block Texture: ''src/main/resources/assets/tutorial/textures/block/example_block.png''+  * //For version 1.21.3 and below:// Item Model: ''src/main/resources/assets/tutorial/models/item/example_block.json'' 
 +  * //For version 1.21.4 and above:// Item Model: ''src/main/resources/assets/tutorial/items/example_block.json''
  
-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 definition file determines which model a block should use depending on its blockstate. Our block doesn't have any properties so it has only one state, so we cover everything with ''%%""%%''
  
 <code JavaScript src/main/resources/assets/tutorial/blockstates/example_block.json> <code JavaScript src/main/resources/assets/tutorial/blockstates/example_block.json>
Line 154: Line 194:
 </code> </code>
  
-In most cases, you will want the block to look the same in item form. You can make an item model that has the block model file as a parent, which makes it appear exactly like the block:+In most cases, you will want the block to look the same in item form.  
 + 
 +In version 1.21.3 and below, you can make an item model that has the block model file as a parent, which makes it appear exactly like the block:
  
 <code JavaScript src/main/resources/assets/tutorial/models/item/example_block.json> <code JavaScript src/main/resources/assets/tutorial/models/item/example_block.json>
 { {
   "parent": "tutorial:block/example_block"   "parent": "tutorial:block/example_block"
 +}
 +</code>
 +
 +For version 1.21.4 and above, you can create an item model definition for the corresponding to let the item directly use the block model:
 +<code JavaScript src/main/resources/assets/tutorial/items/example_block.json>
 +{
 +  "model": {
 +    "type": "minecraft:model",
 +    "model": "tutorial:block/example_block"
 +  }
 } }
 </code> </code>
Line 294: Line 346:
  
 ===== Next Steps ===== ===== Next Steps =====
-[[tutorial:blockstate|Adding simple state to a block, like ints and booleans]].  +  * [[blockstate|Adding simple state to a block, like ints and booleans]].  
- +  [[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.+  * [[datagen_model|Use data generator to generate block model, block model definition and item model definition for the block and item]]. 
 +  * [[datagen_tags|Use data generator to generate tags for the block]]. 
 +  * Don't forget your block cannot have a [[lang|translatable name]].
  
 To make your block flammable (that is, can be burned in fire), you may use ''FlammableBlockRegistry''. To make your block flammable (that is, can be burned in fire), you may use ''FlammableBlockRegistry''.
tutorial/blocks.1730038178.txt.gz · Last modified: 2024/10/27 14:09 by solidblock