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 [2020/06/14 01:00] – fix highlighting + more comments draylar | tutorial:blocks [2024/04/15 01:45] – [Registering an Item for your Block] solidblock | ||
---|---|---|---|
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 11: | Line 11: | ||
/* Declare and initialize our custom block instance. | /* Declare and initialize our custom block instance. | ||
- | We set out block material to METAL, which requires | + | We set our block material to `METAL`. |
- | | + | |
+ | | ||
+ | | ||
+ | Stone has a hardness of 1.5f and a resistance of 6.0f, while Obsidian has a hardness of 50.0f and a resistance of 1200.0f. | ||
+ | |||
+ | 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).hardness(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 25: | 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 '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | /* Declare and initialize our custom block instance. | + | // For versions below 1.20: |
- | We set out block material to METAL, which requires a pickaxe to efficiently break. | + | // 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.of(Material.METAL).hardness(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(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
} | } | ||
} | } | ||
</ | </ | ||
- | 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 Registry.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 '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | /* Declare and initialize our custom block instance. | + | // For versions below 1.20: |
- | We set out block material to METAL, which requires a pickaxe to efficiently break. | + | // 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)); |
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL)); | + | // 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(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.ITEM, new Identifier(" | + | |
+ | // Registry.register(Registries.ITEM, new Identifier(" | ||
+ | // For versions since 1.20.5: | ||
+ | Registry.register(Registries.ITEM, new Identifier(" | ||
} | } | ||
} | } | ||
Line 76: | Line 94: | ||
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 91: | Line 109: | ||
</ | </ | ||
- | The block model file defines the shape and texture of your block. Our model will parent | + | The block model file defines the shape and texture of your block. Our model will have '' |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 102: | Line 120: | ||
</ | </ | ||
- | In most cases, you will want the block to look the same in item form. You can make an item model that parents | + | 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: |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 136: | Line 154: | ||
] | ] | ||
} | } | ||
+ | </ | ||
+ | |||
+ | 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: | ||
+ | |||
+ | * Harvest tool: '' | ||
+ | * Harvest level: '' | ||
+ | |||
+ | <code JavaScript src/ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code JavaScript src/ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | For the harvest level tags ('' | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool()); | ||
</ | </ | ||
===== Creating a Custom Block Class ===== | ===== Creating a Custom Block Class ===== | ||
- | The above approach works well for simple | + | The above approach works well for simple |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 153: | Line 202: | ||
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=" |
- | @Override | + | |
public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
Line 161: | Line 209: | ||
} | } | ||
+ | // For versions below 1.20.5, the parameters should be " | ||
@Override | @Override | ||
- | public ActionResult onUse(BlockState state, | + | public ActionResult onUse(World world, PlayerEntity player, BlockHitResult hit) { |
- | if(!world.isClient) { | + | if (!world.isClient) { |
- | player.sendMessage(new LiteralText(" | + | player.sendMessage(Text.literal(" |
} | } | ||
Line 172: | Line 221: | ||
</ | </ | ||
- | 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 class ExampleMod implements ModInitializer { | ||
- | /* Declare and initialize our custom block instance using our new ExampleBlock class. | + | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); // fabric api version <= 0.77.0 |
- | We set out block material to METAL, which requires a pickaxe to efficiently break. | + | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); |
- | | + | |
- | */ | + | |
- | public static final ExampleBlock | + | |
| | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | Registry.register(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.ITEM, new Identifier(" | + | Registry.register(Registries.ITEM, new Identifier(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ==== Custom | + | ==== Custom |
- | When using block models that do not // | + | When using block models that do not // |
{{: | {{: | ||
- | To fix this, we have to define the //VoxelShape// of the new block: | + | To fix this, we have to define the '' |
- | < | + | < |
- | @Override | + | public class ExampleBlock extends Block { |
- | | + | [...] |
- | | + | |
- | } | + | public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { |
+ | return VoxelShapes.cuboid(0f, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | |||
- | 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-party view. Glass and powder snow have an empty camera collision shape. | ||
===== Next Steps ===== | ===== Next Steps ===== | ||
Line 214: | Line 267: | ||
[[tutorial: | [[tutorial: | ||
+ | |||
+ | To make your block flammable (that is, can be burned in fire), you may use '' |
tutorial/blocks.txt · Last modified: 2024/06/13 08:12 by s1lverposting