tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blocks [2023/11/18 08:26] – solidblock | tutorial:blocks [2025/04/02 01:13] (current) – [Giving your Block Visuals] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Adding a Block ====== | ====== Adding a Block ====== | ||
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 19: | Line 22: | ||
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 | + | // For versions below 1.20: |
+ | | ||
+ | | ||
+ | | ||
+ | // For versions since 1.20.5 below 1.21.2: | ||
+ | public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | ||
+ | | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
Line 28: | Line 37: | ||
</ | </ | ||
- | ==== Registering your Block ==== | + | ===== Registering your Block (before 1.21.2) ===== |
- | Blocks should be registered under the '' | + | Blocks should be registered under the '' |
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 { | ||
- | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); | + | |
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | + | |
+ | | ||
+ | | ||
+ | // For versions since 1.20.5 below 1.21.2: | ||
+ | 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, | + | |
+ | // Registry.register(Registries.BLOCK, | ||
+ | // For versions since 1.21: | ||
+ | Registry.register(Registries.BLOCK, | ||
} | } | ||
} | } | ||
Line 49: | 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 { | ||
- | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); | + | |
- | public static final Block EXAMPLE_BLOCK | + | |
+ | | ||
+ | | ||
+ | // For versions since 1.20.5 below 1.21.2: | ||
+ | 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, | Registry.register(Registries.BLOCK, | ||
- | Registry.register(Registries.ITEM, | + | |
+ | // Registry.register(Registries.ITEM, | ||
+ | |||
+ | // For versions below 1.21: | ||
+ | // Registry.register(Registries.ITEM, | ||
+ | |||
+ | // For versions since 1.21: | ||
+ | Registry.register(Registries.ITEM, | ||
} | } | ||
} | } | ||
+ | </ | ||
+ | |||
+ | ===== 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. | ||
+ | <code java TutorialBlocks.java> | ||
+ | public final class TutorialBlocks { | ||
+ | public static final Block EXAMPLE_BLOCK = register(" | ||
+ | | ||
+ | private static <T extends Block> T register(String path, T block) { | ||
+ | Registry.register(Registries.BLOCK, | ||
+ | Registry.register(Registries.ITEM, | ||
+ | return block; | ||
+ | } | ||
+ | | ||
+ | public static void initialize() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Remember to initialize the '' | ||
+ | <code java ExampleMod.java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | TutorialBlocks.initialize(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 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 70: | 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 103: | 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 117: | Line 220: | ||
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/ | + | For versions since 1.21, the path is '' |
+ | |||
+ | <code JavaScript src/ | ||
{ | { | ||
" | " | ||
Line 143: | Line 248: | ||
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: | 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: | ||
- | * Harvest tool: '' | + | * Harvest tool: '' |
- | * Harvest level: '' | + | * Harvest level: '' |
- | <code JavaScript src/ | + | <code JavaScript src/ |
{ | { | ||
" | " | ||
Line 155: | Line 260: | ||
</ | </ | ||
- | <code JavaScript src/ | + | <code JavaScript src/ |
{ | { | ||
" | " | ||
Line 164: | Line 269: | ||
</ | </ | ||
- | For the harvest level tags ('' | + | For the harvest level tags ('' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | | + | 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()); | + | |
</ | </ | ||
Line 175: | 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) { | ||
super(settings); | super(settings); | ||
Line 186: | 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 193: | Line 296: | ||
} | } | ||
+ | // For versions below 1.20.5, the parameters should be " | ||
@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(" | player.sendMessage(Text.literal(" | ||
Line 206: | 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 class ExampleMod implements ModInitializer | + | public |
- | | + | public static final Block EXAMPLE_BLOCK = register(" |
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | + | |
| | ||
- | | + | |
- | public void onInitialize() { | + | |
- | Registry.register(Registries.BLOCK, new Identifier(" | + | |
- | Registry.register(Registries.ITEM, | + | |
- | } | + | |
} | } | ||
</ | </ | ||
Line 228: | 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 237: | Line 336: | ||
} | } | ||
</ | </ | ||
- | |||
- | Note that the //collision shape// of the block defaults to the outline shape if it is not specified. | ||
{{: | {{: | ||
+ | |||
+ | 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 '' | ||
+ | * **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-person view. Glass and powder snow have an empty camera collision shape. | ||
===== 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.1700295991.txt.gz · Last modified: 2023/11/18 08:26 by solidblock