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/04/15 22:32] – Fixed loot table directory darthjake | tutorial:blocks [2020/06/14 00:58] – copy comments to bottom code block draylar | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Adding a Block ====== | ====== Adding a Block ====== | ||
- | ==== Introduction ==== | + | Adding blocks to your mod follows a similar process to [[tutorial: |
- | 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 | + | ===== Creating |
- | ==== Creating | + | |
+ | Start by creating an instance of '' | ||
- | To start, create an instance of Block in your main mod class. Block' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class ExampleMod implements ModInitializer | + | public class ExampleMod implements ModInitializer { |
- | { | + | |
- | // an instance of our new block | + | /* Declare and initialize |
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build()); | + | 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).hardness(4.0f)); | ||
+ | | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | |||
+ | } | ||
} | } | ||
</ | </ | ||
- | ==== Registering | + | ==== Registering |
- | Registering blocks is the same as registering items. Call // | + | Blocks should be registered under the '' |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
- | public class ExampleMod implements ModInitializer | + | public class ExampleMod implements ModInitializer { |
- | { | + | |
- | // block creation | + | /* Declare and initialize our custom |
- | | + | 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).hardness(4.0f)); | ||
| | ||
@Override | @Override | ||
- | public void onInitialize() | + | public void onInitialize() { |
- | | + | |
Registry.register(Registry.BLOCK, | Registry.register(Registry.BLOCK, | ||
} | } | ||
Line 34: | Line 43: | ||
</ | </ | ||
- | Your block will //not// be accessible as an item, but it can be seen in-game by using ''/ | + | Your custom |
- | ==== Registering | + | ==== Registering |
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=" | + | <code java [enable_line_numbers=" |
- | public class ExampleMod implements ModInitializer | + | public class ExampleMod implements ModInitializer { |
- | { | + | |
- | // block creation | + | // an instance of our new block |
- | | + | |
| | ||
@Override | @Override | ||
- | public void onInitialize() | + | public void onInitialize() { |
- | | + | |
- | | + | |
- | [...] | + | |
- | | + | |
Registry.register(Registry.ITEM, | Registry.register(Registry.ITEM, | ||
} | } | ||
Line 57: | Line 63: | ||
</ | </ | ||
- | ==== Giving your block a model ==== | + | ===== Giving your Block Visuals ===== |
- | As you probably have noticed, the block is simply | + | At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft' |
+ | * A blockstate file | ||
+ | * A block model file | ||
+ | * A texture | ||
+ | * An item model file (if the block has an item associated with it). | ||
+ | |||
+ | The files should be located here: | ||
Blockstate: src/ | Blockstate: src/ | ||
Line 66: | Line 78: | ||
Block Texture: src/ | Block Texture: src/ | ||
- | The blockstate file determines which model that the block should use depending on it' | + | The blockstate file determines which model a block should use depending on its blockstate. |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 76: | Line 88: | ||
</ | </ | ||
- | The block model file defines the shape and texture of your block. | + | The block model file defines the shape and texture of your block. |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 87: | Line 99: | ||
</ | </ | ||
- | 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 will want the block to look the same in item form. You can make an item model that parents |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 95: | Line 107: | ||
</ | </ | ||
- | Load up Minecraft and your block should | + | Load up Minecraft and your block should have visuals! |
- | ==== Adding a block loot table ==== | + | ===== Configuring Block Drops ===== |
- | The block must have a loot table for any items to drop when the block is broken. Assuming | + | 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/ | <code JavaScript src/ | ||
Line 123: | Line 135: | ||
</ | </ | ||
- | When broken in survival mode, the block will now drop an item. | + | ===== Creating a Custom Block Class ===== |
- | ==== Creating a Block class ==== | + | The above approach works well for simple items but falls short when you want a block with //unique// mechanics. We'll create a //separate// class that extends |
- | When creating a simple block the above approach works well, but sometimes | + | |
<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 138: | Line 148: | ||
</ | </ | ||
- | Just like we did in the item tutorial, you can override methods in the block class for custom functionality. | + | You can override methods in the block class for custom functionality. |
- | If you want your block to be transparent, | + | <code java [enable_line_numbers=" |
- | <code java> | + | @Override |
- | | + | public class ExampleBlock extends Block { |
- | </ | + | |
- | In versions before 1.15/ | + | public ExampleBlock(Settings settings) { |
+ | super(settings); | ||
+ | } | ||
- | <code java> | ||
- | @Environment(EnvType.CLIENT) | ||
@Override | @Override | ||
- | public | + | public |
- | return | + | if(!world.isClient) { |
+ | player.sendMessage(new LiteralText(" | ||
+ | } | ||
+ | |||
+ | return | ||
} | } | ||
+ | } | ||
</ | </ | ||
- | To add this block into the game, replace //new Block// with //new ExampleBlock// | + | To use your custom |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class ExampleMod implements ModInitializer | + | public class ExampleMod implements ModInitializer { |
- | { | + | |
- | // an instance | + | /* Declare and initialize our custom block instance |
- | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE)); | + | We set out block material to METAL, which requires a pickaxe to efficiently break. |
- | | + | |
+ | */ | ||
+ | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE).hardness(4.0f)); | ||
+ | | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | Registry.register(Registry.BLOCK, new Identifier(" | ||
+ | Registry.register(Registry.ITEM, | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | |||
- | Your custom block should now be transparent! | ||
- | |||
==== Custom VoxelShape ==== | ==== Custom VoxelShape ==== | ||
- | When making custom blocks which do not entirely fill the block, | + | When using block models that do not //entirely// fill the block (eg. Anvil, Slab, Stairs), adjacent blocks hide their faces: |
{{: | {{: | ||
- | + | To fix this, we have to define the //VoxelShape// of the new block: | |
- | We have to define the VoxelShape of the new block into one which is not an entire | + | |
< | < | ||
Line 185: | Line 203: | ||
</ | </ | ||
- | By doing so we also define getCollisionShape, | + | Note that the //collision shape// |
{{: | {{: | ||
- | ==== Next Steps ==== | + | ===== Next Steps ===== |
[[tutorial: | [[tutorial: | ||
[[tutorial: | [[tutorial: |
tutorial/blocks.txt · Last modified: 2024/08/26 01:22 by solidblock