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 [2019/03/19 00:22] – fixed some english stuff draylar | tutorial:blocks [2019/11/14 22:00] – fudge | ||
---|---|---|---|
Line 3: | Line 3: | ||
==== Introduction ==== | ==== Introduction ==== | ||
- | To add a block to your mod, you will need to register a new instance of the Block class. For more control over your block, you can create a custom block class that extends Block. We'll also look at adding a block model. | + | To add a block to your mod, you will need to register a new instance of the Block class. For more control over your block, you can create a custom block class. We'll also look at adding a block model. |
- | + | ||
- | | + | |
==== Creating a Block ==== | ==== Creating a Block ==== | ||
- | To start, create an instance of Block in your main mod class. Block' | + | To start, create an instance of Block in your main mod class. Block' |
+ | <code java [enable_line_numbers=" | ||
+ | public class ExampleMod implements ModInitializer | ||
+ | { | ||
+ | // an instance of our new block | ||
+ | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build()); | ||
+ | [...] | ||
+ | } | ||
+ | </ | ||
- | public class ExampleMod implements ModInitializer | ||
- | { | ||
- | // an instance of our new block | ||
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build()); | ||
- | [...] | ||
- | } | ||
- | | ||
==== Registering a Block ==== | ==== Registering a Block ==== | ||
- | When adding most things to the game, you will need to register it. You can call // | + | Registering blocks is the same as registering items. Call // |
- | | + | <code java [enable_line_numbers=" |
- | { | + | public class ExampleMod implements ModInitializer |
- | // block creation | + | { |
- | […] | + | // block creation |
- | + | […] | |
- | @Override | + | |
- | public void onInitialize() | + | @Override |
- | { | + | public void onInitialize() |
- | Registry.register(Registry.BLOCK, | + | { |
- | } | + | Registry.register(Registry.BLOCK, |
- | } | + | } |
+ | } | ||
+ | </ | ||
- | Your block will not be accessible as an item, but it can be seen in-game by using / | + | Your block will //not// be accessible as an item, but it can be seen in-game by using '' |
==== Registering a BlockItem ==== | ==== Registering a BlockItem ==== | ||
- | 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 Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block. |
- | | + | <code java [enable_line_numbers=" |
- | { | + | public class ExampleMod implements ModInitializer |
- | // block creation | + | { |
- | […] | + | // block creation |
- | + | […] | |
- | @Override | + | |
- | public void onInitialize() | + | @Override |
- | { | + | public void onInitialize() |
- | // block registration | + | { |
- | [...] | + | // block registration |
- | + | [...] | |
- | Registry.register(Registry.ITEM, | + | |
- | } | + | Registry.register(Registry.ITEM, |
- | } | + | } |
+ | } | ||
+ | </ | ||
==== Giving your block a model ==== | ==== Giving your block a model ==== | ||
Line 58: | Line 61: | ||
As you probably have noticed, the block is simply a purple and black checkerboard pattern in-game. This is Minecraft' | As you probably have noticed, the block is simply a purple and black checkerboard pattern in-game. This is Minecraft' | ||
- | Blockstate: src/ | + | Blockstate: src/ |
- | Block Model: src/ | + | Block Model: src/ |
- | Item Model: src/ | + | Item Model: src/ |
- | Block Texture: src/ | + | Block Texture: src/ |
The blockstate file determines which model that the block should use depending on it's blockstate. As our block has only one state, the file is a simple as this: | The blockstate file determines which model that the block should use depending on it's blockstate. As our block has only one state, the file is a simple as this: | ||
- | | + | <code JavaScript src/ |
- | " | + | { |
- | "": | + | " |
- | } | + | "": |
- | } | + | } |
+ | } | ||
+ | </ | ||
The block model file defines the shape and texture of your block. We will use block/ | The block model file defines the shape and texture of your block. We will use block/ | ||
- | | + | <code JavaScript src/ |
- | " | + | { |
- | " | + | " |
- | " | + | " |
- | } | + | " |
} | } | ||
+ | } | ||
+ | </ | ||
In most cases you want the block to look the same in hand. To do this, you can make an item file that inherits from the block model file: | In most cases you want the block to look the same in hand. To do this, you can make an item file that inherits from the block model file: | ||
- | | + | <code JavaScript src/ |
- | " | + | { |
- | } | + | " |
+ | } | ||
+ | </ | ||
Load up Minecraft and your block should finally have a texture! | Load up Minecraft and your block should finally have a texture! | ||
+ | |||
+ | ==== Adding a block loot table ==== | ||
+ | |||
+ | The block must have a loot table for any items to drop when the block is broken. Assuming you have created an item for your block and registered it using the same name as the block, the following file will produce regular block drops '' | ||
+ | |||
+ | <code JavaScript src/ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ], | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | When broken in survival mode, the block will now drop an item. | ||
==== Creating a Block class ==== | ==== Creating a Block class ==== | ||
- | When creating a simple | + | When creating a simple block the above approach |
- | | + | <code java [enable_line_numbers=" |
- | { | + | public class ExampleBlock extends Block |
- | public ExampleBlock(Settings settings) | + | { |
- | { | + | public ExampleBlock(Settings settings) |
- | super(settings); | + | { |
- | } | + | super(settings); |
- | } | + | } |
+ | } | ||
+ | </ | ||
- | Here in the your custom block class you can customize your blocks | + | Just like we did in the item tutorial, |
- | | + | <code java> |
- | | + | |
- | | + | public BlockRenderLayer getRenderLayer() { |
- | | + | return BlockRenderLayer.TRANSLUCENT; |
+ | } | ||
+ | </ | ||
- | To add this block into the game you have to replace //new Block// with //new ExampleBlock// | + | To add this block into the game, replace //new Block// with //new ExampleBlock// |
- | | + | <code java [enable_line_numbers=" |
- | { | + | public class ExampleMod implements ModInitializer |
- | // an instance of our new item | + | { |
- | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE)); | + | // an instance of our new item |
- | [...] | + | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE)); |
- | } | + | [...] |
- | + | } | |
- | Your custom block should now be transparent | + | </ |
+ | |||
+ | Your custom block should now be transparent! | ||
+ | ==== Next Steps ==== | ||
+ | [[tutorial: | ||
+ | |||
+ | [[tutorial: |
tutorial/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11