tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blocks [2022/12/16 01:19] – [Custom VoxelShape] solidblock | tutorial:blocks [2024/06/21 21:37] – [Custom Shape] ryhon | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== 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 ===== | ||
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)); | + | |
+ | // For versions below 1.20: | ||
+ | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); | ||
+ | // For versions below 1.20.5: | ||
+ | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
+ | // For versions since 1.20.5: | ||
+ | public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | ||
| | ||
@Override | @Override | ||
Line 37: | Line 43: | ||
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)); | ||
+ | // For versions below 1.20.5: | ||
+ | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
+ | // For versions since 1.20.5: | ||
+ | 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, | ||
} | } | ||
} | } | ||
</ | </ | ||
- | 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 ==== | ||
Line 55: | Line 69: | ||
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)); | ||
+ | // For versions below 1.20.5: | ||
+ | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
+ | // For versions since 1.20.5: | ||
+ | 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, | ||
} | } | ||
} | } | ||
Line 75: | Line 99: | ||
The files should be located here: | The files should be located here: | ||
- | Blockstate: src/ | + | |
- | Block Model: src/ | + | |
- | Item Model: src/ | + | |
- | Block Texture: src/ | + | |
The blockstate file determines which model a block should use depending on its blockstate. Our block doesn' | The blockstate file determines which model a block should use depending on its blockstate. Our block doesn' | ||
Line 115: | Line 139: | ||
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 136: | Line 162: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | The condition '' | ||
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 level: src/ | + | * Harvest level: |
- | <code JavaScript src/ | + | <code JavaScript src/ |
{ | { | ||
" | " | ||
Line 151: | Line 179: | ||
</ | </ | ||
- | <code JavaScript src/ | + | <code JavaScript src/ |
{ | { | ||
" | " | ||
Line 160: | Line 188: | ||
</ | </ | ||
- | For the harvest level tags (needs_stone_tool, | + | For the harvest level tags ('' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public static final Block EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.METAL).strength(4.0f).requiresTool()); | + | public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool()); |
</ | </ | ||
Line 189: | Line 216: | ||
} | } | ||
+ | // 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.of(" | + | player.sendMessage(Text.literal(" |
} | } | ||
Line 205: | Line 233: | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | public static final ExampleBlock | + | |
+ | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
| | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | Registry.register(Registries.BLOCK, | + | |
- | Registry.register(Registries.ITEM, | + | // Registry.register(Registries.BLOCK, |
+ | | ||
+ | |||
+ | // For versions since 1.21: | ||
+ | Registry.register(Registries.BLOCK, Identifier.of(" | ||
+ | Registry.register(Registries.ITEM, | ||
} | } | ||
} | } | ||
Line 224: | Line 258: | ||
<code java> | <code java> | ||
- | public class ExambleBlock | + | public class ExampleBlock |
[...] | [...] | ||
@Override | @Override | ||
Line 232: | Line 266: | ||
} | } | ||
</ | </ | ||
- | |||
- | 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/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11