tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorial:blocks [2020/04/16 01:09] – Added required .INSTANCE to BlockRenderLayerMap example and added the example block name. darthjake | tutorial:blocks [2024/04/15 01:45] – [Registering an Item for your Block] solidblock | ||
---|---|---|---|
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 | + | /* Declare and initialize our custom block instance. |
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build()); | + | 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. | ||
+ | | ||
+ | |||
+ | // For versions below 1.20: | ||
+ | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); | ||
+ | | ||
+ | // 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 | ||
+ | public void onInitialize() { | ||
+ | |||
+ | } | ||
} | } | ||
</ | </ | ||
- | ==== Registering | + | ==== Registering |
- | Registering blocks is the same as registering items. Call //Registry.register// | + | Blocks should be registered under the '' |
- | <code java [enable_line_numbers=" | + | If you're using version 1.19.2 or below, please replace '' |
- | public class ExampleMod implements ModInitializer | + | |
- | { | + | <code java [enable_line_numbers=" |
- | // block creation | + | public class ExampleMod implements ModInitializer { |
- | | + | |
+ | // For versions below 1.20: | ||
+ | | ||
+ | // 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, new Identifier(" |
- | Registry.register(Registry.BLOCK, new Identifier(" | + | |
} | } | ||
} | } | ||
</ | </ | ||
- | 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 '' |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
- | public class ExampleMod implements ModInitializer | + | public class ExampleMod implements ModInitializer { |
- | { | + | |
- | // block creation | + | // For versions below 1.20: |
- | | + | |
+ | // 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, |
- | // block registration | + | // For versions below 1.20.5: |
- | | + | |
- | + | | |
- | Registry.register(Registry.ITEM, new Identifier(" | + | Registry.register(Registries.ITEM, new Identifier(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ==== 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). | ||
- | Blockstate: src/ | + | The files should be located here: |
- | Block Model: src/ | + | |
- | Item Model: src/ | + | |
- | Block Texture: src/ | + | |
- | The blockstate file determines which model that the block should use depending on it' | + | * Blockstate: '' |
+ | * Block Model: '' | ||
+ | * Item Model: '' | ||
+ | * Block Texture: '' | ||
+ | |||
+ | The blockstate file determines which model a block should use depending on its blockstate. | ||
<code JavaScript src/ | <code JavaScript src/ | ||
Line 76: | Line 109: | ||
</ | </ | ||
- | 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 120: | ||
</ | </ | ||
- | 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 has the block model file as a parent, which makes it appear exactly like the block: |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 95: | Line 128: | ||
</ | </ | ||
- | 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 156: | ||
</ | </ | ||
- | When broken in survival mode, the block will now drop an item. | + | The condition '' |
- | ==== Creating a Block class ==== | + | 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: |
- | When creating a simple block the above approach works well, but sometimes you want a //special// block with unique mechanics. We'll create a separate class that extends Block to do this. The class needs a constructor | + | |
- | <code java [enable_line_numbers=" | + | * Harvest tool: '' |
- | public class ExampleBlock extends Block | + | * Harvest level: '' |
+ | |||
+ | <code JavaScript src/ | ||
{ | { | ||
- | public ExampleBlock(Settings settings) | + | " |
- | | + | " |
- | | + | |
- | } | + | ] |
} | } | ||
</ | </ | ||
- | Just like we did in the item tutorial, you can override methods in the block class for custom functionality. | + | <code JavaScript src/ |
- | + | { | |
- | If you want your block to be transparent, in your client mod initializer code, do: | + | " |
- | <code java> | + | " |
- | | + | " |
+ | ] | ||
+ | } | ||
</ | </ | ||
- | In versions | + | For the harvest level tags ('' |
- | <code java> | + | <code java [enable_line_numbers=" |
- | | + | |
- | @Override | + | |
- | public BlockRenderLayer getRenderLayer() { | + | |
- | return BlockRenderLayer.TRANSLUCENT; | + | |
- | } | + | |
</ | </ | ||
- | To add this block into the game, replace | + | ===== Creating a Custom Block Class ===== |
+ | |||
+ | The above approach works well for simple blocks but falls short when you want a block with //unique// mechanics. We'll create a //separate// class that extends '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class ExampleMod implements ModInitializer | + | public class ExampleBlock extends Block { |
- | { | + | |
- | // an instance of our new item | + | public ExampleBlock(Settings |
- | public | + | super(settings); |
- | | + | |
} | } | ||
</ | </ | ||
- | Your custom block should now be transparent! | + | You can override methods in the block class for custom |
+ | <code java [enable_line_numbers=" | ||
+ | public class ExampleBlock extends Block { | ||
- | ==== Custom VoxelShape ==== | + | public ExampleBlock(Settings settings) { |
+ | super(settings); | ||
+ | } | ||
- | When making custom blocks which do not entirely fill the block, the adjacent blocks might hide their faces. In this case of a custom vertical slab it would look like this: | + | // For versions below 1.20.5, the parameters should be " |
+ | @Override | ||
+ | public ActionResult onUse(World world, PlayerEntity player, BlockHitResult hit) { | ||
+ | if (!world.isClient) { | ||
+ | player.sendMessage(Text.literal(" | ||
+ | } | ||
- | {{: | + | return ActionResult.SUCCESS; |
+ | | ||
+ | } | ||
+ | </ | ||
+ | To use your custom block class, replace '' | ||
- | We have to define the VoxelShape of the new block into one which is not an entire block: | + | <code java [enable_line_numbers=" |
+ | public class ExampleMod implements ModInitializer { | ||
- | <code> | + | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); |
- | @Override | + | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); |
- | | + | |
- | return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f); | + | @Override |
- | } | + | public |
+ | Registry.register(Registries.BLOCK, new Identifier(" | ||
+ | | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | By doing so we also define | + | ==== Custom Shape==== |
+ | |||
+ | When using block models that do not // | ||
+ | |||
+ | {{: | ||
+ | |||
+ | To fix this, we have to define the '' | ||
+ | |||
+ | <code java> | ||
+ | public class ExampleBlock extends Block { | ||
+ | [...] | ||
+ | @Override | ||
+ | public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { | ||
+ | return VoxelShapes.cuboid(0f, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
{{: | {{: | ||
- | ==== Next Steps ==== | + | 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 ===== | ||
[[tutorial: | [[tutorial: | ||
[[tutorial: | [[tutorial: | ||
+ | |||
+ | To make your block flammable (that is, can be burned in fire), you may use '' |
tutorial/blocks.txt · Last modified: 2024/04/15 01:52 by solidblock