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/04/15 01:44] – update solidblock | tutorial:blocks [2024/07/04 16:29] (current) – mineblock11 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Adding a Block ====== | ====== Adding a Block ====== | ||
Line 7: | Line 9: | ||
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 34: | Line 36: | ||
</ | </ | ||
- | ==== Registering your Block ==== | + | ===== Registering your Block ===== |
- | 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 { | ||
Line 52: | Line 54: | ||
@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 59: | Line 64: | ||
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 ===== |
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 77: | Line 82: | ||
Registry.register(Registries.BLOCK, | Registry.register(Registries.BLOCK, | ||
// For versions below 1.20.5: | // For versions below 1.20.5: | ||
- | Registry.register(Registries.ITEM, | + | |
- | // For versions | + | // For versions |
- | Registry.register(Registries.ITEM, | + | |
+ | // For versions since 1.21: | ||
+ | Registry.register(Registries.ITEM, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 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)), | ||
+ | |||
+ | private static Block register(Block block, String path) { | ||
+ | Registry.register(Registries.BLOCK, | ||
+ | Registry.register(Registries.ITEM, | ||
+ | } | ||
+ | |||
+ | public static void initialize() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Remember to initialize the '' | ||
+ | <code java ExampleMod> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | TutorialBlocks.initialize(); | ||
} | } | ||
} | } | ||
Line 134: | Line 167: | ||
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 160: | Line 195: | ||
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 172: | Line 207: | ||
</ | </ | ||
- | <code JavaScript src/ | + | <code JavaScript src/ |
{ | { | ||
" | " | ||
Line 193: | Line 228: | ||
<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 211: | Line 245: | ||
// For versions below 1.20.5, the parameters should be " | // For versions below 1.20.5, the parameters should be " | ||
@Override | @Override | ||
- | public ActionResult onUse(World world, PlayerEntity player, BlockHitResult hit) { | + | public ActionResult onUse(BlockState state, |
if (!world.isClient) { | if (!world.isClient) { | ||
player.sendMessage(Text.literal(" | player.sendMessage(Text.literal(" | ||
Line 224: | Line 258: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class ExampleMod implements ModInitializer | + | public |
- | | + | public static final Block EXAMPLE_BLOCK = register(new ExampleBLock(Block.Settings.create().strength(4.0f)), " |
- | 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 259: | Line 288: | ||
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 '' | + | * **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. | * **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 |
===== Next Steps ===== | ===== Next Steps ===== |
tutorial/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11