tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blocks [2024/07/04 16:29] – mineblock11 | tutorial: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: | Adding blocks to your mod follows a similar process to [[tutorial: | ||
- | ===== Creating a Block ===== | + | ===== Creating a Block (before 1.21.2) |
+ | :!: If you are using 1.21.2 or later versions, please directly see [[# | ||
Start by creating an instance of '' | Start by creating an instance of '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
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 |
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: | ||
</ | </ | ||
- | ===== Registering your Block ===== | + | ===== Registering your Block (before 1.21.2) |
Blocks should be registered under the '' | Blocks should be registered under the '' | ||
Line 42: | Line 43: | ||
If you're using version 1.19.2 or below, please replace '' | If you're using version 1.19.2 or below, please replace '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
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 |
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 ''/ | Your custom block will //not// be accessible as an item yet, but it can be seen in-game by using the command ''/ | ||
- | ===== 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 '' | 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 '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
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 |
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, | // Registry.register(Registries.ITEM, | ||
+ | | ||
// For versions below 1.21: | // For versions below 1.21: | ||
// Registry.register(Registries.ITEM, | // Registry.register(Registries.ITEM, | ||
+ | | ||
// For versions since 1.21: | // For versions since 1.21: | ||
Registry.register(Registries.ITEM, | Registry.register(Registries.ITEM, | ||
Line 91: | Line 94: | ||
</ | </ | ||
- | ===== Best practice of registering blocks ===== | + | ===== Best practice of registering blocks |
+ | :!: 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> | + | <code java TutorialBlocks.java> |
public final class TutorialBlocks { | public final class TutorialBlocks { | ||
- | public static final Block EXAMPLE_BLOCK = register(new Block(Block.Settings.create().strength(4.0f)), " | + | public static final Block EXAMPLE_BLOCK = register(" |
| | ||
- | private static Block register(Block block, | + | private static |
Registry.register(Registries.BLOCK, | Registry.register(Registries.BLOCK, | ||
Registry.register(Registries.ITEM, | Registry.register(Registries.ITEM, | ||
+ | return block; | ||
} | } | ||
| | ||
Line 108: | Line 114: | ||
Remember to initialize the '' | Remember to initialize the '' | ||
- | <code java ExampleMod> | + | <code java ExampleMod.java> |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
@Override | @Override | ||
Line 115: | Line 121: | ||
} | } | ||
} | } | ||
+ | </ | ||
+ | |||
+ | ===== Registering blocks in 1.21.2+ ===== | ||
+ | In 1.21.2+, '' | ||
+ | |||
+ | <code java TutorialBlocks.java> | ||
+ | public class TutorialBlocks { | ||
+ | public static final Block EXAMPLE_BLOCK = register(" | ||
+ | |||
+ | private static Block register(String path, Function< | ||
+ | final Identifier identifier = Identifier.of(" | ||
+ | final RegistryKey< | ||
+ | |||
+ | final Block block = Blocks.register(registryKey, | ||
+ | Items.register(block); | ||
+ | return block; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In the code above, '' | ||
+ | |||
+ | 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(); | ||
+ | } | ||
+ | } | ||
</ | </ | ||
Line 120: | Line 159: | ||
At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft' | At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft' | ||
- | * A blockstate file | + | * A [[https:// |
- | * A block model file | + | * A [[https:// |
- | * A texture | + | * A texture |
- | * An item model file (if the block has an item associated with it). | + | * //For version 1.21.3 and below:// |
+ | * //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: '' | + | * Blockstates definition: '' |
- | * Block Model: '' | + | * Baked Block Model: '' |
- | * Item Model: '' | + | * Texture for the block: '' |
- | * Block Texture: '' | + | * //For version 1.21.3 and below:// |
+ | * //For version 1.21.4 and above:// Item Model: '' | ||
- | The blockstate file determines which model a block should use depending on its blockstate. Our block doesn' | + | The blockstate |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 153: | Line 194: | ||
</ | </ | ||
- | 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/ | <code JavaScript src/ | ||
{ | { | ||
" | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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/ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
} | } | ||
</ | </ | ||
Line 226: | Line 279: | ||
The above approach works well for simple blocks but falls short when you want a block with //unique// mechanics. We'll create a // | The above approach works well for simple blocks but falls short when you want a block with //unique// mechanics. We'll create a // | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
public ExampleBlock(Settings settings) { | public ExampleBlock(Settings settings) { | ||
Line 236: | Line 289: | ||
You can override methods in the block class for custom functionality. Here's an implementation of the '' | You can override methods in the block class for custom functionality. Here's an implementation of the '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
Line 257: | Line 310: | ||
To use your custom block class, replace '' | To use your custom block class, replace '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public final class TutorialBlocks { | public final class TutorialBlocks { | ||
- | public static final Block EXAMPLE_BLOCK = register(new | + | public static final Block EXAMPLE_BLOCK = register(" |
| | ||
// ... | // ... | ||
Line 274: | Line 327: | ||
To fix this, we have to define the '' | To fix this, we have to define the '' | ||
- | <code java> | + | < |
public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
[...] | [...] | ||
Line 293: | 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]]. |
- | + | | |
- | [[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 '' | To make your block flammable (that is, can be burned in fire), you may use '' |
tutorial/blocks.1720110572.txt.gz · Last modified: 2024/07/04 16:29 by mineblock11